Creating new virtual host in Windows

Step by step guide to create virtual host on Windows XP or Window7 (WAMP installed)

  • Navigate to: C:\Windows\System32\drivers\etc and open the file named ‘hosts’ in a text editor. You should see 1 or more lines that have an ip address, then the hostname. Eg:
    127.0.0.1      localhost

    Under this line add the following line. (‘mysite.localhost’ is the name of your new virtual host)

    127.0.0.1      mysite.localhost

    Save and close this (hosts) file.

  • Open up httpd.conf from the Wamp tray icon in a text editor. You can find httpd.conf the way as shown in the image below:
    How to find httpd.conf in WAMP
    How to find httpd.conf in WAMP

    Once httpd.conf is open scroll down until you find:

    #Include conf/extra/httpd-vhosts.conf

    And un comment this line by removing the ‘#’ from the beginning of line.

  • Open up: C:\wamp\bin\apache\apache2.2.11\conf\extra\httpd-vhosts.conf. (Replace ‘apache2.2.11’ with appropriate version of Apache server your wamp tool may has been using) In httpd-vhosts.conf you should find 2 demo virtual hosts in there (if this file had not been edited earlier) – dummy-host.localhost – and – dummy-host2.localhost. Now you have three options. 1). You can delete these two demo hosts and add your new virtual host under they existed. 2). Delete one of them and edit one of them to suit your needs. 3). Do nothing but add a new virtual host somewhere under ‘NameVirtualHost *:80’ line. See the example below how to add new virtual host:
    <VirtualHost *:80>
    ServerAdmin webmaster@mysite.localhost
    DocumentRoot "C:\wamp\www\mysite_foldername"
    ServerName mysite.localhost
    ErrorLog "logs/mysite.localhost-error.log"
    CustomLog "logs/mysite.localhost-access.log" common
    </VirtualHost>

    A quick reference:
    ServerAdmin – Not important
    DocumentRoot – Full path to your site code directory
    ServerName – full access name of your virtual host (eg: http://mysite.localhost/)

    Restart Apache and it should work. If you face any problem after the restart, check the Apache error log. You may also need to restart your PC for Windows to pickup the changes you made to the Hosts file.

Important Note:
When you enable virtual hosting, Apache disables the main server root, so the first definition needs to reproduce the original server root. You then add each new virtual host within a pair of tags, using the location of the site’s web files as the value for DocumentRoot, and the name of the virtual host for ServerName. Again, use forward slashes, and if the path contains any spaces, enclose the whole path in quotes. If your server root is located, like mine, at C:\wamp\www add the following lines of code just before other virtual host code you just added so they look like this:

<VirtualHost *:80>
  DocumentRoot c:/wamp/www
  ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@mysite.localhost
DocumentRoot "C:\wamp\www\mysite_foldername"
ServerName mysite.localhost
ErrorLog "logs/mysite.localhost-error.log"
CustomLog "logs/mysite.localhost-access.log" common
</VirtualHost>

 

Leave a Reply