How to Create sub domains on localhost in apache on Ubuntu

By | July 24, 2020

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

The hosts file on a linux system is used to map ip addresses to specific host names. We can easily create multiple sub domains in here and point them to the localhost ip address.

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


The following page from the Ubuntu wiki has more information on setting up localhost sub domains in apache.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].


How to Create sub domains on localhost in apache on Ubuntu
  1. Jules Colle

    I got this error on restarting apache service: `Syntax error: Either all Options must start with + or -, or no Option may.`

    Fixed it by modifying this line:

    Options +Indexes +FollowSymLinks +MultiViews +Includes

  2. John Dee

    Great tutorial! This post is on my bookmarks now. :) On most servers, the apache files have .conf extension.
    sudo nano /etc/apache2/sites-enabled/000-default.conf
    sudo nano /etc/apache2/sites-enabled/000-default

  3. jack king

    hello silver moon ! im am delighted to tell you that python version 3.3 will soon be removed and replaced by java scripts and HTML languages . thanks for your patience please enquire if you have any further quries about python or milk snakes . I strongly with the statement to a large exten ,,,, oops soz m8 I accidently typed my English homework wot I wus doing on word m8 dam them margrat thsather


    How can I work with sub domains using multi site network on localhost? I don’t have much experience with WordPress yet. I am on Ubuntu 14.04 LTS. Is there any step by step tutorial available for this? I created a topic at but no idea about how to add manually created sub domains in multisite network. @Tanmay: does this cover how to do it on local Ubuntu platform?

    Any suggestion would be greatly appreciated.


Leave a Reply

Your email address will not be published. Required fields are marked *