以下是在CentOS中防止Apache2被攻击的关键措施:
系统与软件更新
定期更新系统和Apache软件包,修补已知漏洞:
sudo yum update -y 配置防火墙
使用firewalld限制仅开放HTTP(80)和HTTPS(443)端口:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload 禁用不必要的模块
编辑/etc/httpd/conf/httpd.conf,关闭不需要的模块(如mod_rewrite等):
# 示例:禁用重写引擎 RewriteEngine Off 启用安全模块
隐藏敏感信息
httpd.conf隐藏Apache版本和签名:ServerTokens Prod ServerSignature Off Options -Indexes 强化认证与加密
.htaccess或配置文件限制访问。权限与资源控制
www-data)运行Apache,限制文件权限:sudo chown -R www-data:www-data /var/www/html sudo chmod -R 755 /var/www/html 日志与监控
启用详细日志记录,定期分析异常行为:
LogLevel warn ErrorLog /var/log/httpd/error_log CustomLog /var/log/httpd/access_log combined DDoS防护
mod_ratelimit限制IP请求速率。定期审计与备份
定期扫描配置漏洞,备份配置文件以便快速恢复。
参考来源: