温馨提示×

debian apache怎样防攻击

小樊
42
2026-10-27 00:11:14
栏目: 网络安全

在 Debian 上运行 Apache 时,防止攻击通常需要系统层 + Web 层 + 应用层一起做。下面按实用优先级给你一套方案。


一、基础系统安全

1. 及时更新系统

apt update
apt upgrade -y

定期打补丁,避免已知漏洞。

2. 使用非 root 用户管理

  • 禁止 root 直接 SSH
  • 使用 sudo 用户

3. 防火墙(必做)

使用 ufw:

apt install ufw
ufw default deny incoming
ufw allow 22
ufw allow 80
ufw allow 443
ufw enable

二、Apache 自身加固

1. 隐藏版本信息

编辑:

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

修改:

ServerTokens Prod
ServerSignature Off

2. 限制目录访问

避免列目录:

<Directory /var/www/>
    Options -Indexes
    AllowOverride All
</Directory>

3. 禁用不必要的模块

a2dismod autoindex status cgi

三、防常见 Web 攻击

1. 安装 ModSecurity(强烈推荐)

Web 应用防火墙(WAF)

apt install libapache2-mod-security2
a2enmod security2

启用规则:

cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf

编辑:

SecRuleEngine On

重启:

systemctl restart apache2

2. 防 DDOS / 暴力请求

使用 mod_evasive:

apt install libapache2-mod-evasive
a2enmod evasive

配置示例:

<IfModule mod_evasive20.c>
    DOSHashTableSize 3097
    DOSPageCount 10
    DOSSiteCount 50
    DOSBlockingPeriod 60
</IfModule>

四、SSL / HTTPS 安全

1. 使用 Let’s Encrypt

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

2. 强加密配置

SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite HIGH:!aNULL:!MD5

五、限制访问与限速

1. 限制 IP(可选)

<Directory /admin>
    Require ip 1.2.3.4
</Directory>

2. 请求限速

a2enmod ratelimit
<Location />
    SetOutputFilter RATE_LIMIT
    SetEnv rate-limit 100
</Location>

六、日志与监控

1. 查看攻击日志

tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log

2. 使用 fail2ban

apt install fail2ban

可自动封 IP。


七、应用层(非常重要)

  • 网站代码及时更新
  • 不使用弱密码
  • 上传文件严格校验
  • 数据库防 SQL 注入

总结(最小必做项)

✅ 防火墙
✅ 隐藏 Apache 信息
✅ ModSecurity
✅ HTTPS
✅ 定期更新

如果你愿意,我可以:

  • 给你一份 完整 Apache 安全配置文件
  • 或针对 WordPress / Laravel / 自建 API 单独优化

0 踩