Virtual Hosting Refresher

The Debian installation of Apache2 comes all set up do “name-based” virtual hosting right out of the box. This is perfect if you have more than one domain to host on your server, or plan to in the future.

Name-based virtual hosting is when you have multiple domains running from one IP address, and is what we will be using here. This means that the DNS A records (or CNAME records) for and can both point to the one IP address of your server, and Apache will sort out which site to serve up based on the domain that was requested. If someone points their browser at the IP address of your server, e.g. http://123.456.123.45, they will be served the first site that you have configured. Unless you change it, this will be the Apache default site as it is installed by Debian’s Apache2 package.

For more info on virtual hosting, read the Apache docs.

Getting Familiar With Apache

Setting up a name-based virtual site is easy once you understand the layout a bit. I suggest you scan the file /etc/apache2/README. It’s pretty short and clear. For the impatient, here are just the things we are going to touch to get one name-based virtual site up. I’ll use “” as the virtual domain in all the examples.

This directory is the document root for the web server. This is where your web sites go, this is not mandatory, and is simply good practice.
A directory you create to contain all files related to the site This directory is not publicly accessible.
A directory you create to contain the publicly accessible files for
An html file you create to test your configuration.
This file you create containing the Apache configuration directives to define the virtual domain Each file in the sites-available directory defines a different virtual domain.
This is a symlink you create with the handy little command a2ensite The presence of a symlink in the sites-enabled directory has the effect of “enabling” a site defined in the sites-available directory.

Setting Up a Web Site

First, create a subdirectory of /var/www/ to contain the site. It makes sense to name this directory after the domain that will live there.

$> mkdir /var/www/

Now create the subdirectory /var/www/ This will be the only directory that is publicly accessible.

$> mkdir /var/www/

Now create /var/www/, something simple like the following: test index page

Hello World!

Now create /etc/apache2/sites-available/ We use the directive to define the virtual site, and the directive to define the …xxx/docs/ directory as publicly accessible (Allow from all). Like so:

#Basic setup
ServerAdmin [email protected]
DocumentRoot /var/www/

Order Deny,Allow
Allow from all
# Don't show indexes for directories
Options -Indexes

Now, to enable the site you use the a2ensite command (Note:use a2dissite to disable). The following creates a symlink /etc/apache2/sites-enabled/, which enables the site.

$> a2ensite

You need to reload Apache to see the site…

$> /etc/init.d/apache2 reload

If DNS is working, you should now be able to point your browser at see the index.html page you created above.