以下是防止CentOS Apache2遭受攻击的关键措施:
系统与软件更新
定期更新系统和Apache及其模块,修补已知漏洞:
sudo yum update
禁用不必要的模块与服务
编辑/etc/httpd/conf/httpd.conf,关闭不需要的模块(如mod_cgi、mod_status):
# 示例:禁用mod_cgi
<IfModule mod_cgi.c>
LoadModule cgi_module modules/mod_cgi.so
</IfModule>
配置SSL/TLS加密
安装mod_ssl,配置HTTPS并使用有效证书:
sudo yum install mod_ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/pki/tls/private/server.key -out /etc/pki/tls/certs/server.crt
在配置文件中启用SSL:
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/server.crt
SSLCertificateKeyFile /etc/pki/tls/private/server.key
防火墙规则配置
使用firewalld允许HTTP(80)和HTTPS(443)流量:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
隐藏服务器信息
修改配置文件隐藏版本信息,减少被针对性攻击的风险:
ServerTokens Prod
ServerSignature Off
访问控制与权限管理
<Directory /var/www/html>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
启用安全模块
强化用户与认证安全
sudo操作。日志监控与审计
启用详细日志记录,定期分析异常行为:
ErrorLog /var/log/httpd/error_log
CustomLog /var/log/httpd/access_log combined
其他高级防护
X-XSS-Protection、Content-Security-Policy)。通过以上措施可显著提升CentOS Apache2的安全性,建议定期审查配置并关注安全漏洞公告。