There are many different ways you can configure Apache2 to host multiple sites. Here we’re only going to cover the basics with the use of the NameVirtualHost directive.
The advantage of this approach is that you don’t need to hard-wire any IP addresses, and it will just work. The only thing you need is for your domain names to resolve to the IP address of your webserver.
For example if you have an Apache2 server running upon the IP address 192.168.4.1 and you wish to host the three sites example.com, example.net, and example.org you’ll need to make sure that these names resolve to the IP address of your server.
Since we’ll be hosting multiple websites on the same host it makes a lot of sense to be very clear on the location of each sites files upon the filesystem. The way I suggest you manage this is to create a completely seperate document root and logfile directory for each host. You can place these beneath the standard Debian prefix of /var/www or you may use a completely different root.
Step 1. If you’ve not already done create the directories to contain your content, etc, as follows:
Deb:~# mkdir /var/www/www.example.com
Deb:~# mkdir /var/www/www.example.net
Deb:~# mkdir /var/www/www.example.org
Step 2. Next thing to do is to enable virtual hosts in your Apache2 configuration. The simplest way to do this is to create a file called /etc/apache2/conf.d/virtual.conf and include the following content in it:
NameVirtualHost *
Step 3. Once we’ve done this we can create the individual host configuration files. The Apache2 setup you’ll find on DebianĀ includes two directories for locating your site configuration files:
/etc/apache2/sites-available
* This contains configuration files for sites which are available
/etc/apache2/sites-enabled.
* This directory contains site files which are enabled.
Step 4. Lets start with a real example. Create /etc/apache2/sites-available/www.example.com with the following contents:
ServerAdmin webmaster@example.com
ServerName www.example.com
ServerAlias example.comDocumentRoot /var/www/www.example.com/
# Logfiles
ErrorLog /var/logs/www.example.com/logs/error.log
CustomLog /var/logs/www.example.com/logs/access.log combined
Step 4.2 Next create the file www.example.net:
ServerAdmin webmaster@example.net
ServerName www.example.net
ServerAlias example.netDocumentRoot /var/www/www.example.net/
# Logfiles
ErrorLog /var/logs/www.example.net/logs/error.log
CustomLog /var/logs/www.example.net/logs/access.log combined
Step 4.3 Create the file www.example.org:
ServerAdmin webmaster@example.org
ServerName www.example.org
ServerAlias example.orgDocumentRoot /var/www/www.example.org/
# Logfiles
ErrorLog /var/logs/www.example.org/logs/error.log
CustomLog /var/logs/www.example.org/logs/access.log combined
Step 5. To enable the sites simply run:
Deb:~# a2ensite www.example.com
Deb:~# a2ensite www.example.net
Deb:~# a2ensite www.example.org
* This will now create the symbolic links so that /etc/apache2/sites-enabled/www.example.org, etc, now exist and will be read.
Step 6. Now we’ve finished our setup we can restart, or reload, the webserver:
Deb:~# /etc/init.d/apache2 reload
Reloading web server config…done.
Done

Recent Comments