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 any extension (e.g., .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:\wamp\www"
</VirtualHost>

 

...then underneath that add this chunk:

<VirtualHost 127.0.0.1>
    ServerName myclient.local
    DocumentRoot "C:\wamp\www\Clients\MyClient"
</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 client's file path. But 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!)

"You don't have permission to access / on this server"

1/6/11 UPDATE:
I had some trouble today getting my vhosts to work properly on a new workstation. I was getting the same error that @greg heller noted below - something like "you don't have permission to access / on this server". After a couple hours of racking my brain, I came across a post on ServerFault that saved me.

Basically, the folder I was pointing the vhost to had the wrong permissions set. So to remedy this, I did just what the post recommended and added this block to my httpd.conf file:

<Directory "/Volumes/Macintosh HD 2/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

Now everything is working smoothly :)

"The requested URL /{some_url} was not found on this server."

12/21/12 UPDATE:
I recently moved all my project files to an external drive and encountered the message above when navigating to any links past the root. What happened was Apache basically couldn't map to a different drive. It's likely more complicated than that but it doesn't matter. The solution is to create an "alias" in Apache.

Left click on your Wamp Server tray icon, click "Apache" > "Alias Directories" > "Add an alias"

A command prompt will open up explaining the next two steps. The first step is typing in the name of your alias. I used "external". The next and last step is to type in the path to the drive you want to map the alias to. I used "J:/" which is the path to my 1TB external Western Digital HD.

Next, in your httpd.conf file, set up permissions for the new "external" alias directory

<Directory "/external">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

Next, in your vhosts.conf file, instead of this:

<VirtualHost 127.0.0.1>
   DocumentRoot "J:/Dropbox/Clients/Current/ProjectName"
   ServerName projectname.local
</VirtualHost>

Replace the J:/ with "/external" like so:

<VirtualHost 127.0.0.1>
   DocumentRoot "/external/Dropbox/Clients/Current/ProjectName"
   ServerName projectname.local
</VirtualHost>

Here are the two links that helped me figure this out:
http://www.webmasterworld.com/apache/3949190.htm
http://www.webmasterworld.com/forum92/1273.htm

I hope this helps spare you some misery. I'm always surprised about how often I get tripped up over vhosts even after years of dealing with them. I'm sure I'll be back in another year with an update. Until then, happy coding!

Related Books

  1. Tim says:

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

  2. reuben says:

    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. Scott says:

    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. 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. 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. Leo says:

    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. Greg Heller says:

    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. Greg Heller says:

    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. judick says:

    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. Martin says:

    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. Tanmay says:

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

  12. 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. Codecraft says:

    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. @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. Manjusha says:

    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, http://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, http://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 http://www.site1.com
    DocumentRoot “~/wamp/www/site1″

    After restarting the wamp server, firing http://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. Pingback: WAMPserver and Virtual Hosts? | Free Hosting Blog

  17. Thanks mate, lifesafer

  18. Michelle Rupisan says:

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

    Regards..

  19. nad says:

    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

  20. 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

  21. sparty says:

    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

  22. Abeon says:

    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!

  23. eric says:

    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.

  24. Pingback: Magento install on XP Pro – Ubuntu, WAMP or XAMPP ? « Gwilym GJ

  25. Mert CAN says:

    Thanks, i am using zendframework with virtualhost.

  26. Kevin says:

    Thanks!

  27. Melounek says:

    Thanks very much :)

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

  28. Gerard says:

    Thanks so much. Just what I was looking for

  29. Mark says:

    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.

  30. Ravi says:

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

  31. Thanks. It surely Helps me out.

  32. Pingback: Moving MIGHTYminnow.com to Wordpress

  33. Abeon says:

    Thanks for sharing this… I was tearing my hair out trying to get more than 1 site working with WAMP!

  34. 2pha says:

    Abeon
    there is no need to do this to have multiple sites run in wamp, just put them within different folders in your www folder.

  35. mike homme says:

    Quick and easy, as I knew it would be but, a goog search landed me here and this def saved me an hour or two. Thanks!

  36. Paul Martin says:

    If anyone is having problems with absolute URLS simply add an extra Virtual host for www. as well as the domain

    ServerName domain.com
    DocumentRoot “C:wamp\www\folder”

    ServerName http://www.domain.com
    DocumentRoot “C:wamp\www\folder”

    this may fix your problem of it not liking absolute paths when accessing from other computers.

  37. Maurice says:

    Hi there,

    I have made a full tutorial on my companies website blog. Here’s the link, hope it helps!

    http://www.kintek.com.au/web-design-blog/configuring-multiple-domains-within-wamp-for-local-development/

  38. chichilatte says:

    I found that using 127.0.0.1 works really well for your local computer, but if you want to have your site accessible to the outside world (e.g. to a client during development) you need to use *:80 in the virtualhost part of httpd.conf. For example…

    NameVirtualHost *:80

    ServerName your.websites.domain.name
    DocumentRoot “C:/path/to/your/sites/public/folder/”

    Not sure if you can use this method to have lots of public domains going to different folders on your harddrive (it’s definitely possible somehow).

    You can still add the NameVirtualHost for 127.0.0.1 (as mentioned in the article) underneath that, so you can have lots of multiple sites working just locally as well as one public site.

  39. docfish says:

    I think from 2008 these settings became out of date with new version of Apache.

  40. @docfish Nope, I just referenced this article this week to set up a new OS X + Windows workstation :)

  41. Pingback: Moving MIGHTYminnow.com to Wordpress | MIGHTYminnow

  42. Joe says:

    I’ve spent a couple of days trying to create an alias that points to a directory on a different server. All I’m getting is a 403 error. However, everything works as long as the alias is on the same server. I’m using WAMP on W2k8.

  43. Rafael Collado says:

    Despues de configurar el virtual host en WampServer2 y enciendo para publicar, en la red en la barra de direcciones se repite la direccion ejemplo:
    http://192.168.0.1/miweb/http://192.168.0.1/miweb/index.php
    y no se porque.
    eso me ha pasado con todos los wampServer2.*

    Google Translate:
    After configuring the virtual host and switch WampServer2 to publish on the web at the address bar the address is repeated eg
    http://192.168.0.1/miweb/http://192.168.0.1/miweb/index.php
    and do not know why.
    That happened to me all wampServer2 .*

    • Jorge says:

      Rafa, no sé mucho de esto, pero lo que presentas parece mucho a que tienes alguna regla en el archivo .htaccess.

      //

      I’m not an expert onthis, but that looks to me that you have some type of rewrite rule in the .htaccess file or the conf file.

  44. adam says:

    Works like a dream. Thanks. Wish more support blogs were this easy to find and this useful.

  45. Pingback: Setting Up WordPress Locally with WAMP | Jayded Designs

  46. S.A.E says:

    Hi, thanx it was great.
    If you are looking for NameVirtualHost *:80 in WAMP, it is found here in a separated file for VHosts: wamp\bin\apache\Apache2.2.17\conf\extra\httpd-vhosts.conf
    I think here is the place we should define our virtual hosts.

  47. T.G says:

    This is what i am looking .Working fine.Thanks all.

  48. Donna says:

    Hi. I have wamp on a local disk E: but would like to share a usb external drive (F:). Is this possible?

  49. Pingback: WAMP Virtual Host not working | Hosting Fit

  50. Steven says:

    Hey Donna – did you find an answer to your question? I’m curious to see if you can have this setup with an external drive. I can’t imagine it being too difficult as soon as the external drive’s assigned disk is consistent.

    Please let me know, I’m thinking about this setup for some developers.

    Thanks,
    Steve

  51. Menake says:

    Many Many thanks

  52. Abdul says:

    This is the best tutorial out there, easy and it is working :)

  53. Pingback: I Love WAMP | Bryan Hadro

  54. Thanks everyone!

    Also, @Donna and @Steven – I just added an update with instructions for hosting your projects on an external drive on Windows. LMK if you have any other questions.

  55. Dan Ord says:

    Great guide, one thing to point out, I’m using Windows 7 and if anyone else is on the same OS you might have problems saving your hosts file. To remedy this, right click on notepad.exe or whatever text editor you are using and select “Run as administrator”. You’ll be able to save your changes this way.

    Also, since i was using Windows i had to change the path slightly from: DocumentRoot “C:\wamp\www\Clients\MyClient”

    To:

    DocumentRoot “C:/wamp/www/Clients/MyClient”

    This fixed the 403 permission denied error for me!

  56. Really helpful this morning. Just setting up a brand new machine and this saved me lots of time.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>