Other articles
- Center a web page in CSS »
- Basics of Database optimisation »
- How to configure Outlook Express to receive emails »
- How to configure Outlook 2007 to receive emails »
- HTML Base Tag »
- Scaleable HTML and CSS Guide, Part 1 - Introduction »
- Problems Encountered With PHP DOM Functions »
- Refactoring code with find, xargs and sed »
- What do webdesigners need from clients »
- Using any font on a website »
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 :-).
Configuring DNS
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:
nameserver 127.0.0.1
you can do it by running this in your shell:
sudo bash
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.
Configuring Apache
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
ServerName localhost
You can put those directly under
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.







Comments
Tim
19th June, 2009 at 4:55pm
Developing locally: Perhaps vi /etc/hosts add 127.0.0.1 mysite :wq Then open a new tab in FF enter mysite Then you don't need to run local DNS servers, also hosts is faster. Vhosts in Apache: create/copy existing vhost config cp: /etc/apache2/sites-available/site1.conf /etc/apache2/sites-available/site2.conf or use in conjunction with sed.