Configure Apache on Debian or Ubuntu

Apache is the most widely used HTTP server on the internet, and we use it extensively at DreamHost as the default HTTP server for all hosting products.
 * You can use it for your DreamCompute instance as well, and the following information helps you install and configure it.
 * These instructions assume you run a Debian- or Ubuntu-based system as they have their own specific configuration and file hierarchy.

Installing Apache
To install apache on your system, run the following commands:

sudo apt-get update sudo apt-get install apache2


 * The install process asks you to confirm if you wish to install any additional packages needed for this instance of apache.
 * Confirm by entering "y" and hitting enter.

Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert Suggested packages: apache2-doc apache2-suexec apache2-suexec-custom openssl-blacklist The following NEW packages will be installed: apache2 apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert 0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded. Need to get 1,487 kB of archives. After this operation, 5,673 kB of additional disk space will be used. Do you want to continue [Y/n]? y
 * 1) apt-get install apache2


 * When it completes, the apache HTTP server is installed and runs with its default configuration.
 * If you visit the floating IP in your browser for your DreamCompute instance, you are able to see the 'getting started' page.
 * You can find this IP on the Instances (IP Address column) or Access & Security (floating ips tab) panel pages.

The default page displays the following when apache successfully installs:

It works! This is the default web page for this server. The web server software is running but no content has been added, yet.

The /etc/apache2 Directory
This directory contains all the configuration files for your apache server. The top of most configuration files in this directory include details on its general purpose.

The basic functionality of the files are described in the next section.

/etc/apache2/apache2.conf File
This is the main configuration file that ultimately controls how apache functions. While it is possible to completely configure your sites and modules directly in this file, instead it is recommended to use smaller individual files for each of your sites and modules for simplicity. This is made possible by the "Include" directive to insert other files into the apache2.conf at runtime. Some of the values of interest in here are:


 * Timeout
 * Length of time in seconds that apache attempts to fulfill a request. Default:  300


 * KeepAlive
 * Define if persistent connections is allowed, which allows more than one request per connection. Default:  On


 * MaxKeepAliveRequests
 * Define the maximum number of requests allowed for each KeepAlive persistent connection. Default:  100


 * KeepAliveTimeout
 * Define the number of seconds to wait for another request before ending the KeepAlive persistent connection. Default:  5


 * MPM Configuration
 * Debian and Ubuntu have different apache packages that are optimized for different situations. Each package is a different flavor of MPM (multi-processing module) and settings for each are defined near the end of this file.  The packages available are apache2-mpm-prefork, apache2-mpm-worker, and apache2-mpm-event, with apache2-mpm-worker being the default.  This is the default threaded implementation of apache and is recommended for high-traffic sites due to its speed and smaller memory footprint.

To check which MPM configuration (and modules) exist, run the following:

apache2 -l

''The enabled modules are listed, ending with .c. In this case, the worker module is enabled:''

Compiled in modules: core.c mod_log_config.c  mod_logio.c  mod_version.c  worker.c  http_core.c  mod_so.c

Virtual Hosts
Virtual hosts define each site so that apache knows what it should do when it receives a request.

/etc/apache2/sites-available/default

 * Defines what apache should do when it gets a request that matches no other virtual hosts.
 * If you only expect to have one site on your DreamCompute instance, you could use this file and no others if you prefer.
 * For those with multiple sites, this can be used to instruct the visitor that they may have done something wrong, or redirect them to another site.

/etc/apache2/sites-available/YOURSITEHERE

 * For each site you wish to configure, we recommended you name a file similar to your site name in the /etc/apache2/sites-available/ directory.
 * There are several example virtual hosts available on the wiki.apache.org Example Vhosts page but you can view a basic one for listening on port 80 (http) with custom logging here:

 ServerName foo.com ServerAlias www.foo.com DocumentRoot /var/www/www.foo.com

CustomLog /var/log/apache/www.foo.com-access.log combined ErrorLog /var/log/apache/www.foo.com-error.log 


 * Alternatively, if you wish to specify the ip instead of "*" you can use the following command:



Managing Virtual Host files
When you have your sites virtual host file setup, you can enable/disable it by entering the following commands:


 * sudo a2ensite
 * Provides a list of sites files that you can enable.


 * sudo a2dissite
 * Provides a list of sites files you can disable.


 * service apache2 reload
 * Reloads apache to make the change live after you enable or disable a site.

These commands create a symlink for your sites file from /etc/apache2/sites-enabled to its corresponding file in /etc/apache2/sites-available.

Modules
Modules can be enabled or disabled by the following commands:

sudo a2ensite

sudo a2dissite


 * When you run the command, it displays a list of modules available to enable or disable.
 * After you enable or disable a site, reload apache to make the change live by using the following command:

service apache2 reload