Setting up Apache Virtual Hosts

By | July 30, 2006

When you are configuring a testing server, or just playing around with setting up a webserver, at one time you will arrive at the point where you want to create more “domainsâ€?. Here you can do two things, set up multiple servers, one for each sub-domain, or use the build in feature ‘Virtual Hosts’ in Apache.

What I will try and explain in this somewhat short tutorial is how to setup a virtual domain in Apache using the httpd.conf file. (Note that if you don’t know what this file does this tutorial is probably not meant for you!)

Basic requirements
Before you start reading this post please make sure you have the following things ready. If not you may not be able to successfully complete the setting up of virtual hosts.

  • Have a version of Apache installed
  • Are familiar with the httpd.conf (at least a little)
  • Have administrator rights in Windows or root access in Linux (in order to restart the Apache services)

If you have all the above then you are ready to set up some virtual hosts. If you are missing any then you will most likely fail, still feel free to read on just don’t expect miracle’s.

Setting up the basics
Now for this all to succeed you should have a domain name. If you don’t you can set up a free one at no-ip.com or an equivalent service. Why is this so important you may ask. Well simple, Apache uses this domain name and any sub-domains related. It translates them using the virtual hosts and calls up the proper website.

For example if you have the domain: play.no-ip.com
and somebody calls mail.play.no-ip.com, then Apache can see this and look in its virtual hosts to find a matching website.

Now that you know this it is time to set up our so called “mailâ€? sub-domain. Note that the content is irrelevant for setting it up. Pretend that every virtual host contains some type of meaningful website.

Every virtual host will look similar to the following code:

DocumentRoot "/usr/http_roots/mail"
ServerName mail.play.no-ip.com
ErrorLog logs/mail.play.no-ip.com-error_log
CustomLog logs/mail.playno-ip.com-access_log common

DirectoryIndex index.html index.htm index.php
So lets explain what it all means. I’ll do this step by step to not confuse you any further.

indicates to Apache that a new virtual host definition is starting, where it is listening on all network cards in the PC and to port 80. This is default for most websites.

The DocumentRoot is somewhat self explaining. It is the address to the website, meaning the fully qualified location on the disk. Eg for Windows this could be something like “C:/http_root/mailâ€?. For any application that originates from Linux it is best to use forward slashes rather then backslashes.

The errorlog and CustomLog both contain information as to where Apache is to store requests sent and failed. In theory you can place them anywhere.

The last and probably most important thing to keep track of is the DirectoryIndex. This allows you to set what files Apache should load if the root of the directory is called. For example:
mail.play.no-ip.com/
is translated by Apache into
mail.play.no-ip.com/index.html

Advanced options
Like with anything in Apache you can use somewhat more advanced options to tweak your virtual host. I will discus a few of them to give you a feel of what you can do with virtual hosts. For example expanding the root of the website a bit (don’t worry if you don’t know what it means I will come back on this somewhat later).

Setting options
On of the more useful settings is the Option tag, this allows you to enable or disable specific Apache options. I commonly use this for enabling file browsing (Indexes) or allowing multiple views (MultiViews).

Syntax: Options

Expanding the root
Another advanced option I love to use is expanding the website with other websites. For example you could mount the userhelp.play.no-ip.com website under mail.play.no-ip.com/help.

Off course the folder you mount does not need to be an existing part of the domain, it can quite literally be any folder you like (as long as Apache has access).

To do this you need only one command, namely AliasMatch. This allows you to translate any web-address into a physical address on the disk. In this post we will continue on the example already mentioned. So lets add the help directory.
AliasMatch ^/help(/.*)?$ “/usr/http_root/userhelpâ€?

As you can see the first part contains weird looking code, well that is because Apache supports the usage of regular expressions. I used those to map anything starting with “helpâ€? to the userhelp files on the disk.

In closings
I’ve introduced you with a lot of new and hopefully useful information. But keep in mind that every change you make in the httpd.conf is to be followed by restarting Apache. Otherwise the changes will not appear until the next time you reboot the system.

Leave a Reply