Apache Virtual Hosts

Howto Setup Apache Virtual Hosts

This article relates to Apache2 running on Debian GNU/Linux.

Install Apache2

bash ~# apt-get install apache2

Use Default Config for New Virtual Host

Change directories to the site-available directory in the apache configuration
directory.

bash ~# cd /etc/apache2/sites-available/

There is a default site in this directory. To create a new site just cp the
default one to the name of your new site.

bash ~# cp default www.example.com

Now you will want to edit www.example.com and configure it the way you want it.

bash ~# nano -w www.example.com

Here is an idea of what you might have in the file www.example.com:

-------------------------------------------------------------------------------
# If you have multiple virtual hosts one virtual host needs the
# NameVritualHost directive set. Usually this is the main virtual host.
NameVirtualHost *

ServerAdmin webmaster@example.com
ServerName www.example.com
# If you want the server to answer for other names uncomment the
# directive below.
ServerAlias example.com

# Set the DocumentRoot to where your website files reside.
DocumentRoot /var/www

Options FollowSymLinks
AllowOverride None

# Set Directory /var/www to the same thing as DocumentRoot above.

Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
# RedirectMatch ^/$ /apache2-default/

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

# Usually we customize the log files to be specific for a site.
ErrorLog /var/log/apache2/www.example.com-error.log

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

# Usually we customize the log files to be specific for a site.
CustomLog /var/log/apache2/www.example.com-access.log combined
ServerSignature On

-------------------------------------------------------------------------------

When you are done configuring the file save it and exit.

Enable The New Virtual Host

bash ~# a2dissite 000-default
Site 000-default disabled; run /etc/init.d/apache2 reload to fully disable.
bash ~# a2ensite www.example.com
Site www.example.com installed; run /etc/init.d/apache2 reload to enable.

Restart/Reload Apache2

bash ~# /etc/init.d/apache2 restart

To reload apache rather then restarting it.

bash ~# /etc/init.d/apache2 reload