以下是在Ubuntu上優化Apache安全設置的關鍵步驟:
更新系統與軟件包
定期更新系統和Apache相關包,修補安全漏洞:
sudo apt update && sudo apt upgrade
隱藏敏感信息
修改配置文件(/etc/apache2/apache2.conf
),隱藏版本信息:
ServerSignature Off
ServerTokens Prod
禁用不必要的模塊
列出已啟用模塊并禁用非必需模塊(如autoindex
):
apache2ctl -M # 查看模塊
sudo a2dismod autoindex # 禁用示例模塊
sudo systemctl restart apache2
啟用SSL/TLS加密
安裝SSL模塊并配置證書(可使用Let’s Encrypt免費證書):
sudo a2enmod ssl
sudo apt install certbot python3-certbot-apache
sudo certbot --apache # 自動生成證書并配置
配置防火墻
使用UFW限制訪問,僅允許HTTP/HTTPS流量:
sudo ufw allow 'Apache Full'
sudo ufw enable
限制訪問權限
<Directory /var/www/html>
Options -Indexes +FollowSymLinks
Require all granted
</Directory>
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
啟用安全模塊
安裝并啟用mod_security
(Web應用防火墻):
sudo apt install libapache2-mod-security2
sudo a2enmod security2
sudo systemctl restart apache2
監控與日志管理
定期查看訪問日志和錯誤日志,配置日志輪轉:
sudo tail -f /var/log/apache2/access.log
sudo nano /etc/logrotate.d/apache2 # 調整日志保留策略
強化SSH訪問(輔助安全)
禁用root登錄,使用密鑰認證:
sudo nano /etc/ssh/sshd_config
# 修改以下配置
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
sudo systemctl restart sshd
注:配置后需重啟Apache服務使更改生效,生產環境建議結合自動化工具(如Ansible)批量部署安全策略,并定期進行滲透測試。