How To Configure the Apache Web Server on an Ubuntu or Debian VPS
Practical guide to configure apache on ubuntu: file hierarchy, apache2.conf, global settings, virtual hosts, enabling modules and troubleshooting.
Drake Nguyen
Founder · System Architect
Introduction
Apache remains one of the most widely used web servers. Whether you run a personal site or manage multiple virtual hosts on a VPS, knowing how to configure apache on ubuntu helps you control how content is served, which modules are enabled, and how requests are routed. This guide covers the Apache file layout used on Ubuntu and Debian, explains key settings in apache2.conf, and walks through creating virtual hosts and enabling modules with the standard tools.
Prerequisites
A server running Ubuntu or Debian with a non-root sudo user and a firewall configured. See your distribution’s initial server setup if you need to prepare the machine.
Apache installed (for example, on Ubuntu 22.04). If you haven’t installed Apache yet, follow a distribution-specific install guide for apache ubuntu or debian apache before continuing.
Overview: 5 Steps to configure apache on ubuntu
Understand the apache file hierarchy
Inspect
/etc/apache2/apache2.confTune global configuration options (timeouts, keepalive, MPM)
Create and edit apache virtual host files
Enable sites and modules using
a2ensiteanda2enmod
Step 1: Apache file hierarchy on Ubuntu/Debian
Apache on Ubuntu and Debian organizes configuration into a modular directory structure under /etc/apache2. This layout helps separate global settings, virtual hosts, and module fragments so you can manage each piece independently.
apache2.conf— primary global configuration file that includes other files.ports.conf— declares which ports Apache listens on (important for HTTP and HTTPS).sites-available/andsites-enabled/— virtual host files live in sites-available and are activated via symlinks in sites-enabled (sites-available vs sites-enabled apache ubuntu).conf-available/andconf-enabled/— smaller configuration fragments that can be enabled or disabled.mods-available/andmods-enabled/— module.loadand.conffiles; use the a2enmod/a2dismod helper commands.
ls -f /etc/apache2
Step 2: Explore apache2.conf and included files
The /etc/apache2/apache2.conf file contains global directives and uses Include and IncludeOptional to pull in module definitions, port settings and enabled site configurations. This include model is what makes apache2.conf act as a central manifest while keeping each area modular.
/etc/apache2/apache2.conf (example excerpts)
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
How this matters
When you enable a module or site, Apache reads the enabled directories to build the effective configuration at startup. Understanding the include chain (apache2.conf includeoptional mods-enabled conf-enabled) helps troubleshoot missing directives or duplicate definitions.
Step 3: Set global Apache configuration
Global settings in apache2.conf or enabled conf files control behavior for the whole server. Common parameters to review include:
Timeout — default is often high (e.g., 300). Lowering to 30–60 seconds reduces long-hanging connections for many setups.
KeepAlive — when On, connections stay open to serve multiple requests; Off forces new TCP handshakes for each request and increases overhead.
MaxKeepAliveRequests — how many requests a persistent connection may handle (default ~100; 0 = unlimited).
KeepAliveTimeout — how long Apache waits for the next request on a keep-alive connection (default often 5 seconds).
Multi-Processing Modules (MPMs)
MPMs control how Apache handles concurrency. On Ubuntu you can check the active MPM and available options. The common MPMs are event, worker, and prefork. To query the MPM:
a2query -M
Note: Only one MPM can be enabled at a time. Choose an MPM that matches your application’s threading and module compatibility requirements (for example, some PHP setups work best with
prefork).
Step 4: Configure Apache virtual hosts
Virtual host files map requests to content and are typically stored in /etc/apache2/sites-available/. The default file 000-default.conf demonstrates a basic vhost that listens on port 80. Use a site-specific file to define ServerName, ServerAlias, and DocumentRoot.
sudo nano /etc/apache2/sites-available/your_domain.conf
ServerName your_domain.com
ServerAlias www.your_domain.com
DocumentRoot /var/www/your_domain/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Directory and access control
Define directory-level settings to control permissions and whether .htaccess files can override global rules (AllowOverride). Default entries in apache2.conf often include examples for /, /usr/share, and /var/www/. Use Require directives to restrict or allow access.
Alias and ScriptAlias
Use Alias to map URL paths to filesystem locations and ScriptAlias for directories that contain executable scripts (CGI). Always accompany aliases with <Directory> blocks that set access controls.
Alias "/content/" "/usr/local/apache/content/"
ScriptAlias "/cgi-bin/" "/usr/local/apache2/cgi-bin/"
Step 5: Enable sites and modules
After creating a virtual host in sites-available, enable it with a2ensite. Enable or disable modules using a2enmod and a2dismod. After changes restart or reload Apache so the new configuration takes effect.
sudo a2ensite your_domain
sudo a2dissite 000-default
sudo a2enmod info
sudo a2dismod info
sudo systemctl restart apache2
Understanding sites-available sites-enabled explained and the companion a2ensite command ubuntu example helps keep your server tidy and prevents accidentally serving unwanted configurations.
Troubleshooting and tips
Check syntax before restarting:
apache2ctl configtest.Inspect logs:
/var/log/apache2/error.logand/var/log/apache2/access.log.For SSL, confirm
ports.confincludes the correct Listen directives for port 443.When enabling features, ensure any required module is loaded (use
a2enmodto enable apache modules ubuntu a2enmod).
Conclusion
Learning how to configure apache on ubuntu gives you control over performance, security, and how multiple sites are served. The modular directory layout (/etc/apache2), the role of apache2.conf, and the site/module helpers (a2ensite, a2enmod) make Apache flexible and maintainable. For deeper details consult the official Apache documentation and test changes in a staging environment before applying them in production.