Configure Apache rate limiting and DDoS protection with mod_security and mod_evasive

Intermediate 25 min Apr 06, 2026 72 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up comprehensive Apache protection against DDoS attacks and malicious traffic using mod_evasive for rate limiting, mod_security with OWASP rules for web application firewall capabilities, and fail2ban integration for automated IP blocking.

Prerequisites

  • Root or sudo access
  • Apache web server
  • Internet connection for package installation
  • Basic knowledge of Apache configuration

What this solves

This tutorial helps you protect your Apache web server from DDoS attacks, brute force attempts, and malicious traffic patterns. You'll implement multiple layers of protection including rate limiting, web application firewall rules, and automated IP blocking to maintain server availability under attack conditions.

Step-by-step installation

Update system packages

Start by updating your package manager to ensure you get the latest security patches and module versions.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install Apache and security modules

Install Apache web server along with mod_evasive for DDoS protection and mod_security for web application firewall capabilities.

sudo apt install -y apache2 libapache2-mod-evasive libapache2-mod-security2 modsecurity-crs
sudo dnf install -y httpd mod_evasive mod_security mod_security_crs

Install mod_limitipconn for connection limiting

Add mod_limitipconn to limit the number of simultaneous connections per IP address, providing additional protection against connection flooding attacks.

sudo apt install -y libapache2-mod-limitipconn
sudo dnf install -y mod_limitipconn

Install fail2ban for automated IP blocking

Install fail2ban to automatically block IP addresses that exhibit suspicious behavior patterns detected in Apache logs.

sudo apt install -y fail2ban
sudo dnf install -y fail2ban

Enable Apache security modules

Enable the installed security modules in Apache configuration. These modules will be loaded when Apache starts.

sudo a2enmod evasive
sudo a2enmod security2
sudo a2enmod limitipconn
sudo a2enmod headers
sudo a2enmod rewrite
sudo systemctl enable httpd

Configure mod_evasive for DDoS protection

Create mod_evasive configuration to limit request rates and protect against DoS attacks. This configuration sets thresholds for page requests and site requests per second.


    DOSHashTableSize     512
    DOSPageCount         3
    DOSPageInterval      1
    DOSSiteCount         50
    DOSSiteInterval      1
    DOSBlockingPeriod    600
    DOSLogDir            "/var/log/apache2"
    DOSEmailNotify       admin@example.com
    DOSWhitelist         127.0.0.1
    DOSWhitelist         203.0.113.0/24

    DOSHashTableSize     512
    DOSPageCount         3
    DOSPageInterval      1
    DOSSiteCount         50
    DOSSiteInterval      1
    DOSBlockingPeriod    600
    DOSLogDir            "/var/log/httpd"
    DOSEmailNotify       admin@example.com
    DOSWhitelist         127.0.0.1
    DOSWhitelist         203.0.113.0/24

Configure mod_security with OWASP Core Rule Set

Set up mod_security with the OWASP Core Rule Set to protect against web application attacks including SQL injection, XSS, and other common vulnerabilities.

sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
sudo cp /etc/httpd/modsecurity.d/modsecurity.conf-recommended /etc/httpd/modsecurity.d/modsecurity.conf

Configure mod_security rules

Enable mod_security in blocking mode and configure logging for security events. This configuration activates the web application firewall protection.

sudo sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/modsecurity/modsecurity.conf
sudo sed -i 's/SecAuditLogParts ABIJDEFHZ/SecAuditLogParts ABCEFHJKZ/' /etc/modsecurity/modsecurity.conf
sudo sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/httpd/modsecurity.d/modsecurity.conf
sudo sed -i 's/SecAuditLogParts ABIJDEFHZ/SecAuditLogParts ABCEFHJKZ/' /etc/httpd/modsecurity.d/modsecurity.conf

Create Apache security configuration

Create a comprehensive security configuration file that includes mod_security, mod_evasive, and mod_limitipconn settings for your virtual hosts.

# ModSecurity Configuration
SecRuleEngine On
SecRequestBodyAccess On
SecRequestBodyLimit 13107200
SecRequestBodyNoFilesLimit 131072
SecRequestBodyInMemoryLimit 131072
SecRequestBodyLimitAction Reject
SecResponseBodyAccess Off
SecResponseBodyMimeType text/plain text/html text/xml
SecResponseBodyLimit 524288
SecResponseBodyLimitAction ProcessPartial
SecTmpDir /tmp/
SecDataDir /tmp/

Rate Limiting with mod_limitipconn

LoadModule limitipconn_module modules/mod_limitipconn.so MaxConnPerIP 20 NoIPLimit image/*

Security Headers

Header always set X-Frame-Options DENY Header always set X-Content-Type-Options nosniff Header always set X-XSS-Protection "1; mode=block" Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" Header always set Referrer-Policy "strict-origin-when-cross-origin" Header always set Content-Security-Policy "default-src 'self'"

Hide Apache Version

ServerTokens Prod ServerSignature Off
# ModSecurity Configuration
SecRuleEngine On
SecRequestBodyAccess On
SecRequestBodyLimit 13107200
SecRequestBodyNoFilesLimit 131072
SecRequestBodyInMemoryLimit 131072
SecRequestBodyLimitAction Reject
SecResponseBodyAccess Off
SecResponseBodyMimeType text/plain text/html text/xml
SecResponseBodyLimit 524288
SecResponseBodyLimitAction ProcessPartial
SecTmpDir /tmp/
SecDataDir /tmp/

Rate Limiting with mod_limitipconn

LoadModule limitipconn_module modules/mod_limitipconn.so MaxConnPerIP 20 NoIPLimit image/*

Security Headers

Header always set X-Frame-Options DENY Header always set X-Content-Type-Options nosniff Header always set X-XSS-Protection "1; mode=block" Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" Header always set Referrer-Policy "strict-origin-when-cross-origin" Header always set Content-Security-Policy "default-src 'self'"

Hide Apache Version

ServerTokens Prod ServerSignature Off

Enable security configuration

Enable the security hardening configuration and create necessary directories for mod_evasive logging.

sudo a2enconf security-hardening
sudo mkdir -p /var/log/mod_evasive
sudo chown www-data:www-data /var/log/mod_evasive
sudo chmod 755 /var/log/mod_evasive
sudo mkdir -p /var/log/mod_evasive
sudo chown apache:apache /var/log/mod_evasive
sudo chmod 755 /var/log/mod_evasive

Configure fail2ban for Apache protection

Set up fail2ban jails to automatically block IP addresses that trigger mod_security rules or show suspicious behavior patterns in Apache logs.

[apache-auth]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache2/error.log
maxretry = 3
bantime = 3600
findtime = 600

[apache-badbots]
enabled = true
port = http,https
filter = apache-badbots
logpath = /var/log/apache2/access.log
maxretry = 2
bantime = 86400
findtime = 600

[apache-noscript]
enabled = true
port = http,https
filter = apache-noscript
logpath = /var/log/apache2/access.log
maxretry = 3
bantime = 3600
findtime = 600

[apache-overflows]
enabled = true
port = http,https
filter = apache-overflows
logpath = /var/log/apache2/access.log
maxretry = 2
bantime = 3600
findtime = 600

[apache-modsecurity]
enabled = true
port = http,https
filter = apache-modsecurity
logpath = /var/log/apache2/error.log
maxretry = 2
bantime = 86400
findtime = 600

Create custom fail2ban filter for mod_security

Create a custom filter to detect mod_security rule violations and automatically block attacking IP addresses.

[Definition]
failregex = ^.\[error\].\[client \] ModSecurity:.*$
            ^.\[error\].\[client \] ModSecurity: Warning.*$
            ^.\[error\].\[client \] ModSecurity: Access denied.*$

ignoreregex =

[INCLUDES]
before = common.conf

Start and enable services

Start Apache and fail2ban services, then enable them to start automatically on system boot.

sudo systemctl restart apache2
sudo systemctl enable apache2
sudo systemctl restart fail2ban
sudo systemctl enable fail2ban
sudo systemctl restart httpd
sudo systemctl enable httpd
sudo systemctl restart fail2ban
sudo systemctl enable fail2ban

Configure firewall rules

Open HTTP and HTTPS ports in your firewall while maintaining security. This allows legitimate web traffic while blocking unauthorized access.

sudo ufw allow 'Apache Full'
sudo ufw enable
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Verify your setup

Check that all security modules are loaded and services are running properly.

sudo apache2ctl -M | grep -E '(evasive|security|limitipconn)'
sudo systemctl status apache2
sudo systemctl status fail2ban
sudo fail2ban-client status
sudo httpd -M | grep -E '(evasive|security|limitipconn)'
sudo systemctl status httpd
sudo systemctl status fail2ban
sudo fail2ban-client status

Test mod_evasive protection by making rapid requests:

for i in {1..10}; do curl -I http://your-server-ip/; done

Check security logs for mod_security events:

sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/apache2/modsec_audit.log
sudo tail -f /var/log/httpd/error_log
sudo tail -f /var/log/httpd/modsec_audit.log

Common issues

Symptom Cause Fix
Apache fails to start Module configuration error Check sudo apache2ctl configtest for syntax errors
mod_security blocks legitimate traffic OWASP rules too strict Review and whitelist specific rule IDs in mod_security config
mod_evasive not blocking requests Incorrect threshold settings Lower DOSPageCount and DOSSiteCount values
fail2ban not detecting attacks Log path mismatch Verify log paths in jail configuration match actual Apache log locations
High false positives in blocking Aggressive rate limiting Increase MaxConnPerIP and adjust mod_evasive thresholds
ModSecurity audit log not created Permission issues Ensure Apache user can write to log directory with chown www-data:www-data
Never use chmod 777. It gives every user on the system full access to your files. Instead, fix ownership with chown and use minimal permissions like 755 for directories and 644 for files.

Next steps

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle infrastructure security hardening for businesses that depend on uptime. From initial setup to ongoing operations.