Sunday, September 3, 2017

How to Create a Sub-domain on WAMP

Virtuals Hosts are handy in making your own domain or subdomains for testing and learning. This tutorial will take you through steps to create subdomain/V-Host on Windows using WAMP.

How to Create a Sub-domain on WAMP

Add an entry in hosts file

Open the hosts file and add an entry for your subdomain the file can be found in the following location on windows7:
c:\windows\system32\drivers\etc\
This file has no extension just "hosts" file. So you have to open it using notepad and it also requires Administrator persmissions so open it using "Run as Administrator". Add the following lines at the end of file:
    127.0.0.1       maindomain.com
    127.0.0.1       subdomain.maindomain.com

Leave the first line as it is where it says "localhost" and the second line is for your main domain and third line is for your subdomain. You can set the maindomain.com and subdomain.maindomain.com to anything you want I am using it for this tutorial. What actually this file does? 127.0.0.1 is self refrenced loop back ip. Whenever this ip is accessed it will reference the computer it is accessed from. That means the operating system will search for the given hostname on the same computer for server instead of looking it up in DNS.

Open the VirtualHosts file

Now that you have done pointing your domain back to your computer open the "httpd-vhosts" file on the following location for WAMP:
C:\wamp\bin\apache\Apache2.2.11\conf\extra\
I have WAMP installed in c:\

Add an entry for VirtualHost (Sub-domain)

After opening the file add the following lines for your main domain and sub-domain save the file and exit. Back up the file before you change it.
<VirtualHost *:80>
    DocumentRoot "c:/wamp/www"
    ServerName maindomain.com
    ServerAlias www.maindomain.com
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "c:/wamp/www/roundcube"
    ServerName subdomain.maindomain.com
    ServerAlias www.subdomain.maindomain.com
</VirtualHost>
DocumentRoot points the directory where files for given domain are stored in my case I have a directory "subdomain" inside of root direcoty "www".

Enable VirtualHosts

Now that you have followed all the instructions properly its time to enable your VirtualHosts in httpd.conf file. The file can be found on the following location:
C:\wamp\bin\apache\Apache2.2.11\conf\
or
To open httpd.conf simply left click your WAMP Tray icon, move your cursor over apache, and choose the file httpd.conf in submenu.

After opening httpd.conf search for the following lines and remove the "#" from the beginning of lines then save and exit.
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
#Include conf/extra/httpd-vhosts.conf

All done now you have to restart WAMP server open your browser and enter maindomain.com and then subdomain.maindomain.com. It should work you can add as many subdomains as you want following these instructions.