在 Debian 上运行 Apache 时,防止攻击通常需要系统层 + Web 层 + 应用层一起做。下面给你一套实用、可落地的方案(从基础到进阶)。
apt update && apt upgrade -y
开启自动安全更新:
apt install unattended-upgrades
dpkg-reconfigure --priority=low unattended-upgrades
apt install ufw
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
编辑:
nano /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
systemctl restart ssh
nano /etc/apache2/conf-enabled/security.conf
修改:
ServerTokens Prod
ServerSignature Off
<Directory /var/www/html>
Options -Indexes
</Directory>
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
<LimitExcept GET POST HEAD>
Require all denied
</LimitExcept>
apt install libapache2-mod-evasive
配置:
nano /etc/apache2/mods-enabled/evasive.conf
示例:
DOSHashTableSize 3097
DOSPageCount 10
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 60
apt install libapache2-mod-security2
启用:
a2enmod security2
使用 OWASP 规则:
apt install modsecurity-crs
编辑:
nano /etc/modsecurity/modsecurity.conf
SecRuleEngine On
apt install fail2ban
Apache 示例:
nano /etc/fail2ban/jail.local
[apache-auth]
enabled = true
[apache-botsearch]
enabled = true
[apache-ddos]
enabled = true
port = http,https
filter = apache-ddos
logpath = /var/log/apache2/access.log
maxretry = 100
findtime = 60
bantime = 3600
apt install certbot python3-certbot-apache
certbot --apache
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
disable_functions = exec,passthru,shell_exec,system
<Directory /var/www/html/uploads>
php_flag engine off
</Directory>
tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log
推荐:
| 攻击类型 | 防护手段 |
|---|---|
| CC / 爬虫 | mod_evasive + fail2ban |
| SQL 注入 / XSS | mod_security |
| 目录遍历 | 关闭 Indexes |
| 暴力破解 | fail2ban |
| 0day / 漏洞 | 及时更新 |
如果你愿意,可以告诉我:
我可以给你定制一份最小安全配置。