How To Configure Apache Web Server on an Ubuntu or Debian VPS

Introduction

Apache is one of the most popular web servers on the internet. It is used to serve more than half of all active websites. Although there are many viable web servers that will serve your content, it is helpful to understand how Apache works because of its ubiquity.

This article will examine some general configuration files and the options that can be controlled within them. This article will follow the Ubuntu/Debian layout of Apache files, which is different from how other distributions build the configuration hierarchy.

Prerequisites to Configure Apache Web Server

If you are using Ubuntu version 16.04 or below, we recommend you upgrade to a more recent version since Ubuntu no longer provides support for these versions. This collection of guides will help you upgrade your Ubuntu version.

  • A server running Ubuntu, along with a non-root user with sudo privileges and an active firewall.
  • Apache installed on your server. Refer to our tutorial on How to Install the Apache Web Server on Ubuntu.

5 Steps to Configure Apache Web Server on Ubuntu

  1. The Apache File Hierarchy
  2. Exploring the apache2.conf File
  3. Set Apache Global Configurations
  4. Setup Apache Virtual Host File
  5. Enable Sites and Modules

Step 1: The Apache File Hierarchy

Apache keeps its main configuration files within the /etc/apache2 folder. Use this command to list the files:

The directory contains:

  • apache2.conf: The main configuration file for the server.
  • ports.conf: Specifies the ports for virtual hosts.
  • sites-available/ and sites-enabled/: Manage virtual host configurations.
  • conf-available/ and conf-enabled/: Contain unattached configuration fragments.
  • mods-enabled/ and mods-available/: Manage optional modules.

Step 2: Exploring the apache2.conf File

The main configuration details for your Apache server are in the /etc/apache2/apache2.conf file. Open it with:

sudo nano /etc/apache2/apache2.conf

The file is divided into sections for global settings, default server configuration, and virtual hosts. It uses Include directives to dynamically generate an overarching configuration file at startup.

Step 3: Set Apache Global Configurations

Modify options such as:

  • Timeout: Reduce from 300 to between 30 and 60 seconds for better performance.
  • KeepAlive: Set to On to handle multiple requests per connection.
  • MaxKeepAliveRequests: Set to 100 or 0 for unlimited requests.
  • KeepAliveTimeout: Default is 5 seconds.

Step 4: Update Apache Virtual Host File

Open the default virtual host file:

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

Adjust directives like ServerName and DocumentRoot to match your domain and content directory. Use ServerAlias to define alternate paths, such as www.your_domain.com.

Step 5: Enabling Sites and Modules

Enable a virtual host:

Restart Apache:

sudo systemctl restart apache2

Enable a module:

Disable a module:

Conclusion to Configure Apache Web Server

Apache is versatile and modular, allowing you to tailor configurations to your needs. This guide provided a foundation to help you confidently explore and modify Apache’s configuration files. For specific options, consult the detailed comments in the provided files or the Apache documentation.