以下是在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
工具),并定期备份配置文件与网站数据。安全配置需结合业务需求持续优化,定期更新系统和模块以应对新威胁。