How to Set Up Apache 2 Name-based Virtual Host on Debian Etch and Ubuntu 8.04

Introduction

This tutorial will walk you through the configuration of name-base virtual hosting using Apache2 server in Debian and Ubuntu. The default configuration of Apache server in Debian Etch and Ubuntu 8.04 are limited only to one website based on your IP Address. We will setup a name-based virtual hosting to have multiple websites using only one machine. After this tutorial, you will have example.com, example.net, and example.org hosted in your computer.

What is Virtual Host?

With virtual host, you can host multiple web site on a single machine.

Virtual Host can be:
Ip-based - you can host multiple website on a single server however, you must assign each website with a different IP Address.
Name-based - you can host multiple website on a single server or a single IP Address but proper DNS configuration is required.

Step-by-Step Configuration

This tutorial assumes that you already have a working apache2 web server. If you don't have it, please install it first before taking these steps:

1. Edit your /etc/hosts file

vi /etc/hosts

and add the example.com, example.net, and example.org in your hosts file.

127.0.0.1 localhost
127.0.0.1 example.com
127.0.0.1 example.net
127.0.0.1 example.org
192.168.0.1 ubuntu

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

This will tell the system that example.com, example.net and example.org are not to be looked for on the internet, but on the local machine instead.

2. Create a a separate document root, cgi-bin directory and logfile directory for each website you want to host. You can place these beneath the standard Debian prefix of /var/www or you may use a completely different directory.

mkdir /var/www/example.com
mkdir /var/www/example.com/cgi-bin
mkdir /var/www/example.com/logs
mkdir /var/www/example.net
mkdir /var/www/example.net/cgi-bin
mkdir /var/www/example.net/logs
mkdir /var/www/example.org
mkdir /var/www/example.org/cgi-bin
mkdir /var/www/example.org/logs

3. We will enable Virtual Host in your /etc/apache2/apache2.conf file. Open /etc/apache2/apache2.conf file

vi /etc/apache2/apache2.conf

and add this line to the end of the file

NameVirtualHost 127.0.0.1:80
NameVirtualHost 127.0.0.1:443

4. Disable the Apache2 default host configuration

a2dissite default

5. Lets create a virtual host configuration for each site. You don't have to create from scatch actually, you can copy the default host configuration and customize it.

cp /etc/apache2/site-available/default /etc/apache2/site-available/example.com
cp /etc/apache2/site-available/default /etc/apache2/site-available/example.net
cp /etc/apache2/site-available/default /etc/apache2/site-available/example.org

The virtual host configuration should look like this.

<VirtualHost 127.0.0.1:80>
        ServerAdmin webmaster@localhost
        ServerName example.com
        DocumentRoot /var/www/example.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/example.com>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
        ScriptAlias /cgi-bin/ /var/www/example.com/cgi-bin/
        <Directory "/var/www/example.net/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>
        ErrorLog /var/www/example.com/logs/error.log
        CustomLog /var/www/example.com/logs/access.log combined
        ServerSignature On
</VirtualHost>

6. Enable your virtual host configuration

a2ensite example.com
a2ensite example.net
a2ensite example.org

7. Finally, restart your Apache2 server

/etc/init.d/apache2 restart

Open your browser and type http://example.com, example.net, and example.org.