π Apache Web Server Security Guide
Securing your Apache server is critical to protect your websites, data, and infrastructure. This guide will help you lock it down using best practices. π¨π‘οΈ
πͺ 1. Disable Directory Listingβ
<Directory /var/www/html>
Options -Indexes
</Directory>
π« Prevents users from browsing directories without an index file.
π· 2. Restrict Access to Sensitive Filesβ
<FilesMatch "(\.htaccess|\.htpasswd|\.env|composer\.json)">
Require all denied
</FilesMatch>
π Blocks direct access to common sensitive config files.
π 3. Keep Apache & System Updatedβ
sudo apt update && sudo apt upgrade
π¦ Apply latest security patches regularly.
π‘οΈ 4. Use the ServerTokens
and ServerSignature
Directivesβ
ServerTokens Prod
ServerSignature Off
π Hides Apache version info from headers and error pages.
π¦ 5. Configure Firewall (UFW Example)β
sudo ufw allow 'Apache Full'
sudo ufw enable
π§± Ensures only HTTP/HTTPS traffic is allowed through the firewall.
π 6. Enable HTTPS (SSL/TLS)β
Use Certbot to enable SSL:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
π Secures traffic with free Letβs Encrypt certificates.
π« 7. Disable Unused Modulesβ
sudo a2dismod autoindex cgi status
βοΈ Reduces potential attack surface by disabling unneeded modules.
π οΈ 8. Use .htaccess
Securityβ
<FilesMatch "^.*\.(php|php5|php7|phtml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "^.*\.(bak|config|sql|ini)$">
Require all denied
</FilesMatch>
π§€ Adds extra security for PHP and config files via .htaccess
.
π 9. Monitor Logs Regularlyβ
tail -f /var/log/apache2/access.log
journalctl -xe | grep apache2
π Spot suspicious activity early with frequent log reviews.
π§ 10. Disable .htaccess if Not Neededβ
AllowOverride None
π Prevents abuse of .htaccess
if site doesnβt need it (improves performance too).
β Summary Checklistβ
Security Step | Purpose |
---|---|
π Disable Directory Index | Stop directory browsing |
π« Block Sensitive Files | Protect .env, .htaccess, etc. |
πΎ Regular Updates | Apply latest security patches |
π Hide Server Info | Disable ServerTokens and ServerSignature |
π§± UFW Firewall | Allow only HTTP and HTTPS |
π Use SSL/TLS | Encrypt all traffic with Letβs Encrypt |
βοΈ Remove Unused Modules | Limit what Apache exposes |
π οΈ Harden .htaccess | Restrict risky file types |
π Monitor Logs | Detect and respond to intrusions early |
π Disable AllowOverride | Only when .htaccess is not required |
π With these steps, your Apache server is now safer, faster, and more secure. Stay vigilant and keep your configs clean and up to date! πͺ
Would you like the next one on .htaccess.md
or performance-tuning.md
? π