How to Create a Subdomain on WAMP Server (Step-by-Step Guide)

As a web application developer first step towards developing any web application is to setup the environment for development on local machine. Which involves setting up virtual hosts for local application address. This post will walk you through the steps to setup WAMP Virtual Hosts in Windows.

How to Create a Subdomain on WAMP

In order to configure local subdomain on WAMP we must first enable virtual hosts and the virtual host alias module in WAMP apache. Learn in this post with simple steps how to create WAMP Virtual Hosts to setup a local subdomain.

  1. Add an entry to windows host file. This will look for domain on local machine.
  2. Enable Virtual Hosts and Virtual Hosts Alias Module in WAMP Apache.
  3. Add entries to Virtual Hosts file for our desired domains.
  4. Test the subdomain implementation.
 

What is a Subdomain/Virtual Host?

A subdomain in WAMP is a locally configured virtual host which can be accessed with unique URL instead of accessing the directories inside a project. This setup allows developers to run multiple projects by mapping custom addresses to different directories. Following are the steps to configure a WAMP subdomain on windows.

 

Step 1: Edit Hosts File to Create a Subdomain in WAMP

First add an entry for subdomain in hosts file. On windows machine this file can be found at the following location:

c:\windows\system32\drivers\etc\

This file has no extension just "hosts" file. So you have to open it using notepad or any editor of your choice. It also requires Administrator permissions so open it using "Run as Administrator". Add the following lines at the end of file:

127.0.0.1  main-domain.com
127.0.0.1  sub-domain.main-domain.com

Leave the first line as it is where it says "localhost" and add the content from above snippet to file. The first line from snippet above is for your main domain whereas second line is for your sub-domain. You can change the main-domain.com and sub-domain.main-domain.com to any domain of your choice, I am using it for this post as an example. What does actually this file do? 127.0.0.1 is self referenced loop back IP. Whenever this IP is accessed it will reference the local machine to resolve IP address to a hostname. That means the operating system will search for the given hostname on the same computer for server instead of looking it up in configured DNS.

 

Step 2: Enable Apache Virtual Hosts in WAMP

After adding entry to windows hosts file, it is time to enable virtual hosts and virtual host alias module in httpd.conf file. The file can be found at the following location:

C:\wamp\bin\apache\Apache2.2.11\conf\

To open httpd.conf, simply click on WAMP Tray icon, move your cursor over apache, and click the file httpd.conf in sub-menu.

After opening httpd.conf, search for the following lines and remove the # from the beginning of these lines, then save and close the file.

#LoadModule vhost_alias_module modules/mod_vhost_alias.so
#Include conf/extra/httpd-vhosts.conf

Now that we are done pointing our domain back to local machine, open the httpd-vhosts file at the following location:

C:\wamp\bin\apache\Apache2.2.11\conf\extra\

I have WAMP installed in C:\ directory.

 

Step 3: Configure Apache VirtualHost for WAMP Subdomain

After opening the file, add entries for virtual hosts with following lines for our main domain and sub-domain, then save and close the file. Its best to keep a backup of original file before making any changes.

<VirtualHost *:80>
    DocumentRoot "c:/wamp/www"
    ServerName main-domain.com
    ServerAlias www.main-domain.com
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/sub-domain"
    ServerName sub-domain.main-domain.com
    ServerAlias sub-domain.main-domain.com
</VirtualHost>

The DocumentRoot is the directory where files for given domain are stored in my case I have a directory sub-domain inside www root direcoty. The ServerName is the domain name or subdomain name. The ServerAlias is an alternate domain which is usually www we did not use any for our subdomain in second entry above.


Step 4: Restart WAMP Server and Test Local Subdomain Configuration

After following the above steps we need to restart WAMP server, open a browser and enter main-domain.com and then sub-domain.main-domain.com to confirm our configuration. We should see the content of www as root of main-domain.com and content of www/sub-domain for our sub-domain.main-domain.com as per our configurations.