A pdf version of this posting can be downloaded here.
Websites, in all their different forms, are hosted on web servers and the Apache web server is one of the most popular currently available. It would be incredibly inefficient to only host one website per server, particularly in a commercial web hosting scenario, but for designers and developers at any level there is often a need to work on different sites, or different versions of the same site, in the same environment. In order to make the most of your resources the logical approach is to host multiple sites on one installation of Apache server.
On Apache each website is treated as a Virtual Host, with the concept of hosts relating to how DNS is configured in order to route traffic to a website. When a web browser requests a website via a URL the request is handled by DNS which knows that, for example, http://www.somesite.com relates to a specific IP address – the address of the server that’s hosting the site. DNS forwards the traffic to the server which in turn responds with the information requested. As far as DNS is concerned the URL of the website bound to a specific IP address is just another host in its database.
However, for another example, the DNS record for http://www.someothersite.com also relates to the same IP address as http://www.somesite.com as both sites are hosted on the same server, so requests for either site end up hitting the same IP address. In order to handle multiple sites Apache takes over once the request for a site reaches the server. Apache reads the Fully Qualified Domain Name (FQDN) requested, checks the name against its configuration, and retrieves the information for that site. In this way a request for http://www.somesite.com delivers different information than the request for http://www.someothersite.com despite them both existing on the same server.
When creating a new website on Apache you are in fact creating a new Virtual Host, i.e. a new host (made up of a name bound to an IP address) for DNS to route requests to. From the perspective of DNS each website is a different host even though each one has the same IP address. Apache treats all sites as virtual hosts so the default site that is enabled when apache is installed is actually the first virtual host on the server. The most efficient method of creating additional virtual hosts is to copy the default and change the details to suit the new site.
This procedure configures Apache server to host multiple sites on a Linux platform, in this case Ubuntu 10.04 running Apache 2.2.14 and assumes that Ubuntu is fully installed, that Open SSH (or Telnet or similar) is installed on the Linux computer to allow you to connect remotely via SSH client or Putty (alternatively you can perform the procedure locally on the computer), and that Apache is installed.
It is also assumed that DNS is providing name resolution services via a Windows Server 2003 implementation.
This procedure creates a new Virtual Host for the site mynewsite on an Apache server within the domain testdomain.com (i.e. the Fully Qualified Domain Name of the resulting website will be: mynewsite.testdomain.com.
1. Create a directory in /var to store your websites. The directory where the default website installed with Apache stores its files is /var/www. Create a directory into which subdirectories for each site hosted can be created.
sudo mkdir /var/sites
sudo mkdir /var/sites/mynewsite
2. Copy in the index.html from the default virtual host to the directory of the new host in order to provide a basic test of the new host
sudo cp /var/www/index.html /var/sites/mynewsite
3. Edit the index.html file in the mynewsite directory so that you can identify the site when you access it (basic vi commands are provided in the Additional Information section of this post).
sudo vi /var/sites/mynewsite/index.html
4. The configuration file for each host is stored in /etc/apache2/sites-available. Make a copy of default in /etc/apache2/sites-available and give it the name of the new site being created
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mynewsite
5. Edit the new configuration file with details of the new site
sudo vi /etc/apache2/sites-available/mynewsite
a. Navigate to the first free space in the file after the <VirtualHost *.80> entry and add the ServerName directive with details of the fully qualified domain name:
b. Edit the DocumentRoot directive to reflect the location of the files that make up the site:
c. Edit the second Directory directive to reflect the location of the files that make up the site:
6. Run the Apache2 Enable Site utility to create the symbolic link to your new site file from /etc/apache2/sites-enabled to /etc/apache2/sites-available
sudo a2ensite mynewsite
7. Reload Apache to make the new site available
sudo /etc/init.d/apache2 reload
8. Access the DNS system on Windows Server 2003
Start > All Programs > Administrative Tools > DNS
a. Expand Forward Lookup Zones, expand testdomain.com
b. Right click in right hand pane and choose “new host (A)”
c. Enter the website name mynewsite (do not enter the FQDN as this is populated automatically in the FQDN field)
d. Enter the IP address of the computer running Apache
e. Click Add host
9. Test the site by entering the fully qualified name mynewsite.testdomain.com into a web browser.
Basic vi Commands:
1. i to insert
2. x to delete
3. dd to delete line
4. [ESC] to leave current mode
5. :w to write to file
6. :q to quit
Ubuntu Apache documentation: https://help.ubuntu.com/8.04/serverguide/C/httpd.html
The Apache Software Foundation: http://www.apache.org/
Windows Server 2003 DNS FAQ: http://support.microsoft.com/kb/291382