Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Apache HTTP Server

Architecture

Apache uses Multi-Processing Modules (MPMs):

MPM prefork

Client → Master → Worker Process (handles entire request)
                → Worker Process
                → Worker Process

One process per connection. Safe for non-thread-safe modules (PHP mod_php). High memory usage.

MPM worker

Client → Master → Worker Process → Thread 1 (handles request)
                                  → Thread 2
                                  → Thread 3

Multiple threads per process. Better memory efficiency. Thread-safe modules required.

MPM event

Client → Master → Worker Process → Thread (async keep-alive)
                                  → Thread (active request)

Like worker but handles keep-alive connections asynchronously. Best performance. Closest to Nginx model.

Configuration

Virtual Hosts

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example
    
    <Directory /var/www/example>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

.htaccess

Per-directory configuration (slower than central config):

# /var/www/example/.htaccess
RewriteEngine On
RewriteRule ^api/(.*)$ http://backend:8080/$1 [P,L]

# Caching
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
</IfModule>

mod_rewrite

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [L]

mod_proxy (Reverse Proxy)

ProxyPass /api/ http://localhost:8080/
ProxyPassReverse /api/ http://localhost:8080/
ProxyPreserveHost On
ProxyPass /ws/ ws://localhost:8080/ws/

.htaccess Performance

Aspect.htaccessCentral Config
PerformanceSlower (read per request)Fast (parsed once)
FlexibilityPer-directoryServer-wide
Restart neededNoYes
SecurityUser-controllableAdmin-only
RecommendationDisable in productionUse AllowOverride None

Modules

ModulePurpose
mod_rewriteURL rewriting
mod_proxyReverse proxy
mod_sslTLS/SSL
mod_deflateCompression
mod_expiresCache headers
mod_securityWAF
mod_phpPHP processing (prefork only)
mod_fastcgiFastCGI proxy

References