以下是在Ubuntu上配置Apache安全的關鍵步驟:
系統更新與補丁管理
sudo apt update && sudo apt upgrade
防火墻配置(UFW)
sudo apt install ufw
sudo ufw enable
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
sudo ufw allow ssh
隱藏Apache敏感信息
編輯 /etc/apache2/apache2.conf
:
ServerTokens Prod
ServerSignature Off
重啟Apache:
sudo systemctl restart apache2
禁用不必要的模塊
sudo a2dismod 模塊名(如status、authz_host)
sudo systemctl restart apache2
啟用SSL/TLS加密
sudo a2enmod ssl
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/your_cert.crt
SSLCertificateKeyFile /etc/ssl/private/your_key.key
</VirtualHost>
sudo systemctl restart apache2
安裝安全模塊(如mod_security)
sudo apt install libapache2-mod-security2
sudo a2enmod security2
編輯規則文件 /etc/apache2/conf-available/security2.conf
,啟用規則集(如OWASP CRS):
SecRuleEngine On
Include /etc/apache2/conf-available/crs-setup.conf
重啟Apache:
sudo systemctl restart apache2
訪問控制與權限管理
.htaccess
或主配置文件限制IP訪問:<Directory /var/www/html>
Require ip 192.168.1.0/24
</Directory>
sudo chown -R www-data:www-data /var/www/html
sudo chmod 755 /var/www/html
日志監控與審計
sudo tail -f /var/log/apache2/access.log /var/log/apache2/error.log
可使用工具(如Logwatch)定期分析日志:
sudo apt install logwatch
sudo logwatch --output mail --mailto your_email@example.com
注:生產環境中建議使用Let’s Encrypt免費證書(certbot
工具),并定期備份配置文件與網站數據。安全配置需結合業務需求持續優化,定期更新系統和模塊以應對新威脅。