Apache2 name-based virtual hosting on Debian/Ubuntu

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/Ubuntu is 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

  1. NameVirtualHost 127.0.0.1:80
  2. 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.

  1. <VirtualHost 127.0.0.1:80>
  2.         ServerAdmin webmaster@localhost
  3.         ServerName example.com
  4.         DocumentRoot /var/www/example.com
  5.         <Directory />
  6.                 Options FollowSymLinks
  7.                 AllowOverride None
  8.         </Directory>
  9.         <Directory /var/www/example.com>
  10.                 Options Indexes FollowSymLinks MultiViews
  11.                 AllowOverride None
  12.                 Order allow,deny
  13.                 allow from all
  14.         </Directory>
  15.         ScriptAlias /cgi-bin/ /var/www/example.com/cgi-bin/
  16.         <Directory "/var/www/example.net/cgi-bin">
  17.                 AllowOverride None
  18.                 Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  19.                 Order allow,deny
  20.                 Allow from all
  21.         </Directory>
  22.         ErrorLog /var/www/example.com/logs/error.log
  23.         CustomLog /var/www/example.com/logs/access.log combined
  24.         ServerSignature On
  25. </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.

No votes yet

Comments

Anonymous's picture

for you perfectly working introduction!
Great!

Anonymous's picture

[...] Apache2 name-based virtual hosting on Debian/Ubuntu [...]

Anonymous's picture

Wow, you really came with a great solution here. I am a freshman in web hosting but I want to move one further into blog hosting, this is the kind of business I want to be involved in. Your little tutorial is brilliant, congrats for that!

Anonymous's picture

[...] Apache2 name-based realistic hosting on Debian/Ubuntu Posted in Web Hosting on Aug 26th, 2008, 5:35 [...]

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may quote other posts using [quote] tags.

More information about formatting options