以下是在CentOS上进行Apache2安全设置的关键步骤:
更新系统与软件
sudo yum update -y
sudo yum install httpd mod_ssl mod_security mod_evasive -y # 安装Apache及安全模块
配置防火墙
开放HTTP(80)和HTTPS(443)端口:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
隐藏Apache版本信息
编辑/etc/httpd/conf/httpd.conf,设置:
ServerTokens Prod
ServerSignature Off
启用SSL/TLS加密
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.crt
/etc/httpd/conf.d/ssl.conf,启用SSL并指定证书路径:<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/pki/tls/private/apache-selfsigned.key
</VirtualHost>
配置安全模块
/etc/httpd/conf.d/security2.conf,启用规则引擎:SecRuleEngine On
SecAuditLog /var/log/httpd/modsec_audit.log
/etc/httpd/conf.d/evasive.conf,设置防DoS参数:<IfModule mod_evasive20.c>
DOSHashTableSize 3097
DOSPageCount 2
DOSSiteCount 50
DOSBlockingPeriod 10
</IfModule>
限制访问权限
Options -Indexes。.htaccess或主配置文件限制IP访问:<Directory "/var/www/html">
Require ip 192.168.1.0/24
</Directory>
优化用户权限
确保Apache以apache用户/组运行,网站目录权限设置为750:
sudo chown -R apache:apache /var/www/html
sudo chmod -R 750 /var/www/html
定期更新与监控
sudo yum update修复漏洞。/var/log/httpd/access_log和/var/log/httpd/error_log。注:若使用SELinux,需确保其策略允许Apache访问必要资源,可通过sudo setsebool -P httpd_can_network_connect 1调整。
以上步骤综合自,可根据实际需求调整。