How to create virtual hosts in Windows (wamp) web server?
Intro
Creating virtual hosts is the better way to simulate the hosting server in your own development server. Simply your localhost folders will act as domains where you can easily test several sites in the local server as http://mysite.local or http://mytest.mysite. To create this kind of virtual hosts or virtual domains in your Apache server on Windows using WAMP server is just a matter of editing few files. Find the walk-through below.

Step 1 – Setting up the ‘host’ file
- Find the ‘host’ file in ‘C:\WINDOWS\system32\drivers\etc’ folder (or where you installed windows)
- Open it with Notepade or any text editor.
- You will see following lines
- Add the desired domain names in the end of the text (after the default localhost settings indicated above) it can be anything with or without extension (see the examples below)
# Copyright (c) 1993-2009 Microsoft Corp. # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # For example: # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host # localhost name resolution is handled within DNS itself. 127.0.0.1 localhost ::1 localhost
127.0.0.1 mydomain.local 127.0.0.1 www.mydomain 127.0.0.1 mywebsite 127.0.0.1 subdomain.mydomain.local
Step 1 is done.
Step 2 – Configuring the Apache ‘httpd.conf’ and ‘httpd-vhosts.conf’ files
- First we’ll enable a configuration file located in the WAMP server Apache folder. For this, open up the ‘httpd.conf’ file from the the “ c:\wamp\bin\apache\apahce2.2.11\conf” folder.
- Find the below lines and delete the # key in front of the second line to un-comment and enable it.
- Now we’ll find the ‘httpd-vhosts.conf’ file located in the Apache server. Usually we can find it in “c:\wamp\bin\apache\apahce2.2.11\conf\extra\” in the WAMP server (replace the apache version number after the \apache\ folder)
# Virtual hosts Include conf/extra/httpd-vhosts.conf
This will include the particular configuration file where we’ll setup the virtual hosts and their folders.
Add these lines in that file to enable the virtual hosts (which we created in step 1)
NameVirtualHost 127.0.0.1 # This line will make virtual host based on names not IPs <VirtualHost 127.0.0.1> DocumentRoot "c:/wamp/www” #this is default root for websites in WAMP ServerName localhost #this is default localhost domain </VirtualHost>
Now again we’ll add another block of virtualhost which will point out custom domain this time.
<VirtualHost 127.0.0.1> DocumentRoot "c:/wamp/www/mydomain.local" #We’ll create a folder inside /www/ folder for our domain files. #No restrictions in the folder name but we’ll keep it same to make it #easily identifiable. ServerName mydomain.local #this is our custom domain we added in the first step </VirtualHost>
So, the final look of the file will be like this:
NameVirtualHost 127.0.0.1 <VirtualHost 127.0.0.1> DocumentRoot "c:/wamp/www” ServerName localhost </VirtualHost> <VirtualHost 127.0.0.1> DocumentRoot "c:/wamp/www/mydomain.local" ServerName mydomain.local </VirtualHost>
We can add virtual hosts as much as we needed using the same block. Once everything is added in the ‘host’ file as in step 1 and in the apache config files as in the step 2, we’ll have to restart the WAMP server to take effect new changes we’ve done. Click the WAMP server icon in the system notification area and chose ‘Restart All services’.
Thats it. Now you can access the domain by typing ‘http://mydomain.local’ in the address bar of your favorite browser.
Just landed on this place via Google research. I love it. This situation change my percept and I am taking the RSS feeds. Cheers.
@Chauncey thank for your input. I’m just posting here what I’m practicing. I’m glad that you learned something from it.