β Back to Blog
Apache Cheat Sheet

Apache HTTP Server remains one of the most widely used web servers in the world. This cheat sheet covers the most important commands, virtual hosts, SSL configuration, redirects, security hardening, troubleshooting and performance optimization.
Apache Service Commands
sudo systemctl start apache2sudo systemctl stop apache2sudo systemctl restart apache2sudo systemctl reload apache2sudo systemctl status apache2sudo systemctl enable apache2Configuration Validation
sudo apachectl configtestsudo apachectl -SImportant Apache Locations
/etc/apache2/apache2.conf
/etc/apache2/sites-available/
/etc/apache2/sites-enabled/
/var/log/apache2/access.log
/var/log/apache2/error.log
/etc/apache2/mods-available/
/etc/apache2/mods-enabled/Virtual Host Example
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>Enable Virtual Host
sudo a2ensite example.conf
sudo systemctl reload apache2SSL Certificate (Let's Encrypt)
sudo apt install certbot python3-certbot-apache -ysudo certbot --apache -d example.com -d www.example.comRedirect HTTP To HTTPS
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]Common .htaccess Rules
# Block Directory Listing
Options -Indexes
# Protect .env
<Files ".env">
Order allow,deny
Deny from all
</Files>Enable Required Modules
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod headers
sudo a2enmod expires
sudo systemctl restart apache2Security Headers
Header always set X-Frame-Options SAMEORIGIN
Header always set X-Content-Type-Options nosniff
Header always set Referrer-Policy strict-originEnable Compression
sudo a2enmod deflate
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>Browser Caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
</IfModule>Monitor Logs
tail -f /var/log/apache2/access.logtail -f /var/log/apache2/error.logTroubleshooting Commands
sudo apachectl configtestsudo journalctl -u apache2 -fcurl -I https://example.comnetstat -tulpn | grep :80netstat -tulpn | grep :443Apache Optimization With CloudRevol
CloudRevol's AcceleronX Hosting Stack includes Apache tuning, compression, browser caching, SSL optimization, security hardening, database optimization and proactive monitoring.

