Apache
Apache cheat sheet for virtual hosts, .htaccess rules, mod_rewrite, SSL setup, modules, and performance tuning with config examples.
Sign in to mark items as known and track your progress.
Sign inInstallation & Basic Commands
Installing and managing Apache
Installation
Installing Apache on different platforms
# Ubuntu/Debian
sudo apt update
sudo apt install apache2
# CentOS/RHEL/Fedora
sudo yum install httpd
# macOS with Homebrew
brew install httpd
# Check version
apache2 -v # Debian/Ubuntu
httpd -v # CentOS/RHELCommands & Configuration
Service commands, config testing, and configuration file locations
# Start / stop / restart
sudo systemctl start apache2
sudo systemctl restart apache2
sudo systemctl reload apache2 # Graceful reload (no downtime)
# Test configuration before applying
sudo apachectl configtest # Syntax OK or error details
# Check loaded modules
apachectl -MVirtual Hosts
Configuring virtual hosts for multiple sites
Basic Virtual Host
Setting up a basic virtual host
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>SSL/TLS Configuration
Configuring HTTPS with SSL certificates
# Redirect HTTP to HTTPS (must be a sibling block - vhosts cannot nest)
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/example.com
SSLEngine on
SSLCertificateFile /path/to/cert.pem
SSLCertificateKeyFile /path/to/key.pem
</VirtualHost>Reverse Proxy
Proxy requests to backend application servers with mod_proxy
Reverse Proxy & Load Balancing
Forward requests to Node.js, Python, or other backend servers
# Enable required modules
sudo a2enmod proxy proxy_http proxy_balancer lbmethod_byrequests
# Basic reverse proxy
<VirtualHost *:80>
ServerName app.example.com
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>.htaccess & URL Rewriting
Directory-level configuration and URL rewriting
.htaccess Basics
Common .htaccess configurations
# Enable rewrite engine
RewriteEngine On
# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Custom error pages
ErrorDocument 404 /404.html
ErrorDocument 500 /500.htmlURL Rewriting
Advanced URL rewriting with mod_rewrite
# Basic rewrite
RewriteEngine On
RewriteRule ^old-page$ /new-page [R=301,L]
# Dynamic rewrite
RewriteRule ^user/([0-9]+)$ /profile.php?id=$1 [L]
# WordPress permalinks
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]Redirects & Error Pages
Simple redirects with mod_alias and custom error pages
# Permanent redirect (301)
Redirect 301 /old-page https://example.com/new-page
# Custom error pages
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.htmlLogging
Configure access logs, error logs, and custom log formats
Access & Error Logs
Configure log files, formats, and rotation
# Error log
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
# Access log with combined format
CustomLog ${APACHE_LOG_DIR}/access.log combinedSecurity & Access Control
Securing your Apache server
Authentication & Access Control
Password-protect directories and restrict access by IP
# Password-protect a directory
<Directory /var/www/admin>
AuthType Basic
AuthName "Admin Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
# Create password file
# htpasswd -c /etc/apache2/.htpasswd adminSecurity Best Practices
Essential security configurations
# Hide Apache version
ServerTokens Prod
ServerSignature Off
# Disable directory listing
Options -Indexes
# Prevent clickjacking
Header always append X-Frame-Options SAMEORIGIN
# Block access to sensitive files
<FilesMatch "^\.(htaccess|htpasswd|ini|log|sh)">
Order allow,deny
Deny from all
</FilesMatch>Headers & CORS
Set security headers and configure CORS with mod_headers
# Enable mod_headers
# sudo a2enmod headers
# Security headers
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-XSS-Protection "1; mode=block"
Header set Strict-Transport-Security "max-age=31536000"Modules & Performance
Apache modules and performance optimization
Essential Modules
Common Apache modules and their usage
# Enable modules (Debian/Ubuntu)
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod headers
sudo a2enmod deflate
# List loaded modules
apache2 -M
# Module configuration
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule ssl_module modules/mod_ssl.soPerformance Tuning
Optimizing Apache for better performance
# MPM configuration
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
# Enable caching
<IfModule mod_cache.c>
CacheEnable disk /
CacheRoot /var/cache/apache2/mod_cache_disk
</IfModule>
# KeepAlive settings
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5Caching & Compression
Enable GZIP compression and browser caching for performance
# Enable compression (mod_deflate)
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css
AddOutputFilterByType DEFLATE application/javascript application/json
</IfModule>
# Browser caching (mod_expires)
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
</IfModule>