以下是Debian Apache配置的最佳實踐,涵蓋安裝、性能、安全及維護等方面:
系統更新與軟件安裝
sudo apt update && sudo apt install apache2
sudo systemctl start apache2 && sudo systemctl enable apache2
虛擬主機配置
/etc/apache2/sites-available/
下創建配置文件(如example.com.conf
),指定DocumentRoot
、ServerName
等,啟用時使用sudo a2ensite
。sudo a2dissite 000-default.conf
。模塊管理
rewrite
、deflate
):sudo a2enmod rewrite deflate
。autoindex
)以減少資源占用。緩存與壓縮
mod_cache
和mod_expires
緩存靜態資源:<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 month"
</IfModule>
mod_deflate
壓縮內容:sudo a2enmod deflate
。連接優化
KeepAlive
參數:KeepAlive On
,KeepAliveTimeout 5
。MaxClients
和MaxRequestsPerChild
,避免內存耗盡(根據服務器內存調整)。訪問控制
Options -Indexes
。<Directory>
指令限制IP訪問或啟用密碼認證。SSL/TLS配置
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com
```。
ServerSignature Off
,ServerTokens Prod
。防火墻與日志
ufw
限制端口:sudo ufw allow 'Apache Full'
。/var/log/apache2/access.log
和error.log
,監控異常請求。定期更新
sudo apt update && sudo apt upgrade -y
備份策略
sudo cp -R /etc/apache2 /etc/apache2-backup
。性能監控
htop
、top
監控資源使用,調整MaxClients
等參數避免過載。prefork
切換至event
或worker
模塊,提升并發能力。參考來源:[1,2,3,4,5,6,7,8,9,10]