Apache cheatsheet
Service control
| Command | Description |
|---|---|
systemctl status apache2 | Service status (Debian/Ubuntu) |
systemctl status httpd | Service status (RHEL/CentOS) |
systemctl reload apache2 | Reload config without dropping connections |
apache2ctl configtest | Test config syntax (Debian) |
httpd -t | Test config syntax (RHEL) |
Sites and modules (Debian/Ubuntu)
| Command | Description |
|---|---|
a2ensite mysite.conf | Enable a site |
a2dissite 000-default.conf | Disable a site |
a2enmod proxy proxy_http ssl | Enable modules |
a2dismod mpm_prefork | Disable a module |
apache2ctl -S | List vhosts and match order |
apache2ctl -M | List loaded modules |
Logs and debugging
| Command | Description |
|---|---|
tail -f /var/log/apache2/error.log | Follow error log (Debian) |
tail -f /var/log/httpd/error_log | Follow error log (RHEL) |
tail -f /var/log/apache2/access.log | Follow access log |
journalctl -u apache2 -f | systemd journal for Apache |
ss -tlnp | grep -E ':80|:443' | Check listeners |
curl -vI http://localhost/ | Test HTTP response headers |
Minimal virtual host example
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/example
ErrorLog ${APACHE_LOG_DIR}/example-error.log
CustomLog ${APACHE_LOG_DIR}/example-access.log combined
<Directory /var/www/example>
Require all granted
</Directory>
</VirtualHost>
Reverse proxy snippet
<VirtualHost *:80>
ServerName api.example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8000/
ProxyPassReverse / http://127.0.0.1:8000/
</VirtualHost>
Pro tips
- Always run
configtestbeforereload— a bad config can prevent startup - Use
apache2ctl -Sto see which vhost handles a given name - Check
error_logfirst — Apache logs module and permission errors there - Disable the default site on Debian if it steals traffic from your vhosts
Practice scenarios
Hands-on Apache scenarios on live Linux VMs: apache