Setting up subdomains on your local web server

One of the primary tasks for a web developer is to set up a development environment that resembles the production environment as much as possible.

When I was a beginner in web development, I used to create a subfolder for each new project in my web server documents root and put all my project files in there; the local address of my projects would look something like this: localhost/myproject/index.php. This approach was very awkward and used to cause many problems during deployment into production environment.

Now, when I start developing a new web project, first thing I do is setting up a local hostname for my project; something like myproject.localhost which maps to a folder separated from other ones.

In this article, I want to show you how you can create hostnames on your local computer. I assume that you use Apache web server. Please note that these hostnames are not necessarily subdomains of localhost. I like to create them like myproject.localhost; you can create something like mywebsite.com.dev or even google.com.

For creating a local hostname, you should do these steps:

  1. Map you hostname to an IP on your local computer or local network. It usually is 127.0.0.1. You can set different IPs as well.
  2. Create a virtual host in you Apache webs server an tell it where does this hostname point to.

Mapping host name to an IP

In each operating system, there is a file that lists all pairs of hostname/IP addresses. In each OS it is located in a different path, but in all of them -as far as I know- it's name is hosts.
Path of the file in some operating systems are:

OS Path
Windows: NT, 2000, XP, Vista, 7 %SystemRoot%\system32\drivers\etc\hosts
Most Unix and Unix-likes /etc/hosts
Mac OS X /private/etc/hosts or /etc/hosts (/etc on Mac OS X is a symbolic link to /private/etc)

By default, hosts file contains few rows, each one looks like this:
127.0.0.1        hostname
By default, localhost is mapped to IP 127.0.0.1. You should add you own rows as well. Here is an example:

  1. 127.0.0.1        localhost
  2. 127.0.0.1        myproject.localhost
  3. 127.0.0.1        mywesite.com.dev

Add your own rows and save the file. Changes should take effect immediately. Otherwise, run one of these command in command line:

  • In Linux:
    /etc/rc.d/init.d/nscd restart
  • In Mac OS X Leopard and newer:
    dscacheutil -flushcache
  • In Mac OS X, before Leopard:
    lookupd -flushcache
  • In Windows:
    ipconfig /flushdns

Creating virtual hosts in Apache web server

Go to this folder: <Apache folder>/conf and open file https.cong in an editor. Find the following lines:

  1. # Virtual hosts
  2. #Include conf/extra/httpd-vhosts.conf

Uncomment the include directive:
  1. # Virtual hosts
  2. Include conf/extra/httpd-vhosts.conf

Save file and now go to this folder: <Apache folder>/conf/extra and open file httpd-vhosts.conf it in an editor.
For each hostname you added to the hosts file, you should define a virtual host here. Add following lines to the file:

  1. <VirtualHost *:80>
  2.     DocumentRoot "/path/to/virtual/host/directory"
  3.     ServerName myproject.localhost
  4.     DirectoryIndex index.php index.html index.html index.htm index.shtml
  5. </VirtualHost>

Here is full list of directives:

  1. <VirtualHost *:80>
  2.     ServerAdmin webmaster@dummy-host.example.com
  3.     DocumentRoot "/path/to/virtual/host/directory"
  4.     ServerName dummy-host.example.com
  5.     ServerAlias www.dummy-host.example.com
  6.     ErrorLog "logs/dummy-host.example.com-error_log"
  7.     CustomLog "logs/dummy-host.example.com-access_log" common
  8. </VirtualHost>

Save the changes and restart the Apache.

Note

If for any reason you decided to add your virtual hosts directly into httpd.conf file, you should add following directive to this file as well:

  1. NameVirtualHost *:80

This directive exists in httpd-vhosts.conf by default.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.