Hosting multiple sites with Apache

Posted by Steve on Thu 30 Sep 2004 at 18:50

Apache is probably the most popular webserver for the Linux platform, and despite being very powerful and extensible it is very well documented. In spite of this documentation many people seem to struggle with hosting multiple sites with Apache.

There are two ways to host multiple sites with one Apache instance, and both are very simple to setup. You have the choice of using Name based virtual hosts, or IP based virtual hosts.

In most common situations you will use Name based virtual hosts, this only requires that all the sites you wish to host point to the IP address of your Apache server.

To start with you'll need to install the apache server itself. On a Debian system you will install software via the apt-get system, so as root you need to run the following commands apt-get update and apt-get install apache.

Once this has been done you will find you have Apache installed, and it's default configuration is included inside the directory /etc/apache, by default this will be setup to serve the files that are contained in the directory /var/www.

To enable multiple host support you must add, or uncomment, the following line:


NameVirtualHost *

This sets up Apache to accept the hosts.

Once this is done you need to create the directories to contain your sites, personally I use /home/www/name.of.site.org.

Assuming that you wish to host two sites you should create two directories:


root@skx:# mkdir -p /home/www/www.foo.com
root@skx:# mkdir -p /home/www/www.foo.com/htdocs
root@skx:# mkdir -p /home/www/www.foo.com/logs
root@skx:# mkdir -p /home/www/www.foo.com/cgi-bin
root@skx:# mkdir -p /home/www/www.bar.com/
root@skx:# mkdir -p /home/www/www.bar.com/htdocs
root@skx:# mkdir -p /home/www/www.bar.com/logs
root@skx:# mkdir -p /home/www/www.bar.com/cgi-bin

This gives you the directories to place your content inside htdocs, a directory for the CGI scripts, if you need them, and a directory to contain the logfiles for that host.

The next step is to add the configuration for each site, this can be done by adding the following settings to the default configuration file, /etc/apache/httpd.conf:


<VirtualHost *>
        # Basic setup
        ServerAdmin webmaster@foo.com
        ServerName www.foo.com
        DocumentRoot /home/www/www.foo.com/htdocs/


	# HTML documents, with indexing.
        <Directory />
        Options +Includes
        </Directory>

        # CGI Handling
        ScriptAlias /cgi-bin/ /home/www/www.foo.com/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>

        # Logfiles
        ErrorLog  /home/www/www.foo.com/logs/error.log
        CustomLog /home/www/www.foo.com/logs/access.log combined
</VirtualHost>

<VirtualHost *>
        # Basic setup
        ServerAdmin webmaster@bar.com
        ServerName www.bar.com

        DocumentRoot /home/www/www.bar.com/htdocs/

	# HTML documents, with indexing.
        <Directory />
        Options +Includes
        </Directory>

        # CGI Handling
        ScriptAlias /cgi-bin/ /home/www/www.bar.com/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>

        # Logfiles
        ErrorLog  /home/www/www.bar.com/logs/error.log
        CustomLog /home/www/www.bar.com/logs/access.log combined
</VirtualHost>

This sets up two sites, which have their docements and logfiles contained beneath /home/www/www.nameofsite.com. You can adjust the paths if you wish to keep the files elsewhere.

Before attempting to restart the server you should run a quick test to make sure that the configuration file /etc/apache/httpd.conf doesn't contain any errors. To do that you can run:


root@skx:/etc/apache# apachectl  configtest
Syntax OK
root@skx:/etc/apache#

Any errors should be highlighted, but in this case we see that our syntax is OK, so we can restart apache with:


root@skx:/etc/apache# /etc/init.d/apache reload
Reloading apache configuration.
root@skx:/etc/apache#


This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2004 Steve - please ask for permission to republish or translate.