1. 更新系统与Apache软件包
定期更新CentOS系统和Apache相关软件包,修复已知安全漏洞。执行命令:sudo yum update -y。
2. 配置防火墙限制访问
使用firewalld仅允许HTTP(80端口)和HTTPS(443端口)流量,阻断非法访问。操作步骤:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
```。
**3. 安装并配置安全模块**
- **mod_security**:启用Web应用防火墙(WAF),防御SQL注入、XSS等常见攻击。安装后编辑`/etc/httpd/conf.d/security2.conf`,开启规则引擎:
```apache
SecRuleEngine On
SecAuditLog /var/log/httpd/security_audit.log
/etc/httpd/conf.d/evasive.conf,设置阈值:DOSHashTableSize 3097
DOSPageCount 2
DOSSiteCount 50
DOSBlockingPeriod 10
```。
4. 隐藏敏感信息
修改Apache配置文件(/etc/httpd/conf/httpd.conf),关闭版本号显示和服务器签名,防止攻击者利用信息针对性攻击:
ServerTokens Prod
ServerSignature Off
```。
**5. 禁用不必要模块**
通过注释或删除`/etc/httpd/conf/httpd.conf`中不需要的模块(如`mod_cgi`、`mod_autoindex`),减少攻击面。例如禁用`mod_autoindex`(防止目录列表):
```apache
# LoadModule autoindex_module modules/mod_autoindex.so
```。
**6. 强化访问控制**
- **限制目录访问**:在配置文件中设置`Options -Indexes`(禁用目录列表),并通过`Require`指令限制IP访问。例如:
```apache
<Directory /var/www/html>
Options -Indexes
Require ip 192.168.1.0/24
</Directory>
AllowOverride All并创建.htaccess文件。7. 配置SSL/TLS加密
使用mod_ssl模块启用HTTPS,加密数据传输。步骤:
sudo yum install mod_ssl -ysudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/pki/tls/private/apache-selfsigned.key -out /etc/pki/tls/certs/apache-selfsigned.crt
/etc/httpd/conf.d/ssl.conf,配置虚拟主机:<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/pki/tls/private/apache-selfsigned.key
DocumentRoot /var/www/html
</VirtualHost>
sudo systemctl restart httpd。8. 配置SELinux策略
若SELinux处于Enforcing模式,调整策略允许Apache访问必要资源:
sudo setsebool -P httpd_can_network_connect 1sudo setsebool -P httpd_can_sendmail 1getenforce(应为Enforcing)。9. 监控与日志记录
/etc/httpd/conf/httpd.conf中配置ErrorLog和CustomLog,记录错误和访问信息:ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
fail2ban)识别并阻断恶意IP。10. 限制资源使用
通过LimitRequestBody指令限制上传文件大小(如50MB),防止资源耗尽攻击:
<Directory /var/www/html>
LimitRequestBody 52428800
</Directory>
```。