Skip to main content

πŸ” 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 StepPurpose
πŸ”’ Disable Directory IndexStop directory browsing
🚫 Block Sensitive FilesProtect .env, .htaccess, etc.
πŸ’Ύ Regular UpdatesApply latest security patches
πŸ™ˆ Hide Server InfoDisable ServerTokens and ServerSignature
🧱 UFW FirewallAllow only HTTP and HTTPS
πŸ” Use SSL/TLSEncrypt all traffic with Let’s Encrypt
βš™οΈ Remove Unused ModulesLimit what Apache exposes
πŸ› οΈ Harden .htaccessRestrict risky file types
πŸ“Š Monitor LogsDetect and respond to intrusions early
πŸ›‘ Disable AllowOverrideOnly 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? πŸš€