Apache Configuration Best Practices: sites-available vs httpd.conf on Ubuntu/Debian Systems


1 views

If you're working with Apache on Ubuntu/Debian systems, you've likely encountered two different configuration approaches:

# Traditional approach (httpd.conf)
DocumentRoot /var/www/html
<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

# Debian-style approach (sites-available)
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/example.com
    ServerName example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The difference stems from package maintainer decisions. RedHat-based systems (CentOS/RHEL) use the monolithic httpd.conf approach, while Debian/Ubuntu adopted a modular design with:

  • /etc/apache2/apache2.conf - Main configuration file
  • /etc/apache2/ports.conf - Port listening directives
  • /etc/apache2/sites-available/ - Available virtual hosts
  • /etc/apache2/sites-enabled/ - Enabled virtual hosts (symlinks)

For Ubuntu/Debian systems, here's the recommended approach:

# 1. Create new site configuration
sudo nano /etc/apache2/sites-available/example.com.conf

# 2. Enable the site (creates symlink)
sudo a2ensite example.com.conf

# 3. Reload Apache
sudo systemctl reload apache2

While sites-available is preferred for virtual hosts, some global settings belong in these files:

# In /etc/apache2/apache2.conf:
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

# In /etc/apache2/mods-available/mpm_prefork.conf:
<IfModule mpm_prefork_module>
    StartServers        5
    MinSpareServers     5
    MaxSpareServers     10
    MaxRequestWorkers   150
    MaxConnectionsPerChild 0
</IfModule>

If you're converting an existing httpd.conf setup:

# Original httpd.conf content:
DocumentRoot /var/www/old_site
<Directory /var/www/old_site>
    Options -Indexes +FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

# New sites-available/old_site.conf:
<VirtualHost *:80>
    DocumentRoot /var/www/old_site
    <Directory /var/www/old_site>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Use these commands to verify your setup:

# Check syntax without restarting
sudo apache2ctl configtest

# See loaded virtual hosts
apache2ctl -S

# List enabled sites
ls -l /etc/apache2/sites-enabled/

The sites-available approach offers several advantages:

  • Clean separation of configurations
  • Easy enable/disable without deletion
  • Better version control integration
  • Simpler mass deployments

In older Apache versions (pre-2.4), the httpd.conf file was indeed the primary configuration file. However, modern Debian/Ubuntu-based distributions (including Amazon Linux AMIs) have adopted a more modular approach using:

/etc/apache2/
├── apache2.conf
├── ports.conf
├── sites-available/
├── sites-enabled/
├── mods-available/
└── mods-enabled/

The main configuration hierarchy works like this:

  1. apache2.conf: Main configuration file that includes all others
  2. sites-available/: Stores all available virtual host configurations
  3. sites-enabled/: Contains symlinks to active configurations

Many tutorials reference httpd.conf because:

  • It's the traditional Apache configuration file name
  • Non-Debian systems (like CentOS) still use this naming convention
  • Older documentation hasn't been updated

Here's how to properly set up a virtual host on Ubuntu:

# Create new configuration
sudo nano /etc/apache2/sites-available/example.com.conf

# File contents:
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# Enable the site
sudo a2ensite example.com.conf
sudo systemctl reload apache2

You may need to edit /etc/apache2/apache2.conf (not httpd.conf) for:

# Global settings like:
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
  1. Always use sites-available for virtual hosts
  2. Use a2ensite/a2dissite commands rather than manually creating symlinks
  3. Test configurations before applying: sudo apachectl configtest
  4. For complex setups, consider using Include directives

If your changes aren't taking effect:

# Check if the site is enabled:
ls -l /etc/apache2/sites-enabled/

# Verify Apache is reading the correct files:
apachectl -S

# Check for syntax errors:
apachectl configtest

Remember that on Ubuntu-based systems, the modular approach using sites-available is the preferred method, while httpd.conf is largely deprecated in favor of apache2.conf.