温馨提示×

centos apache怎样防攻击

小樊
49
2026-09-30 03:07:16
栏目: 网络安全

在 CentOS + Apache 环境下,防攻击通常需要从 系统层、Apache 配置层、应用层、网络层 多维度来做。下面按“实用优先”的方式给你一套常见方案。


一、系统层基础防护

1. 及时更新系统

yum update -y

2. 禁用不必要的服务

systemctl list-unit-files --type=service
systemctl disable telnet

3. 使用防火墙(firewalld / iptables)

systemctl start firewalld
systemctl enable firewalld

# 只开放必要端口
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

4. 修改 SSH 防爆破

  • 修改默认端口
  • 禁止 root 登录
  • 使用密钥登录
vi /etc/ssh/sshd_config
Port 2222
PermitRootLogin no
PasswordAuthentication no

二、Apache 安全加固

1. 隐藏 Apache 版本信息

vi /etc/httpd/conf/httpd.conf
ServerTokens Prod
ServerSignature Off

重载:

systemctl reload httpd

2. 限制请求大小(防大包攻击)

LimitRequestBody 10485760   # 10MB

3. 限制并发连接(防 CC / DOS)

<IfModule mpm_prefork_module>
    MaxClients 150
    ServerLimit 150
</IfModule>

或使用 mod_reqtimeout:

RequestReadTimeout header=10-20,MinRate=500

4. 关闭目录浏览

Options -Indexes

三、防 Web 攻击(SQL注入 / XSS / 扫描)

1. 安装 mod_security(强烈推荐)

yum install mod_security -y
systemctl restart httpd

启用规则:

yum install mod_security_crs -y

编辑:

vi /etc/httpd/conf.d/mod_security.conf
SecRuleEngine On

2. 使用 mod_evasive(防 CC 攻击)

yum install mod_evasive -y

配置示例:

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

四、防恶意扫描 & IP 封禁

1. 使用 fail2ban

yum install epel-release -y
yum install fail2ban -y

示例(Apache 登录暴力):

vi /etc/fail2ban/jail.d/httpd.conf
[httpd-auth]
enabled = true
filter = httpd-auth
logpath = /var/log/httpd/error_log
maxretry = 5
bantime = 3600

启动:

systemctl enable fail2ban
systemctl start fail2ban

2. 手动封 IP

firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=1.2.3.4 reject'
firewall-cmd --reload

五、HTTPS & 安全头

1. 启用 HTTPS(Let’s Encrypt)

yum install certbot python2-certbot-apache -y
certbot --apache

2. 添加安全响应头

Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "DENY"
Header always set X-XSS-Protection "1; mode=block"

六、应用层建议

  • 定期更新网站程序(WordPress / 框架等)
  • 不使用弱密码
  • 上传目录禁止执行脚本
<Directory "/var/www/html/uploads">
    php_admin_flag engine off
</Directory>

七、监控与日志

tail -f /var/log/httpd/access_log
grep "404" /var/log/httpd/access_log | wc -l

可配合:

  • goaccess
  • atop
  • netdata

总结(最小必做)

✅ 防火墙
✅ 隐藏版本
✅ mod_security
✅ mod_evasive
✅ fail2ban
✅ HTTPS

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

  • CentOS 版本(7 / 8 / Stream)
  • Apache 版本
  • 是否跑 WordPress / 自建程序
  • 主要担心哪类攻击(CC / 注入 / 爆破)

我可以给你针对性的配置模板。

0 踩