Social Networking/E-Commerce Web Design & Development

rss

PHP / mySQL / xml / xHTML / CSS / DOM - By Dennis Plucinik

Setting up multiple virtual hosts in WAMP

Like many others I run a development version of a local Apache webhosting server called WAMP. Up until today I had a normal file structure built for clients and specific client work. Usually I build a large majority of the site locally before uploading it to a production server. This has caused me a lot of problems with using absolute paths because I basically couldn’t. Before moving to a production server, I would have to manually change the paths appropriately.

What I wanted was to have a root folder for each client. I am happy to say I found a solution which I will share with you now.

There are two files involved:

- hosts (this file is in the c:\windows\system32\drivers\etc\ folder)
- httpd.conf (this is in your Apache install’s ‘conf’ folder - mine is c:\wamp\Apache2\conf\httpd.conf)

When you open the “hosts” file, you will see this line at the bottom:

127.0.0.1        localhost

 

All you do is make another entry underneath it for your client site:

127.0.0.1        myclient.local (Use whatever extension you want (ex: .local))

 

…and save the file. You can make as many as you want for as many clients as you want. Just keep adding more lines like that last one. Basically what happens is when you type ‘myclient.local’ into a browser now, windows will check the ‘hosts’ file before checking the internet.

Now you need to open the httpd.conf file and add a couple things. Some other sites suggest doing this - search for this line:

 #NameVirtualHost *:80

 

…then add some crap underneath it to reroute ports and whatnot. If you’re in the same boat as me, this doesn’t work because ‘#NameVirtualHost *:80′ is not in my httpd.conf file.

Here’s what I did instead. Underneath ‘DocumentRoot “c:/wamp/www”‘ add this chunk of code:

NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
    ServerName localhost
    DocumentRoot "C:wampwww"
</VirtualHost>

 

…then underneath that add this chunk:

<VirtualHost 127.0.0.1>
    ServerName myclient.local
    DocumentRoot "C:wampwwwClientsMyClient"
</VirtualHost>

 

…obviously you’ll need to modify the code a bit to match the server name with the one from your ‘hosts’ file and the DocumentRoot with your clients file path. That’s basically it. The problem you have now is that when you try and access your files from another computer on your local network, the sites somehow revert back to not liking absolute paths. If I figure this part out, I’ll come back and do a little update. For now, I’m satisfied.

Here are some other older articles/threads on this topic which may be helpful getting a better overall feel for this kind of crap. I’m no server admin but I figured if I’m asking this question now, then so is someone else so I hope this could help shed some light for you.

http://httpd.apache.org/docs/1.3/mod/core.html#virtualhost
http://www.wampserver.com/phorum/read.php?f=2&i=8200&t=8114
http://codylindley.com/Webdev/309/wamp-multiple-roots-using-virtual-host?commented=1#c001924
http://www.thewatchmakerproject.com/journal/378/virtual-hosts-and-the-proper-way-to-work-offline (this one was the key - Thanks Matt!)

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google]

34 Comments, Comment or Ping

  1. gravatar

    Tim

    THANKYOU!!! this is just what i had been looking for! Cheers mate!

  2. gravatar

    yup, cool, thanks for the tip!!! perfect for work in progress space when you’ve got more than one job on the go.

    no security issues if it’s all localhost aye!

  3. gravatar

    Scott

    I just got the latest version of wamp with apache 2.2.6. In my httpd.conf file I found this:

    # Virtual hosts
    #Include conf/extra/httpd-vhosts.conf

    So, check “C:\wamp\bin\apache\apache2.2.6\conf\extra\httpd-vhosts.conf” for the vhost info. Though, it’s still probably easier to add that information into the httpd.conf file since you guys are probably changing it all the time.

    Personally, I add shortcuts to all my development apps and files I edit often to a directory of tools.

  4. gravatar

    I have been basically just including a config file which defines the absolute root so when I deploy a site, all I have to do is change the defined root in the one config file and the rest just falls in place.

    I have gotten away from the vhosts basically because I work on three machines with each running different versions of WAMP or XAMPP or whatever (I can thank Vista and it’s unmatched ability to manage a local network* that the only way I can transfer files across my network to a Mac is by setting up a web server and making it locally public.)

    * sarcasm.

  5. gravatar

    You can add aliases by clicking on the WAMP system tray menu, then going to: Apache > Aliases directories > “add alias”. You’ll be prompted with a simple wizard to make aliases to localhost and point to a folder anywhere your computer can access (local/network/etc.)

    I wish the *AMP packages did the same for VHosts.

  6. gravatar

    Leo

    Below might solve your problem with accessing the web server from other machines. It works for me. Note also includes logging.

    ServerName dev.leoplaw.com
    ServerAdmin admin@dev.leoplaw.com
    DocumentRoot “E:/Projects/Leo Plaw/Website-dev/website”

    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
    Order allow,deny
    Allow from all

    DirectoryIndex index.php
    ErrorLog logs/dev.leoplaw.com-error_log
    CustomLog logs/dev.leoplaw.com-access_log common

  7. gravatar

    Dennis - I setup my hosts file and made the changes to the httpd.conf file so when I try to access my website i use either localhost/qhxmas.local or qhxmas.local: Iget a 403 ‘Forbidden error’ thats says that it found the webpage but I don’t have permission to access /qhxmas.local on this server. Did I miss a step on setting permissions somewhere? I don’t have a logon for my site. I am using Vista Ultimate w/ SP1 though. Any ideas? Thanks in advance.

  8. gravatar

    Thank you anyway — I found the problem, in your directions you included the changes needed for the httpd.conf file and I took them literally. ie:”C:wampwwwClientsMyClient” so I left out the slashes. Didn’t look right when I was doing it but then again I don’t know Apache. Thanks for a much easier way of working multiple joomla sites on one machine.

  9. gravatar

    i need help configuring wamp server as web server i do have my domain name at register.com just that i cannot point the domain to my localhost that ive created im going to to up 3 diffrent websites , in the long run im going to set wamp as web hosting server can someone help me . thanks

  10. gravatar

    Martin

    SOLUTION :)

    I’ve been doing this for quite a while now. What you need to do is in your hosts file use an IP that the other computers on your network can see and reach.

    Here’s what your hostfile would look like if your DEV PC had the IP of 192.168.0.202. It’s better that you set a static IP on your DEV PC so that this works all the time.

    127.0.0.1 localhost
    192.168.0.202 myclientsite1
    192.168.0.202 myclientsite2

    You can copy that hosts file to your other computers on your network and they will also be able to find your sites. You can also add the names to your DNS server if you like.

    It’s not recommended that you hosts sites on the 127.0.0.1 IP since you will only be able to access them from your local PC unless you fiddle around with your apache conf.

    One last thing is to make sure to update the .conf file for apache.

    ServerName localhost
    DocumentRoot “C:wampwww”

    …then underneath that add this chunk:

    ServerName myclientsite1
    DocumentRoot “C:wampwwwClientsMyClient”

    Enjoy!

  11. gravatar

    Tanmay

    Thanks mate. You made it simple for me. keep it up

  12. gravatar

    I made my changes to /extra/httpd-vhosts.conf and I needed to uncomment the following line from httpd.conf:

    Include conf/extra/httpd-vhosts.conf

    thanks for the ‘tut. very useful :D

  13. gravatar

    Problem i’m having is when requiring/including files in PHP.

    On a production server; on a domain - you can always be sure that you can include files like: “/includes/foo.bar”

    Under WAMP; because i’m missing/have forgotten the right bit of config to do - “/” goes right back to the root of your entire hard drive.

    How to persuade PHP that /wamp/www/myclient1 is the root for that vhost, so when you include something with / - thats as far up as it goes?

    Done it before, brain won’t recall how I did it tho :(

  14. gravatar

    @codecraft - I’ve been creating a sitewide config file that gets wrapped up and included into every page on the site. In this config file, I just define the ROOT (ex:http://www.yoursite.com or http://localhost/clients/xyzcompany…)

    Seems to be working fine for me so far. Once you get into using a framework like Cake with a solid dev/QA/prod environment setup I think this issue will resolve itself in a way.

  15. gravatar

    Manjusha

    Hi Dennisplucinik,

    I have built a simple website on Wamp. My website’s contents are stored in ~/wamp/www and I can access my site at say, www.site1.com.

    Now, I’m trying to host one more website on the same Wamp server. That is, I now have two different websites (www.site1.com, www.site2.com) on my Wamp server. Their contents are stored in ~/wamp/www/site1 and ~/wamp/www/site2.

    As per the instructions given in the above article, I added:
    127.0.0.1 site1.com
    127.0.0.1 site2.com
    to the hosts file in c:\windows\system32\drivers\etc\ folder.

    And the following chunk to apache httpd.conf:

    NameVirtualHost 127.0.0.1

    ServerName localhost
    DocumentRoot “~/wamp/www”

    ServerName www.site1.com
    DocumentRoot “~/wamp/www/site1″

    After restarting the wamp server, firing www.site1.com in the browser loads the directory structure of ~/wamp/www folder, but it doesn’t load the index.php in ~/wamp/www/site1 (as expected). Have I missed out anything?

    Pls advice!

  16. gravatar

    Thanks mate, lifesafer

  17. gravatar

    Michelle Rupisan

    I was actually looking for this..thank you very much for the post..it helps a lot!!!

    Regards..

  18. gravatar

    nad

    I have setup wamp local host server fine. i have two different domains with two separate vhosts. Trouble is which ever vhost file is on top in http.config.vhost it shows for both domains from outside.
    Local lan is working fine for both local vhost domains..
    Any help please. I am trying to host two domains on wamp forwarding 1 external ip to wamp server……… .

    thank you in advance for any assistance

  19. gravatar

    I finally got it all figured out! This article was very helpful. This was my final result:

    http://www.lifetimeblogs.com/comment.php?type=trackback&entry_id=5

  20. gravatar

    sparty

    I am having the same problem as @codecraft. The line “./” should take you to site’s root and not to the root of your wamp installation which it currently does. I am trying to install drupal on one of my virtual hosts. Moving the drupal files in to the c:\wamp\www everything works fine, but moving the files to c:\wamp\www\mysite\ it complains when it reaches the line “./include/bootstrap.inc”. Sure if you are building your own site like @dennisplucinik suggests then you can specify and ROOT but I don’t think that will work in this case. Has anyone else solved this problem and can give us some insight

  21. gravatar

    This is great. I couldn’t seem to get it by coppying the other code in the files… It’s been driving me insane, thanks!

  22. gravatar

    eric

    Trying to make this work on Vista. The hosts config works fine, but the Apache hosts.conf config doesn’t seem to have any effect at all. I think in Vista you absolutely have to use the include httpd-vhosts.conf method.

    Found this recipe, which has gotten me part of the way there but still short of the mark:

    http://guides.jlbn.net/setvh/setvh3.html

    Basically, those instructions make some inscrutable changes to the httpd-vhosts.conf file and since the instructions are inscrutable, I can’t tell why every subdomain is now using exactly the same root folder.

  23. gravatar

    Mert CAN

    Thanks, i am using zendframework with virtualhost.

  24. gravatar

    Kevin

    Thanks!

  25. gravatar

    Melounek

    Thanks very much :)

    only little confusing for me was this path :-$
    C:wampwwwClientsMyClient

  26. gravatar

    Thanks so much. Just what I was looking for

  27. gravatar

    Mark

    Well I made 2 httpd.conf files called client1.conf and client2.conf
    and each conf file had a different (wamp\www\client1 or client2) and same for the
    And then I made 2 batch files, 1 for starting each client.

    rem batch file to start client1
    cd\
    cd wamp\bin\apache\Apache2.2.11\conf
    Del httpd.conf
    copy client1.conf httpd.conf
    cd\
    start /d “C:\wamp” wampmanager.exe
    rem finished batch file

    Then I made a shortcut to each batch file.
    This seems to work fine, but not tested in action yet.

  28. gravatar

    Ravi

    Thanks, you made it simple, working great!!!

  29. gravatar

    Thanks. It surely Helps me out.

Reply to “Setting up multiple virtual hosts in WAMP”