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!)






6 Comments, Comment or Ping
Tim
THANKYOU!!! this is just what i had been looking for! Cheers mate!
Jan 24th, 2008
reuben
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!
Jan 26th, 2008
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.
Jan 31st, 2008
dennisplucinik
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.
Jan 31st, 2008
Chris Charlton
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.
Apr 24th, 2008
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
Jun 21st, 2008
Reply to “Setting up multiple virtual hosts in WAMP”