This is a very useful feature of Apache2. Imagine if you have multiple domain names and wish to reduce hosting cost by hosting all the web sites on a single server, now you can do it through virtual hosting.
Basically, this features enables multiple domains such as
www.example.com
www.example.net
www.example.org
to point to the same machine (i.e. IP) and yet their hosting directory are different on the machine.
Eg, you may store files related to www.example.com in /var/www/example.com , www.example.net in /var/www/example.net, etc...
This is to enable easy management of each site affecting the other sites that are also hosted on the same machine.
There are 4 steps to doing this
1) Edit /etc/host so that the DNS resolution is pointing to the correct machine.
Of course, in production, this means updating your domain name service to point to your server machine.
2) Create the directories for the sites (eg /var/www/example.com, etc) and copy the files to the respective directories.
3) Edit /etc/apache2/conf.d/virtual.conf (create if the does not exist)
The file should contain the following line
NameVirtualHost *
4) Individual host configuration files are stored at /etc/apache2/sites-available. Create your host configuration files in /etc/apache2/sites-available, then create a symbolic link to those files in the sites-enabled directory - this will cause them to be actually loaded/read.
The symbolic links can be created using the Debian utility command,
a2ensite and a2dissite to enable and disable the sites.
Example of content of the /etc/apache2/sites-available/www.example.com#
# Example.com (/etc/apache2/sites-available/www.example.com)
#
ServerAdmin webmaster@example.com
ServerName www.example.com
ServerAlias example.com
# Indexes + Directory Root.
DirectoryIndex index.html
DocumentRoot /home/www/www.example.com/htdocs/
# CGI Directory
ScriptAlias /cgi-bin/ /home/www/www.example.com/cgi-bin/
Options +ExecCGI
# Logfiles
ErrorLog /home/www/www.example.com/logs/error.log
CustomLog /home/www/www.example.com/logs/access.log combined
Lastly, run (sudo) a2ensite www.example.com
Restart Apache2 and test out the URL.
#ref: http://www.debian-administration.org/articles/412