Configure Virtual Hosting in Apache Web Server

When we say or mention the word Web Server, the first thing that comes to our mind is Apache, right? (if you don’t then what are you doing here reading this? :-))

Apache is the most well known open source web server there is! Open source?? Make that the most wildly used webserver on the internet, period!

It is believed (and i know they made a survey or study about it) that Apache takes 90% of all web servers in the internet. Most web hosting company, and I’ve once worked in one, use Apache.

This post will talk about how to setup Apache for Virtual Hosting

Setting Up A Virtual Host in Apache

create a completely seperate document root, cgi-bin directory, and logfile directory for each host. You can place these beneath the standard Debian prefix of /var/www or you may use a completely different root

root@irony:~# mkdir /home/www

root@irony:~# mkdir /home/www/www.example.com
root@irony:~# mkdir /home/www/www.example.com/htdocs
root@irony:~# mkdir /home/www/www.example.com/cgi-bin
root@irony:~# mkdir /home/www/www.example.com/logs

root@irony:~# mkdir /home/www/www.example.net
root@irony:~# mkdir /home/www/www.example.net/htdocs
root@irony:~# mkdir /home/www/www.example.net/logs
root@irony:~# mkdir /home/www/www.example.net/cgi-bin

root@irony:~# mkdir /home/www/www.example.org
root@irony:~# mkdir /home/www/www.example.org/htdocs
root@irony:~# mkdir /home/www/www.example.org/logs

root@irony:~# mkdir /home/www/www.example.org/cgi-bin

NEt
Here we’ve setup three different directory trees, one for each site. If you wanted to have identical content it might make sense to only create one, and then use symbolic links instead.

The next thing to do is to enable virtual hosts in your Apache configuration. The simplest way to do this is to create a file called /etc/apache2/conf.d/virtual.conf and include the following content in it:

#
#  We're running multiple virtual hosts.
#
Nameserver *

NameVirtualHost *

(When Apache starts up it reads the contents of all files included in /etc/apache2/conf.d, and files you create here won’t get trashed on package upgrades.)

Once we’ve done this we can create the individual host configuration files. The Apache2 setup you’ll find on Debian GNU/Linux includes two directories for locating your site configuration files:

/etc/apache2/sites-available

This contains configuration files for sites which are available but not necessarily enabled.
/etc/apache2/sites-enabled

This directory contains site files which are enabled.

As with the conf.d directory each configuration file in the sites-enabled directory is loaded when the server starts – whilst the files in sites-available are completely ignored.

You are expected to 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.

Rather than actually messing around with symbolic links the Debian package includes two utility commands a2ensite and a2dissite which will do the necessary work for you as we will demonstrate shortly.

Lets start with a real example. Create /etc/apache2/sites-available/www.example.com with the following contents:

#
#  Example.com (/etc/apache2/sites-available/www.example.com)
#
<VirtualHost *>
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/
<Location /cgi-bin>
Options +ExecCGI
</Location>

# Logfiles
ErrorLog  /home/www/www.example.com/logs/error.log
CustomLog /home/www/www.example.com/logs/access.log combined

</VirtualHost>

ow we’ve got:

  • Three directories which can be used to contain our content.
  • Three directories which can be used to contain our logfiles.
  • Three directories which can be used to contain our dynamic CGI scripts.
  • Three configuration files which are being ignored by Apache.

To enable the sites simply run:

root@irony:~# a2ensite www.example.com
Site www.example.com installed; run /etc/init.d/apache2 reload to enable.

root@irony:~# a2ensite www.example.net
Site www.example.net installed; run /etc/init.d/apache2 reload to enable.

root@irony:~# a2ensite www.example.org
Site www.example.org installed; run /etc/init.d/apache2 reload to enable.

This will now create the symbolic links so that /etc/apache2/sites-enabled/www.example.org, etc, now exist and will be read.

Once we’ve finished our setup we can restart, or reload, the webserver as the output above instructed us to do with:

root@irony:~# /etc/init.d/apache2 reload
Reloading web server config...done.
root@irony:~#

Leave a Reply

Your email address will not be published. Required fields are marked *