How to Create sub domains on localhost in apache on Ubuntu

Sub domains on localhost

When working with apache locally, the common way to use it is though the "http://localhost" url. When doing web development or some kind of testing, you might need to create sub domains on localhost, just like on online servers.

This post shows you how to create sub domains inside Apache on localhost. Its easy and takes only a few minutes.

1. Create sub domain hosts

First edit the /etc/hosts file to add the subdomains you wish to create.

$ cat /etc/hosts       localhost       enlightened       a.localhost       b.localhost       c.localhost

In the above example I have added 3 sub domains, namely a.localhost b.localhost and c.localhost

Now test the sub domains by pinging them, to make sure they are accessible

$ ping a.localhost
PING a.localhost ( 56(84) bytes of data.
64 bytes from localhost ( icmp_req=1 ttl=64 time=0.029 ms
64 bytes from localhost ( icmp_req=2 ttl=64 time=0.029 ms
64 bytes from localhost ( icmp_req=3 ttl=64 time=0.028 ms

As we can see above, the sub-domains are responding to our ping queries, only after we have added them to the hosts file.

2. Create your sub-domain document root directories

The next thing to do is decide, where to keep the sub domain files. This can be any directory of your choice, but in this example we are using the /var/www/ directory, which is used by apache.


3. Now create virtual hosts in Apache configuration file

The last thing to do is create the necessary apache virtualhosts for each of the sub-domain. Basically we are telling apache about the subdomain it needs to serve along with various details like document root for each of them.

In this example, we are editing the main 000-default configuration file. Alternatively you can create separate configuration files in sites-available directory and then symlink them to the sites-enabled directory, or using the a2ensite command.

$ sudo nano /etc/apache2/sites-enabled/000-default

Add a virtualhost section at the bottom like this

# Sub domain a.localhost
<VirtualHost *:80>
	DocumentRoot /var/www/subdomains/a/
	ServerName a.localhost

	<Directory /var/www/subdomains/a/>
		Options Indexes FollowSymLinks MultiViews +Includes
		AllowOverride FileInfo Options
		Order allow,deny
		allow from all

Now restart apache

$ sudo service apache2 restart

4. Test it

Now open the urls for the subdomains in the browser, http://a.localhost/ or similar and see if the index files are being shown up or not.

The apachectl command can also be used to check the details of the subdomains. Use the "-S" option and check the output.

$ sudo apachectl -S
[sudo] password for enlightened: 
apache2: Could not reliably determine the server's fully qualified domain name, using for ServerName
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost a.localhost (/etc/apache2/sites-enabled/000-default:42)
Syntax OK

Last Updated On : 6th May 2016

Subscribe to get updates delivered to your inbox

1 Comment + Add Comment

  • If one has very few projects, the method showed in this article is very good. But if you are developer and keeps working on different projects, very often this process become cumbersome. Instead I would suggest using VirtualDocumentRoot in apache. A very good article is there on site point.

    Hope it helps someone.

Leave a comment to Tanmay