Professional and Affordable Web Design
0800 080 5401
Configure automatic subdomains with Apache
In this article I will show you how to configure your Apache server so you have subdomains redirecting (pointing) automatically to folders of subdomain name. Imagine running a development server, and having people working on an increasing number of different projects. Normally you would set up a Virtual Host configuration in Apache for every subdomain, and add an entry into DNS configuration for every subdomain.
In order for this configuration to be easy to test for the purpose of this article we will configure automatic subdomains for a localhost. Our goal is to be able to add a new subdomain only by adding a new folder in /var/www/localhost/, for example when we add directory /var/www/localhost/test all of its content should be accessible via http://test.localhost .
Also, we will be using Linux operating system, but this configuration can work on other UNIX and Windows boxes as well.
What software will I need?
We will need several software packages, firstly Apache2 ( although this has been tested on version 2.2.9, it should be also working on versions 1.x.x and older ). Our Apache server should have rewrite module enabled, check /etc/apache2/mods-enabled for symlink rewrite.load – if it is not there, you probably need to enable it ( use sudo a2enmod command for this ). Module alias should be enabled the same way as rewrite is.
We will be also needing a DNS server software – bind9 will do great. Of course you will be also needing some text editor to modify configuration files – feel free to use editor of you choice, as long as it is vim, of course :-).
Having DNS server running at your local machine is a good idea not only when developing websites. DNS servers have caching, so basically if there are some websites that you are visiting often – they should load faster with local DNS server. First of all we need our system to use our own DNS server, so edit /etc/resolv.conf file to have following contents:
you can do it by running this in your shell:
echo “nameserver 127.0.0.1” > /etc/reslv.conf
Make sure that NetworkManager is not overwriting this file, and if so – setup NetworkManager to use 127.0.0.1 as your domain name server.
Now it's time to install bind9 server, if you are running debian/ubuntu based system run:
sudo apt-get install bind9
Once installed we need to configure it, all of bind9 configuration files can be found in /etc/bind directory. For now we will be interested in /etc/bind/db.local file. At the end of this file add a new line:
* IN A 127.0.0.1
So the file looks like this:
Now restart your bind server with:sudo /etc/init.d/bind9 restart
Once your DNS server is restarted you should be able to access it through whatever.localhost. Test it with host whatever.localhost and if everything is working right you should get this:
~$ host whatever.localhost
whatever.localhost has address 127.0.0.1
Now no matter what subdomain name you enter for localhost it will always redirect to 127.0.0.1. This will be all that we need from DNS server, now it's time to configure your http server.
All we have to do with our Apache configuration is to update default vhost configuration, so we will be editing /etc/apache2/sites-available/default file. File probably now looks like this:
First we will have to add ServerName and ServerAlias options:
ServerAlias localhost *.localhost
You can put those directly under <VirtualHost localhost> . Now we will write rewrite rules so subdomains will be pointing to folders (in directory /var/www/localhost/) of name of subdomain.
We should also change the document root for our localhost vhost to /var/www/localhost. Let's now put everything together, and we will get our /etc/apache2/sites-available/default file.:
Now you need to create your server root directory:
sudo mkdir /var/www/localhost
It is time now to restart your Apache server ( sudo /etc/init.d/apache2 restart )
That is all, you can now test your configuration by creating a directory in /var/www/localhost/ for example “testsubdomain” and visiting http://testsubdomain.localhost with your browser.
All comments are moderated for spam and will not be shown. All genuine comments wil be show, however the links will be based on a NO FOLLOW RULE. Repeat commenters adding value to the articles and discusions will have removed alowing Follow Rule to Work.
Make a Comment
See Full Portfolio Some Of Our Work
Developed as an experiment to launch a new brand into the industry, cheap-shiphire-manchester.co.uk was commissioned by PDQ skip hire (The...
Mutiny Design was approached by the owner of photographers.co.uk to deliver a new and professional design to a well established and VERY...
Customer News & Resources
At Mutiny Design we are constantly gathering together articles and help guides to assist our clients.Introduction to sitemap.xml
Checking for a sitemap A site map (or sitemap) is a list of pages of a web site accessible to crawlers or users. It can be either a document in any form used as a planning tool for web design, or a web page that lists the pages on a web site, Some developers feel that site index is a more appropriately used term to relay page function, web visitors are used to seeing each term and generally associate both as one and the same. However, a site index is often an A-Z index that provides access to content, while a site map provides a general... Read More »Center a web page in CSS
One simple way to center a web page using CSS is to create a container div, that is horizontally centered by having its left and right margins set to auto. Using this method, you can still apply colours / background images to the body tag, so its a flexible solution. (if you didnt need this ability, just apply margins and width to the body tag instead, and forget using the container div). The container div has the same width as your webpage and, well, contains it. All the code for your web page is placed inside the container div. This will... Read More »