Apache guide
What Apache does in production
Apache listens on ports (typically 80 and 443), matches each request to a
virtual host, and either serves files from disk or forwards
the request to a backend via mod_proxy. Access control, URL
rewriting, TLS termination, and compression are handled by loadable
modules. On SadServers scenarios you debug real configs:
wrong Listen ports, broken vhosts, missing modules, and
permission errors on files or document directories and analize logs.
How a request is handled
When a client connects, the typical workflow is:
- TCP accept — Apache accepts the connection on a
Listenport - TLS handshake — if HTTPS,
mod_sslnegotiates the certificate (SNI determines which certificate to present); vhost selection happens in step 3 - IP/Port match — Apache selects candidate
<VirtualHost>blocks that match the local IP address and port the client connected to - Name based virtual host match — Apache then looks at the
Hostheader and picks a<VirtualHost>usingServerName,ServerAlias - Request processing — Apache determines the handler via
DocumentRoot,Alias,ProxyPass, or rewrite rules - Access check —
Requiredirectives allow or deny the request - Response — static file, CGI, or proxied backend response is served; then logged to
access_log/error_log
If no vhost matches, the first vhost for that port is used as the default — a common source of "wrong site" bugs.
Configuration layout
Paths differ by distribution:
- Debian/Ubuntu (package: `apache2`) —
/etc/apache2/apache2.conf, sites insites-available//sites-enabled/, modules viaa2enmod/a2dismod - RHEL/CentOS/Rocky (packate: `httpd`) —
/etc/httpd/conf/httpd.conf, vhosts inconf.d/, modules inmodules/
Always run a syntax check before reload:
apache2ctl configtest (Debian) or httpd -t (RHEL).
Key concepts to know
- Virtual hosts —
IP/Portfirst, thenServerName/ServerAliasmatching against the Host header - Modules —
LoadModuledirective (ora2enmodon Debian), MPM choice (prefork/worker/event) affects performance and threading - Logs —
ErrorLogfor failures;CustomLogfor access; check these first - Reverse proxy —
ProxyPass/ProxyPassReverseto app servers (Gunicorn, Node, etc.)ProxyPreserveHostis a common pitfall - systemd — service unit
apache2orhttpd;reloadgracefully restart workers without dropping existing connections - .htaccess — per-directory overrides (if
AllowOverridepermits); slower than vhost config because Apache re-parses it per request; prefer vhost config for production
Learning resources
- Apache HTTP Server documentation — httpd.apache.org/docs/2.4 (official 2.4 reference)
- Virtual hosts — httpd.apache.org/docs/2.4/vhosts (name-based vhost guide)
- mod_proxy — httpd.apache.org/docs/2.4/mod/mod_proxy (reverse proxy directives)
- apache2ctl(8) — manpages.debian.org/apache2ctl (Debian control tool)
Practice scenarios
Hands-on Apache scenarios on live Linux VMs: apache