Friday, June 18, 2010

How to create virtual hosts in Windows (wamp) web server?

To create 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.

Prerequisites : Apache server for Windows

Step 1 – Setting up the ‘host’ file

1. Find the ‘host’ file in ‘C:\WINDOWS\system32\drivers\etc’ folder (or where you installed windows)
2. Open it with Notepade or any text editor.
3. You will see following lines

# 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

4. 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)

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

1. 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.
2. Find the below lines and delete the # key in front of the second line to un-comment and enable it.

# 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.
3. 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)

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

DocumentRoot "c:/wamp/www”
#this is default root for websites in WAMP
ServerName localhost
#this is default localhost domain


Now again we’ll add another block of virtualhost which will point out custom domain this time.


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


So, the final look of the file will be like this:

NameVirtualHost 127.0.0.1


DocumentRoot "c:/wamp/www”
ServerName localhost



DocumentRoot "c:/wamp/www/mydomain.local"
ServerName mydomain.local


We can add virtual hosts as many 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.

No comments:

Post a Comment