How to Configure Apache on DreamCompute Running Debian or Ubuntu

Overview

Apache is the most widely used HTTP server on the internet and DreamHost uses it as the default HTTP server for all hosting products.

These instructions assume you have launched an instance running a Debian or Ubuntu. View the following article for instructions on how to launch an instance:

Log into your instance as the default user

Log into your new instance using the SSH Keys you assigned when creating it.

The following commands require sudo privileges which are assigned to the default user of the instance automatically.

Installing Apache

Run the following commands:

[user@instance]:$ sudo apt-get update
[user@instance]:$ sudo apt-get install -y apache2

When it completes, the Apache HTTP server is installed and runs with its default configuration. If you visit the public IP address of your instance in your browser, you will see the Apache default page.

The /etc/apache2 directory

This directory contains all the configuration files for your Apache server. If you edit any of the files you'll find that most include details on the file's general purpose at the top.

/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. See the 'Adding a website to DreamCompute' article for further instructions on how to add site config files in the /etc/apache2/sites-available directory.

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:

  • mpm-event
  • mpm-prefork
  • mpm-worker

Check which MPM configuration is enabled

Run the following command:

[user@instance]:$ apache2ctl -M | grep mpm
 mpm_event_module (shared)

This shows the event module is enabled. If you like, you could change that to mpm-prefork with the following commands.

[user@instance]:$ sudo a2dismod mpm_event
[user@instance]:$ sudo a2enmod mpm_prefork
[user@instance]:$ sudo service apache2 restart

Virtual Hosts

Virtual hosts define each site so that Apache knows what it should do when it receives a request. You can view all virtual host configuration files in the /etc/apache2/sites-available directory.

/etc/apache2/sites-available/000-default.conf

The /etc/apache2-sites-available directory contains a default configuration file titled 000-default.conf. This configuration files is responsible for the following:

  • 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/example.com.conf

Any site added to your DreamCompute instance would have its own configuration file created. If your site was example.com, the configuration file would be titled example.com.conf.

  • For each site you wish to configure, it is 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 Apache’s wiki Example Vhosts page. A basic one for listening on port 80 (http) with custom logging is shown below:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/www.example.com

CustomLog /var/log/apache/www.example.com-access.log combined
ErrorLog /var/log/apache/www.example.com-error.log
</VirtualHost>

Alternatively, if you wish to specify the ip instead of * you can use the following command replacing 1.1.1.1 with your real ip address:

<VirtualHost 1.1.1.1:80>

Add your site code

Now that the config file is created, you should add your site's code to the instance. You'll add it in the following directory:

/var/www/example.com/index.html

Managing Virtual Host files

When you have your sites virtual host file set up, you can enable/disable it by entering the following commands:

sudo a2ensite — Provides a list of sites files that you can enable.
[user@instance]:$ sudo a2ensite
sudo a2dissite — Provides a list of sites files you can disable.
[user@instance]:$ sudo a2dissite
sudo service apache2 reload — Reloads apache to make the change live after you enable or disable a site.
[user@instance]:$ sudo service apache2 reload

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:

[user@instance]:$ sudo a2enmod <module-name>
[user@instance]:$ sudo a2dismod <module-name>

If you do not specify a module name, the command 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:

[user@instance]:$ sudo service apache2 reload

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?