温馨提示×

debian apache怎么防攻击

小樊
34
2026-09-24 19:49:33
栏目: 网络安全

在 Debian 上运行 Apache 时,防止攻击通常需要系统层 + Web 层 + 应用层一起做。下面给你一套实用、可落地的方案(从基础到进阶)。


一、系统层基础防护

1. 及时更新系统

apt update && apt upgrade -y

开启自动安全更新:

apt install unattended-upgrades
dpkg-reconfigure --priority=low unattended-upgrades

2. 使用防火墙(ufw)

apt install ufw
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable

3. 修改 SSH 防护

  • 禁止 root 登录
  • 使用密钥登录
  • 改端口(可选)

编辑:

nano /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
systemctl restart ssh

二、Apache 安全配置

1. 隐藏 Apache 版本信息

nano /etc/apache2/conf-enabled/security.conf

修改:

ServerTokens Prod
ServerSignature Off

2. 禁用目录浏览

<Directory /var/www/html>
    Options -Indexes
</Directory>

3. 限制危险请求方法

<Directory /var/www/html>
    AllowOverride All
    Require all granted
</Directory>

<LimitExcept GET POST HEAD>
    Require all denied
</LimitExcept>

三、防恶意请求 / CC / 扫描

1. 安装并启用 mod_evasive(防 CC)

apt install libapache2-mod-evasive

配置:

nano /etc/apache2/mods-enabled/evasive.conf

示例:

DOSHashTableSize 3097
DOSPageCount 10
DOSSiteCount 50
DOSPageInterval 1
DOSSiteInterval 1
DOSBlockingPeriod 60

2. 安装 mod_security(WAF)

apt install libapache2-mod-security2

启用:

a2enmod security2

使用 OWASP 规则:

apt install modsecurity-crs

编辑:

nano /etc/modsecurity/modsecurity.conf
SecRuleEngine On

四、限制访问与频率(推荐)

1. 使用 fail2ban

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

五、HTTPS 与证书

1. 使用 Let’s Encrypt

apt install certbot python3-certbot-apache
certbot --apache

2. 强制 HTTPS

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

六、应用层防护(非常重要)

  • PHP / 网站程序及时升级
  • 禁用危险函数(php.ini):
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

推荐:

  • GoAccess(实时分析)
  • Prometheus + Grafana(进阶)

八、常见攻击防护总结

攻击类型 防护手段
CC / 爬虫 mod_evasive + fail2ban
SQL 注入 / XSS mod_security
目录遍历 关闭 Indexes
暴力破解 fail2ban
0day / 漏洞 及时更新

如果你愿意,可以告诉我:

  • Debian 版本(11 / 12?)
  • Apache + PHP 还是 Apache + Python / Node?
  • 是公网服务器还是内网?

我可以给你定制一份最小安全配置。

0 踩