Apache2 安全加固清单与实践
一 基础加固
sudo apt update && sudo apt upgrade;在 CentOS/RHEL 执行 sudo yum update,及时修补漏洞。sudo a2dismod autoindex)。Options -Indexes)。<Directory> 内):Require all granted 基础上增加 Require ip 203.0.113.0/24(按需调整)。LimitRequestBody 10485760(示例为 10MB)。二 传输加密与端口管理
sudo apt install certbot python3-certbot-apachesudo certbot --apache -d yourdomain.com -d www.yourdomain.comsudo ufw allow 'Apache Full' && sudo ufw enablesudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload三 请求与响应安全
X-Content-Type-Options: nosniffX-Frame-Options: SAMEORIGINX-XSS-Protection: 1; mode=blockReferrer-Policy: no-referrer-when-downgradeContent-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; object-src 'none';(按站点实际调整策略)Options -Indexes);限制上传与请求体大小(LimitRequestBody);对敏感目录使用认证:<Directory "/var/www/html/admin">
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
四 模块与入侵防护
sudo apt install libapache2-mod-security2 && sudo systemctl restart apache2(规则需按业务定制)。sudo apt install fail2ban && sudo systemctl enable --now fail2ban。五 日志监控与运维
LogLevel warnErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combinedtail -f、集中化日志平台或 SIEM 进行异常检测。php.ini 中禁用危险函数(如 exec、system、shell_exec),并通过 open_basedir 限制可访问目录。