Adding Virtual Hosts to Apache & Wampserver October 16, 2010Posted by Tournas Dimitrios in Uncategorized.
Being able to develop on my local machine with urls like http://testsite/index.php has been a godsend. Mostly because I am able to take full advantage of the document root. If I were to develop on something like http://localhost/testsite/index.php I would be extremely limited and forced to used relative urls. In this article I’ve listed the steps in order to enable this functionality in a wamp server environment.
This is my method for enabling Virtual Hosts on my wampserver on my own local machine but I don’t see why it wouldn’t work for any old Apache installation. If you have trouble with it, leave me a message in the comments below and I’ll be happy to help.
Step 1: Open your hosts file.
- Navigate to your hosts file normally located in \WINDOWS\system32\drivers\etc\hosts
- Open the file named hosts (no extension) with notepad.
- Add this line to the bottom:
This tells your computer that any url with a value of ‘testsite’ should be routed to 127.0.0.1 or more commonly refered to as localhost aka your own machine.
- Next, open httpd.conf located in \apache\conf\httpd.conf
If you installed wamp server this is located at \wamp\bin\apache\apache2.2.6\conf
- At the very bottom add these lines:
. NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot "c:/wamp/www/myfirstsite/" ServerName testsite </VirtualHost> .
Restart apache and you should be able to type http://testsite/ into any browser (may need to restart that too) and it will show you the DocumentRoot you just set.
The first line, NameVirtualHost *:80 tells Apache to look out for virtual hosts using its own naming convention. The next couple lines set up a virtual host. You can use as many of these as you want and set up as many sites as you may be developing at one time. Personally, I find this method to be invaluable.. I hope it helps someone else out there.