Engaging Social Software Blog

Setting up a virtual host on ubuntu tutorial.

Posted by: pagetribe on: March 13, 2009

Introduction:
This tutorial will show you how to set up a name-based virtual host using Apache2 and ubuntu.

Definition:
Virtual host allows you to host multiple websites on a singe webserver.
Name-based means you can access different sites on the same webserver using different names eg. domain1.com & domain2.com

Configuration:

1. Create a document root, cgi-bin and logs directory for each of the new sites you want. This tutorial will set up one new site; example.org

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

2. Create a virtual host configuration file for the new site:

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example.org
(this copies the default file and renames it to example.org)

3. Edit the /etc/apache2/sites-available/example.org file and add the bold

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName example.org
ServerAlias www.example.org

#ServerAlias allows for both example.org and www.example.org to be used.

DocumentRoot /var/www/example.org
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/example.org>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /var/www/example.org/cgi-bin/
<Directory "/var/www/example.org/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/www/example.org/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/www/example.org/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>

4. Edit the /etc/hosts file and add the following:

127.0.0.1  example.org
127.0.0.1 www.example.org

(this will allow both example.org and www.example.org to be used)

5. Edit /etc/apache2/apache2.conf and add the following to the end of the file:

NameVirtualHost 127.0.0.1:80

6. Enable your virtual host (a2ensite is apache2 enable site):

a2ensite example.org

7. Restart apache

/etc/init.d/apache2 restart

8. Finally, open your browser and type http://example.org and then to see if the ServerAliaas is working http://www.example.org

2 other great tutorials:

Apache2 name-based virtual hosting on Debian/Ubuntu

Ubuntu Intrepid – Apache Virtual Hosts #1

Leave a Reply