Skip to main content
< All Topics
Print

Apache HTTPD Configuration

name: apache-httpd-configuration

description: Configure Apache HTTP Server for multi-site hosting, URL rewriting, reverse proxying, and SSL. Use when setting up VirtualHosts, writing mod_rewrite rules, proxying to backend services, tuning Apache performance, or planning migration to Nginx.

Apache HTTPD Configuration

Instructions

Configure Apache HTTP Server for production hosting and reverse proxy use cases.

VirtualHost setup:

  • Define :80> and :443> blocks per site with ServerName and ServerAlias
  • Use DocumentRoot to set the web root for each virtual host
  • Separate site configs into /etc/apache2/sites-available/ and enable with a2ensite

URL rewriting with mod_rewrite:

  • Enable with a2enmod rewrite and set AllowOverride All in the directory block
  • Use RewriteEngine On, RewriteCond, and RewriteRule for URL transformations
  • Common patterns: trailing slash enforcement, www/non-www canonicalization, HTTP-to-HTTPS redirect
  • Prefer RewriteRule in VirtualHost config over .htaccess for performance

Reverse proxying with mod_proxy:

  • Enable mod_proxy and mod_proxy_http with a2enmod proxy proxy_http
  • Use ProxyPass /api/ http://localhost:8000/api/ and matching ProxyPassReverse directives
  • Set ProxyPreserveHost On to forward the original Host header
  • Add RequestHeader set X-Forwarded-Proto "https" when terminating SSL at Apache

.htaccess for directory-level overrides:

  • Use sparingly — each request triggers a filesystem walk to find .htaccess files
  • Common uses: auth (AuthType Basic), rewrite rules, custom error pages, MIME types
  • Disable with AllowOverride None in production when rules can live in VirtualHost config

SSL/TLS with mod_ssl:

  • Enable with a2enmod ssl and configure SSLCertificateFile, SSLCertificateKeyFile, SSLCertificateChainFile
  • Set SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1 and strong SSLCipherSuite
  • Use certbot --apache plugin for automated Let’s Encrypt integration

Performance tuning:

  • Choose the right MPM: event (recommended for modern workloads), worker (threaded), prefork (process-based, required for mod_php)
  • Enable KeepAlive On with reasonable MaxKeepAliveRequests (100) and KeepAliveTimeout (5)
  • Enable mod_deflate for compression and mod_expires for cache headers

Migration path to Nginx:

  • Map VirtualHost blocks to Nginx server blocks
  • Replace mod_rewrite rules with Nginx rewrite and location directives
  • Replace ProxyPass with Nginx proxy_pass in location blocks
  • Eliminate .htaccess — Nginx has no equivalent; all config is centralized
Table of Contents