Address based virtual hosting

Reconfiguring the default virtual host

As we learned in the previous section if the server on which Apache has been installed has only a single IP address then the default configuration, which instructs the daemon to listen on all addresses and use all of those addresses for name-based virtual-hosts, can be left as is as there is no difference between name and address based hosting when only a single site is involved. If the server has more than a single IP address, and we must assume it has or this section of this guide is somewhat irrelevant to you, then it makes sense to only use one IP address for name-based virtual-hosting thus leaving the others free for use by address-based virtual-hosts and, more commonly, SSL secured sites.

The example below shows the changes required to the default virtual host configuration to disable name-based virtual hosts completely. If you have already changed this configuration file to use only a single address for name-based virtual-hosting, as suggested in the previous section, and you wish to continue to use name-based virtual hosting in addition to address-based virtual hosting then the following modifications are not needed as all other addresses are already free to be used by address-based virtual hosts.

/etc/apache2/vhosts.d/00_default_vhost.conf
Listen 80

NameVirtualHost *:80

<VirtualHost *:80>
<VirtualHost 10.0.1.80>

Configuring a new address-based virtual host

To create a new address-based virtual-host we simply need to create a single configuration file, an example of which is shown below, and place it in the /etc/apache2/vhosts.d directory.

/etc/apache2/vhosts.d/20_hacking2_vhost.conf
<VirtualHost 10.0.1.81>
ServerName www.another.hacking.co.uk

DocumentRoot "/var/www/hacking2/website"

<Directory "/var/www/hacking2/website">
Options Indexes

Order allow,deny
Allow from all
</Directory>
</VirtualHost>

As you can see the configuration required is fairly minimal. The first line specifies that this is a declaration of a virtual-host. It also specifies which IP address this virtual-host should be associated with.

The ServerName specifies the official name of the web site and should ideally correspond to the fully qualified host name of the site to be served. The remainder of the configuration consists of the DocumentRoot directive and a Directory block. As these are identical to those which were discussed in the previous sections we shall not go over them again here.

Once the configuration is complete the apache daemon can be signalled to reload its configuration files using the command shown below.

lisa /etc/init.d/apache2 reload