ubuntu

Apache Ubuntu如何防止攻击

小樊
45
2025-12-21 00:19:41
栏目: 网络安全

Ubuntu 上加固 Apache 的实用清单

一 基础加固

二 网络与端口防护

三 抗 DoS 与 Web 应用防火墙

四 运行监控与审计

五 一键加固脚本示例

#!/usr/bin/env bash
set -e

# 0) 更新系统
apt update && apt -y upgrade

# 1) 基础加固:隐藏版本、禁用目录列表
sed -i 's/^ServerTokens .*/ServerTokens Prod/' /etc/apache2/conf-available/security.conf
sed -i 's/^ServerSignature .*/ServerSignature Off/' /etc/apache2/conf-available/security.conf
find /etc/apache2 -type f -name "*.conf" -exec sed -i '/<Directory /var/www/,\|</Directory>|{s/Options \(.*\)Indexes\(.*\)/Options \1\2/g}' {} +

# 2) 禁用不必要模块(按需保留 CGI/SSL 等)
for m in status autoindex info; do
  a2dismod "$m" 2>/dev/null || true
done

# 3) 启用 SSL 与自动证书(确保域名解析已指向本机)
a2enmod ssl
apt install -y certbot python3-certbot-apache
certbot --apache -n --agree-tos --redirect --email admin@example.com -d example.com,www.example.com || true

# 4) 抗 DoS:安装并配置 mod-evasive
apt install -y libapache2-mod-evasive
mkdir -p /var/log/apache2/evasive
chown www-data:root /var/log/apache2/evasive
cat >/etc/apache2/mods-available/evasive.conf <<'EOF'
<IfModule mod_evasive20.c>
  DOSHashTableSize 2048
  DOSPageCount        20
  DOSPageInterval     1.0
  DOSSiteCount        300
  DOSSiteInterval     1.0
  DOSBlockingPeriod  10.0
  DOSLogDir "/var/log/apache2/evasive"
</IfModule>
EOF
a2enmod evasive

# 5) 可选:WAF(mod-security2)
# apt install -y libapache2-mod-security2
# a2enmod security2
# 后续按所选规则集进行配置与调优

# 6) 检查并重载
apache2ctl configtest && systemctl reload apache2

echo "加固完成。请重启后复核:apache2ctl -M、ufw status、日志与证书状态。"

提示:脚本为通用示例,生产环境请先在测试环境验证,按业务需求调整并发、超时与 WAF 规则,并保留回滚方案。

0
看了该问题的人还看了