Securing Apache2 Web Server Against Attacks

This tutorial provides a step-by-step guide to securing your Apache2 web server against common vulnerabilities and attacks. Follow these best practice
Securing Apache2 Web Server Against Attacks
This tutorial provides a step-by-step guide to securing your Apache2 web server against common vulnerabilities and attacks. Follow these best practices to harden your server and protect it from threats.
Table of Contents
- Introduction
- Keep Apache2 Updated
- Configure Firewall
- Disable Directory Listing
- Limit Server Information
- Enable HTTPS
- Enable Security Modules
- IP Whitelisting
- Rate Limiting
- Content Security Policy (CSP)
- Enable Mod_Suohee
- Log Monitoring and Fail2ban
- Disable Unused Modules
- Additional Security Tips
- Summary
Introduction
Apache2 is a widely used open-source web server, but it can be vulnerable to attacks if not properly configured. This tutorial covers essential steps to secure your Apache2 server against common threats.
Update Apache2
Regularly updating Apache2 ensures you receive security patches and bug fixes.
sudo apt update && sudo apt upgrade apache2Configure Firewall
Restrict access to only necessary ports using ufw (Uncomplicated Firewall):
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw enableExplanation:
- SSH (port 22): Required for server access.
- HTTP (port 80) and HTTPS (port 443): For web traffic.
Disable Directory Listing
Prevent sensitive directory contents from being exposed:
Edit Apache config:
sudo nano /etc/apache2/sites-available/000-default.confAdd under <Directory /var/www/html>:
Options -IndexesApply changes:
sudo systemctl restart apache2Limit Server Information
Reduce information Apache2 reveals about itself:
Edit Apache config:
sudo nano /etc/apache2/apache2.confAdd the following lines:
ServerTokens Prod
ServerSignature OffApply changes:
sudo systemctl restart apache2Enable HTTPS
Use Let’s Encrypt to obtain a free SSL certificate and enable HTTPS:
- Install Certbot:
sudo apt install certbot python3-certbot-apache - Run Certbot:
sudo certbot --apache -d yourdomain.com
Notes:
- Redirect HTTP to HTTPS:
Add this to your virtual host config:RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Enable Security Modules
Install and enable critical security modules:
ModSecurity (Web Application Firewall)
sudo apt install libapache2-mod-security2
sudo nano /etc/modsecurity/modsecurity.confUncomment:
SecRuleEngine DetectionOnlyApply changes:
sudo systemctl restart apache2Mod_Litmod (Rate Limiting)
sudo apt install libapache2-mod-evasive
sudo a2enmod evasive
sudo systemctl restart apache2IP Whitelisting
Restrict access by IP address:
Edit Apache config:
sudo nano /etc/apache2/sites-available/000-default.confAdd:
<Limit GET POST>
Require host 192.168.1.100 # Replace with allowed IPs
</Limit>Apply changes:
sudo systemctl restart apache2Rate Limiting
Prevent brute-force attacks:
Edit /etc/modsecurity/modsecurity.conf:
- Find the
<Evasion>section and configure:
EvasionAction StatusCode 450
EvasionMaxSecRulePerIPLimit 3Restart Apache2:
sudo systemctl restart apache2Content Security Policy (CSP)
Add CSP headers to your virtual host config:
<VirtualHost *:80>
...
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'"
</VirtualHost>Notes:
- Customize policies based on your site’s needs.
- CSP protects against XSS by restricting script sources.
Enable Mod_Suohee
Block directory traversal attacks:
sudo a2enmod mod_suohee
sudo systemctl restart apache2Log Monitoring and Fail2ban
Monitor logs and block brute-force attacks:
- Install Fail2ban:
sudo apt install fail2ban - Edit the Apache jail:
sudo nano /etc/fail2ban/jail.local
Add Apache security rules:
[apache-badbots]
enabled = true
port = http,https
filter = badbots
logpath = /var/log/apache2/access.log
maxretry = 100
findtime = 3600
bantime = 86400Disable Unused Modules
Reduce attack surface by disabling unnecessary modules:
sudo a2dismod module_nameExample:
sudo a2dismod autoindex
sudo systemctl restart apache2Additional Security Tips
- File Permissions: Restrict access to sensitive files.
- Backups: Regularly back up configurations.
- HSTS: Enable HTTP Strict Transport Security (HSTS):
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" - Security Headers: Add headers like
X-Content-Type-OptionsandX-Frame-Options.
Summary
By following these steps, you will significantly enhance the security of your Apache2 server:
- Keep Apache2 updated
- Use a firewall to restrict access
- Disable directory listing
- Limit server exposure
- Enforce HTTPS
- Enable security modules (e.g., ModSecurity, Mod_Suohee)
- Monitor logs with Fail2ban
Remember: Security is an ongoing process. Regularly review your configurations and stay informed about new threats.
Question or Need Help?
Let me know if you need clarification on any of the steps! 😊

Written by Robin te Hofstee
Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.
Related Posts
Securing a Next.js Application in Production
Securing a Next.js app in production means protecting the client bundle, the server, and the data flow. This post shows how to lock down Content‑Secur
How Git Works
Git is a distributed version control system that allows multiple people to collaborate on a project while maintaining a complete history of all change
How to Check Disk Usage and Find Large Files on Linux
After setting up a new Linux server, you’ll want to know how to check what’s taking up space. Disk space is often the weakest link in system stability


