Apache2 安全加固清单
一 基础与系统层防护
sudo ufw allow 'Apache Full';在 firewalld 中放行 http/https 服务。chown -R www-data:www-data /var/www/html,权限 755 为主,敏感文件更严格;配置与二进制目录限制为受控组访问。二 核心 Apache 配置加固
ServerTokens Prod 与 ServerSignature Off,避免泄露 Apache/OS/模块 版本信息。Options -Indexes,防止目录文件列表泄露。AllowOverride None,将访问控制集中在主配置,降低被 .htaccess 绕过的风险。<Directory /var/www/html/admin>
Options None
AllowOverride None
Require all denied
Require ip 192.168.100.0/24
Require ip 192.168.100.22
</Directory>
Order deny,allow + Deny from all,仅对业务目录显式放行。httpd -t(或 apache2ctl configtest)校验,再 systemctl restart apache2 使配置生效。三 传输加密与证书管理
sudo apt install certbot python3-certbot-apachesudo certbot --apache -d yourdomain.com -d www.yourdomain.comApache Full 或 firewalld 的 http/https 已开启,避免 443 被阻断。sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/pki/tls/private/apache-selfsigned.key -out /etc/pki/tls/certs/apache-selfsigned.crtSSLEngine on、SSLCertificateFile、SSLCertificateKeyFile 并重启服务。四 安全头与 Web 应用防护
mod_headers):<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"
Header always set Referrer-Policy "no-referrer-when-downgrade"
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; object-src 'none';"
</IfModule>
sudo apt install libapache2-mod-security2sudo a2enmod security2SecRuleEngine On、SecRequestBodyAccess On、SecAuditLog /var/log/modsec_audit.log,按需加载 OWASP Core Rule Set。五 日志监控与持续运维
configtest,变更后留回滚方案与变更记录。