在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