Sunday, April 20, 2008

Virtual Hosting Using Apache on Ubuntu

Virtual Hosting allow web servers to host more than one website on a sing machine. This is how sharing hosting works. I become pretty handy as well while develloping different web project on the same machine and allows you to access to your local repository using addresses such as instead of http://localhost/~myuser/myproject/ :) .
This tutorial is based on a machine runnning ubuntu/linux but should be the same on any debian based distribution and almost the same on other distributions.

First of all, you need an apache server ready to run on your machine, if it is not yet install, open a terminal and type

$sudo apt-get install apache2-utils apache2-common

Once the server is installed, it is time to get into apache 2 configuration.
Let's open apache's main configuration file, name /etc/apache2/apache2.conf. A search for the word virtual bring us to the following line:

Include /etc/apache2/sites-enabled/[^.#]*

This mean that when starting apache, it will look for files in /etc/apache2/sites-enabled/.
Lets go there and see what is in.

$cd /etc/apache2/sites-enabled/

$ls -l

total 1

lrwxrwxrwx 1 root root 36 2005-12-27 01:42 000-default -> /etc/apache2/sites-available/default

Well, this only links to the file in directory /etc/apache2/sites-available/ . You might wonder what is the point in doing such. Well, this simply allows you, mainly when you are using your box as a web server, to:

  1. Have a simple main configuration file
  2. Do be able to edit or create a new host by creating/editing a file from /etc/apache2/sites-available/
  3. In case your web server doesn't restart because of misconfiguration, you can simply remove the link from the file in /etc/apache2/sites-enabled/ pointing to the malformed file in /etc/apache2/sites-available/

Now let say you want to be able to map the domain name to you local machine, using the code file in /home/myuser/public_html/
While in /etc/apache2/sites-available, create a new file (let say

$sudo vi

Now add the following lines:

ServerAdmin webmaster@localhost
#We want to be able to access the web site using or
DocumentRoot /home/myuser/public_html/
#if using awstats
ScriptAlias /awstats/ /usr/lib/cgi-bin/
#we want specific log file for this server
CustomLog /var/log/apache2/ combined

Note:People who don't want to bother knowing how the site enabling system works might just jump to the end of the article to find debian built-in command syntax. If you want to know how it works, or do not use a debian based distro, carry on.

Now, we specified a new host to apache but it is not yet linked to the repertory where apache actually look for virtual hosts. Let go to:

$cd /etc/apache2/sites-enabled/

and create a link to the file we just created:

$sudo ln -s /etc/apache2/sites-available/

Now apache is almost ready to restart, but before doing so, we must inform our linux system that and are not to be looked for on the net, but on the local machine instead.
To do so, simply edit /etc/hosts and add the new host names at the end of the line beginning by, which is localhost.
In the end, your file should look like: localhost.localdomain localhost

And now we are done, simply reload apache:

sudo /etc/init.d/apache2 reload

Open your web browser and enter the following address Magic, it runs the same as when you were using http://localhost/~myuser/ but it is far more usefull when devellopping a web service and want to be able to develop applications on your machine just like it is where the real web site.

Edit: As you can see from the comments, many people pointed out that you can use a debian specific command (so if you are not using a debian based system, don't expect to find that command :) ).
to enable a new virtual host simply type:

sudo a2ensite mysiteavailable-site

to disable a virtual host:

sudo a2dissite mysiteavailable-site

where mysiteavailable-site is the name of the virtual hos you want to enable/disable, so in out example:

Hope this helped.

No comments: