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.

Leave Comment


Nickname (required)

E-Mail (required)

Website



Highest Rated



5/5   -  
Wood Texture
5/5   -  
Sphere 1
5/5   -  
Photo Coloring
© M.Aldworth of Infinite Designs
Any other work is copyrighted to their respected owners if not specified other wise.