在CentOS上设置Apache2的安全配置,可以遵循以下步骤:
关闭SELinux和防火墙:
sudo setenforce 0
sudo nano /etc/selinux/config
将SELINUXenforcing
改为SELINUXdisabled
,然后保存退出。sudo systemctl stop firewalld
sudo systemctl disable firewalld
安装Apache和SSL模块:
sudo yum install httpd mod_ssl
配置Apache:
/etc/httpd/conf/httpd.conf
,设置用户和用户组为 webuser
:User webuser
Group webuser
/etc/systemd/system/multi-user.target.wants/httpd.service
文件,限制Apache使用的内存:memory_limit_in_bytes=500*1024*1024
然后重启Apache:sudo systemctl daemon-reload
sudo systemctl restart httpd
启用TLS访问:
sudo openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout /etc/pki/tls/private/apache-selfsigned.key -out /etc/pki/tls/certs/apache-selfsigned.crt
/etc/httpd/conf.d/ssl.conf
中添加或修改以下内容:VirtualHost *:443
ServerName www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/pki/tls/private/apache-selfsigned.key
SSLCertificateChainFile /etc/pki/tls/certs/ca-bundle.crt
Directory "/var/www/html"
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
配置防火墙:
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
隐藏Apache版本信息:
/etc/httpd/conf/httpd.conf
中添加或修改以下行:ServerTokens Prod
ServerSignature Off
创建认证口令文件:
htpasswd
命令创建认证口令文件:sudo htpasswd -cm /etc/httpd/conf/passwd users
httpd.conf
中配置认证:Directory "/var/www/html"
AuthName "Restricted Access"
AuthType Basic
AuthUserFile /etc/httpd/conf/passwd
Require valid-user
重启Apache服务:
sudo systemctl restart httpd