How To Setup Multiple Virtual Hosts in WAMP
September 19th, 2008

I have decided to start playing around with the Zend Framework. I have a web host (of course) however sometimes when developing I find it is easier and faster to use a local web server, with all the bells and whistles. WAMP, LAMP (this link is ubuntu specific, but any linux distribution should have easy HowTo guide for installing the LAMP software stack), MAMP, all provide the basic environment for beginning web development, and some have nice little GUIs to help you configure and maintain your local web server.

So for this tutorial I am going to show you how to add multiple virtual hosts to a WAMP installation. I am using the latest version from their website at the time this article was written. I installed WAMP, and left all defaults when going through the install wizard. I then launched WAMP and a nice little “odometer” icon appears in the System Tray, and cycles through 3 phases. As is starts the services (MySQL, and Apache) it cycles through the phases, and when you only see white (with some black) then all services started successfully. The little Lock in the middle of the icon means that the webhost is in private mode, so if your on an internal network, this means only you can view the website, and not somebody else. But if you want another user to see your webpage (perhaps a co-worker on another computer), you can then simply Left Click the icon and choose “Put Online”. Wamp Menu

So now that WAMP is installed you can browse to http://localhost and look at your WAMP dashboard.
WAMP Dashboard

This is great, and we can choose the option to put all of our projects or development sandboxes in the “Alias” part of apache, but then each address would be http://localhost/MYALIAS which is bland. We want to make our own local URLs link http://MyAlias, so we can host multiple projects locally each with unique domain names. So we have to get apache to do so. Follow the instructions below to accomplish this.

First lets edit the windows hosts file located in C:\Windows\system32\drivers\etc\hosts. Open up the file in your favourite text editor and add a line like 127.0.0.1 your-local-domainYou can put any name in place of your-local-domain, just be sure to change it everywhere it is mentioned in this guide. Your file should look something like mine does in the picture below.
Hosts File

Now you can save that and close your text editor. Next we want to edit our httpd.conf file and httpd-vhosts.conf file. So first browse to C:\wamp\bin\apache\apache2.2.8\conf and open up httpd.conf in your favorite text editor. Then scroll down to the bottom. Look for this line Include conf/extra/httpd-vhosts.conf and uncomment it. Your file should now look like the picture below. Save it and close your text editor.
httpd.conf file

Now guess what file we are going to edit next? The include file we just uncommented of course. So now browse just one more folder down the tree to C:\wamp\bin\apache\apache2.2.8\conf\extras and open the in your favourite text editor once again. Now scroll to the bottom and add this code
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "c:/wamp/www"
    ServerName localhost
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot "c:/Users/marf/Projects/your local folder/html"
    ServerName your-local-domain
    ErrorLog "logs/your_own-error.log"
    CustomLog "logs/your_own-access.log" common
    <directory "c:/Users/marf/Projects/your local folder/html">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
    </directory>
</VirtualHost>

Now let me explain. The first module is needed to preserve the http://localhost. This way we always have our dashboard. The second (and potentially more) are for each subdomain you choose. I also added the to the second virtual hosts because this allows my folder to be located in C:\User…. If you completely removed the branches, then you would get a 403 forbidden error, stating apache doesn’t have permission to execute inside that folder. HOWEVER, if you chose your DocumentRoot to be in lets say C:\wamp\www\your-local-domain, then you could omit the whole <directory>. It’s up to you.
httpd-vhosts.conf file

Save the file and close your text editor. Now all that’s left is to restart apache, which can simply be done by left clicking the little wamp odometer, and clicking “restart all services”. Done, now you can go to http://your-local-domain and you will probably get a 404 page not found error, but throw an index.html in the folder and it should work like a charm.

Enjoy!



mike
October 15th, 2008 at 9:59 am

Just an FYI– I fought this for a while and then realized that my /’s should be a \ because i was in windows… all is well now

paul
October 29th, 2008 at 6:31 pm

Commenting on your FYI, I did this in windows as well and had no problem leaving the /’s as /’s.

Sven
November 27th, 2008 at 9:04 am

I also use virtual host, but despite the “Deny from all” the virtual host is all open to every one.
Seems that apache isn’t taking that rule?

Marf
November 27th, 2008 at 9:18 am

Curious, does your little WAMP system tray icon have the little Lock on it or not? If it’s not there, then I believe it is in “online” mode, and so anybody can view your web pages.

Sven
November 27th, 2008 at 9:36 am

Marf: It is offline (wamp only add/removes the Allow from line in the http.conf)

Oops…. just copying the VH to this box and suddenly I see it…. Directory directive was not the same as the DocumentRoot.
Solved ;)

lovina
December 5th, 2008 at 6:20 pm

I am able to share my local site with my co worker but only one person at the time then I see your “howto” now either I got fobidden or no page display. Seriously if it is in Windows should be “\s” instead “/s” also when you state document like this

DocumentRoot “c:/Users/marf/Projects/sandbox/html”

How do I approach this?
In your folder sanbox do you anything.html? can I say .htm?
Please help. I need to be done by Monday.

Sopheak
December 10th, 2008 at 11:35 pm

Excellent! Thanks Big big!

Olivier
January 21st, 2009 at 8:21 am

Thanks for the great tutorial, it is very clear and easy to follow with the screen shots!

I tried it on Vista and when I put in subdomain.localhost, it kept bringing up the same page as just localhost, it wasn’t accessing my subdomain site. I had to change the second VirtualHost a little to put in your-local-domain:

ServerAdmin webmaster@localhost
DocumentRoot “c:/Users/marf/Projects/your local folder/html”
ServerName your-local-domain

I restarted the Wamp services and now it works :)

Marf
January 21st, 2009 at 9:00 am

Olivier, if you could be more specific as to where you added that, because I don’t see how that is different than what I have above.
Thanks

AaronK
January 30th, 2009 at 2:23 pm

Thanks for the article. I was having trouble accessing the dashboard after adding my local domain. Going to localhost would load my localdomain site. Making sure the virtual host entry for localhost came before my localdomain took care of the issue.

Luke Harris
February 10th, 2009 at 4:41 pm

Hey, This is the best tutorial on WAMP virtual hosts on the web. I should know, I tried and failed with several others. Perhaps it was because the other tutorials were for an earlier version of WAMP and I have WAMP 2.0 installed?

Thank you so much for taking the trouble to put such a well explained and illustrated tutorial online. I am now hosting multiple local sites.

Well done.

dimo
March 21st, 2009 at 12:46 am

Thanks! I reviewed four or five other sites before finding yours buried deep in Google results. I’ll share this with others.

Zeb
April 10th, 2009 at 4:32 am

Thanks for this awesome tutorial!

I installed WAMP on my machine for the first time yesterday after years of working online. Following your tutorial was completely painless and I’ve now managed to set everything up so I can access all my sites on a separate hard drive where I’ve had them stored - awesome job!

Adam
April 23rd, 2009 at 7:56 pm

Thank you, this is exactly what I needed :)

Obhi
April 29th, 2009 at 11:58 pm

Thanks, it works for me. Thank you very much.

Matt Hill
June 3rd, 2009 at 5:21 pm

Wow, thank you so much for this post!

I just upgraded from WAMP5 to WampServer 2.0 and my old virtual hosts no longer worked. I only had DocumentRoot and ServerName entries under WAMP5 but this doesn’t translate to WampServer2 without throwing 403 Forbidden errors.

Your tutorial was the only one I found that mentioned the entries. I added those and now my local sites have come back.

I wish I’d found your tutorial first, I’ve just wasted the last two hours reading others that have sent me completely on the wrong path!

Thanks again for saving me from losing more hair :-)

tang0
June 21st, 2009 at 11:17 am

Thanks, thats what i was looking for developing symfony projects at same time.

Abeon
June 24th, 2009 at 7:21 pm

I seem to be having problems getting the www. to work as well as the tld. Any ideas?

My code seems identical to the code above :(

mike
July 22nd, 2009 at 9:31 am

This was exactly the solution I was looking for.. worked perfectly. Thanks for putting it together.

janamills
August 17th, 2009 at 6:44 pm

Thanks so much for this, struggled through at least 6 other tutorials before trying yours which worked first time! thanks! though its worth noting for anyone trying this on earlier versions of apache that not all the files and lines are present.

Mukta
September 14th, 2009 at 8:24 am

I wanted virtual server up and running at the ealiest and so googled and tried to implement reading the tutorials - only this worked in mins. Thanks a ton.

Kujoy
November 6th, 2009 at 2:35 pm

Hi,

Thanks for the great tutorial. I have tried a few different easy install apache php mysql along quite a few tutorials trying to get virtual hosts working with limit success. Sounds like you had similar experiences. Your tutorial and WAMP has made my life a lot easier, and its the first tutorial thats made virtual hosts work properly while not breaking the localhost and the WAMP homepage on the localhost.

Well done.

henkedo
November 24th, 2009 at 3:20 am

Hi Thanks for the tut. I had virtual hosts running on WAMP5 no problem. But setting this up doesn’t work for me. FYI i completely uninstalled old WAMP and installed the latest version. I’ve done the changes and restarted all services, http://localhost works fine, displays the WAMP dashboard, but other domains cant be found. any ideas?

ServerAdmin webmaster@localhost
DocumentRoot “c:/wamp/www/”
ServerName localhost
ErrorLog “logs\localhost-error.log”
CustomLog “logs\localhost-access.log” common

ServerAdmin webmaster@localhost
DocumentRoot “c:/wamp/www/dom/www/”
ServerName dom.localhost
ErrorLog “logs\dom-error.log”
CustomLog “logs\dom-access.log” common

Yoga
November 26th, 2009 at 12:06 pm

@henkedo same problem, here. Really pissed.

Leave Comment


Nickname (required)

E-Mail (required)

Website



© M.Aldworth of Infinite Designs
Any other work is copyrighted to their respective owners if not specified other wise.