Top Banner
← Back to Blog

Apache Cheat Sheet

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 apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2
sudo systemctl status apache2
sudo systemctl enable apache2

Configuration Validation

sudo apachectl configtest
sudo apachectl -S

Important 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 apache2

SSL Certificate (Let's Encrypt)

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com

Redirect 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 apache2

Security Headers

Header always set X-Frame-Options SAMEORIGIN
Header always set X-Content-Type-Options nosniff
Header always set Referrer-Policy strict-origin

Enable 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.log
tail -f /var/log/apache2/error.log

Troubleshooting Commands

sudo apachectl configtest
sudo journalctl -u apache2 -f
curl -I https://example.com
netstat -tulpn | grep :80
netstat -tulpn | grep :443

Apache Optimization With CloudRevol

CloudRevol's AcceleronX Hosting Stack includes Apache tuning, compression, browser caching, SSL optimization, security hardening, database optimization and proactive monitoring.

πŸš€ Performance Tuning

πŸ›‘ Security Hardening

πŸ”„ Free Migration