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 127.0.0.1 localhost 127.0.1.1 enlightened 127.0.0.1 a.localhost 127.0.0.1 b.localhost 127.0.0.1 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 (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.029 ms 64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.029 ms 64 bytes from localhost (127.0.0.1): icmp_req=3 ttl=64 time=0.028 ms ^C
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 </Directory> </VirtualHost>
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 127.0.1.1 for ServerName VirtualHost configuration: wildcard NameVirtualHosts and _default_ servers: *:80 is a NameVirtualHost default server 127.0.1.1 (/etc/apache2/sites-enabled/000-default:1) port 80 namevhost 127.0.1.1 (/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.