How to Set Up Virtual Hosts on your Local Host
In this screencast I walk you through how to set up virtual hosts on your local development server, specifically Wampserver on Windows.
As promised in the screencast I am appending a written version of the process on this post for your reference.
Setting Up Virtual Hosts on Wampserver in Windows
First create a new folder for your website under wamp/www and then create a test index.php file in there. Navigate to it in your browser to make sure that it works. Now we want to set things up so that we don’t have to type http://localhost/mylocalsite every time we want to view our site. Instead we can just type http://mylocalsite and it works.
There are two steps to this, the first is on the Windows end of things and the other is on the webserver end of things. On the Windows end we need to edit the hosts file to include our virtual domains. To do this, navigate (in Windows Vista) to Windows->System32->drivers->etc, and open the hosts file for editing. In Vista, and perhaps also in Windows 7 (I haven’t tried it on Windows 7 yet) you have to first give the current user the permissions to edit this file or you’ll be going around in circles trying to save your changes. Make sure you’re logged in as admin. Right click on the file and choose Properties. Then go to the security tab and edit the security settings to allow the current user permissions to edit the file.
OK, now that that’s done, we want to go ahead and add a line to the hosts file assigning the localhost IP to our virtual domain name, something like this:
You can add as many domains as you want here using the same syntax.
This can really be anything you want it to be. Best choice is to not add .com or any extension that exists in the real web world out there because you may not be able to access that outside website when you need to.
Next step is to now work on the webserver side by editing the httpd.conf file in Apache. To do this, click on the wampserver icon, then Apache, and then click on the httpd.conf file which should open in Notepad. You can also navigate to this file directly by accessing wamp->bin->Apache->Apache2.2.11->conf. The Apache2.2.11 may be named differently for you depending on the version of Apache.
In some tutorials you’ll see that they tell you to add some code to the end of this httpd.conf file, but ideally you don’t want to do that. There is a neater better way to do this by using an include file that’s already present. First find the line that refers to the vhosts include file. Fastest way is to search for for the text “vhosts” in the httpd.conf file. Find the lines:
# Virtual hosts
Uncomment the second line by removing the # sign so it reads:
# Virtual hosts
The next thing to do is to open the httpd-vhosts.conf so we can edit it. This will be in wamp->bin->apache->Apache2.2.11->conf->extra. Open the httpd-vhosts.conf file in your favorite code editor, or even in notepad. You will see some sample code there for configuring your virtual hosts. Edit that sample code (the one in the <VirtualHosts> brackets to your configuration. In my case, since I want to point to my drupal6 domain as I referenced it in hosts, I will edit the code to read:
CustomLog "logs/localhost-access.log" common
CustomLog "logs/mylocalsite-access.log" common
Save the file and close it, and then make sure to restart Apache.
You should now be able to navigate to your local site directly by typing http://mylocalsite in your browser. In my case I navigate to http://mylocalsite and it should work.
I hope this helps you out when developing your own sites locally.