Apache 2 Web Server on Debian 6

Set the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you’ve followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Install Apache 2

Make sure your package repositories and installed programs are up to date by issuing the following commands:

apt-get update
apt-get upgrade --show-upgraded

Enter the following command to install the Apache 2 web server, its documentation and a collection of utilities.

apt-get install apache2 apache2-doc apache2-utils

Install Support for Scripting

The following commands are optional, and should be run if you want to have support within Apache for server-side scripting in PHP, Ruby, Python, or Perl.

To install Ruby support, issue the following command:

apt-get install libapache2-mod-ruby

To install Perl support, issue the following command:

apt-get install libapache2-mod-perl2

To install Python support, issue the following command:

apt-get install libapache2-mod-python

If you need support for MySQL in Python, you will also need to install Python MySQL support:

apt-get install python-mysqldb

Your PHP application may require additional dependencies included in Debian. To check for available PHP dependencies run “apt-cache search php“, which will provide a list of package names and descriptions. To install, issue the following command:

apt-get install libapache2-mod-php5 php5 php-pear php5-xcache

Issue the following command to install the php5-suhosin package, which provides additional security to your PHP installation:

apt-get install php5-suhosin

If you’re also hoping to run PHP with MySQL, then also install MySQL support:

apt-get install php5-mysql

Configure Apache for Named-Based Virtual Hosting

Apache supports both IP-based and name-based virtual hosting, allowing you to host multiple domains on a single server. To begin configuration, issue the following command to disable the default Apache virtual host.

a2dissite default

Each additional virtual host needs its own file in the /etc/apache2/sites-available/ directory. In this example, you’ll create files for two name-based virtually hosted sites, “example.net” and “example.org”.

First create example.net (/etc/apache2/sites-available/example.net) so that it resembles the following.


<VirtualHost *:80>
     ServerAdmin webmaster@example.net
     ServerName example.net
     ServerAlias www.example.net
     DocumentRoot /srv/www/example.net/public_html/
     ErrorLog /srv/www/example.net/logs/error.log
     CustomLog /srv/www/example.net/logs/access.log combined

If you would like to enable Perl support, then add the following lines to the VirtualHost entry above.

File excerpt:/etc/apache2/sites-available/bucknell.net

Options ExecCGI
AddHandler cgi-script .pl

Next, create example.org (/etc/apache2/sites-available/example.org) so that it resembles this:


<VirtualHost *:80>
     ServerAdmin admin@example.org
     ServerName example.org
     ServerAlias www.example.org
     DocumentRoot /srv/www/example.org/public_html/
     ErrorLog /srv/www/example.org/logs/error.log
     CustomLog /srv/www/example.org/logs/access.log combined

You’ll note that some basic options are specified for both sites, including where the files for the site will reside (under /srv/www/). You can add (or remove) additional configuration options, such as the Perl support, on a site-by-site basis to these files as your needs dictate.

Create required directories for these sites by issuing the following commands:

mkdir -p /srv/www/example.net/public_html
mkdir /srv/www/example.net/logs

mkdir -p /srv/www/example.org/public_html
mkdir /srv/www/example.org/logs

Enable the sites by issuing these commands:

a2ensite example.net
a2ensite example.org

Finally, restart the Apache server to initialize all the changes, with this command:

/etc/init.d/apache2 restart

When you create or edit any virtual host file, you’ll need to reload the config, which you can do without restarting the server with the following command:

/etc/init.d/apache2 reload

Congratulations! You now have Apache installed on your Debian Linux VPS and have configured the server for virtual hosting.

Install Apache Modules

One of Apache’s prime strengths is its extreme customizability and flexibility. With its support for a large number of modules, there are few web serving tasks that Apache cannot fulfill. By default, modules and their configuration files are installed in the /etc/apache2/mods-available/ directory. Generating a list of this directory will tell you what modules are installed. To enable a module listed in this directory, use the following command:

a2enmod [module-name]

Note that in the /etc/apache2/mods-available/ directory, files have a .load and .conf extension. Module names do not include the extension.

To disable a module that is currently enabled, use the inverse command:

a2dismod [module-name]

To get a list of available Apache modules modules in the Debian repository use the following command:

apt-cache search libapache2*

To install one of these modules use the command:

apt-get install [module-name]

Modules should be enabled and ready to use following installation, though you may have to apply additional configuration options to have access to the modules’ functionality. Consult the Apache module documentation for more information regarding the configuration of specific modules.