在Ubuntu上優化Apache2虛擬主機可以從多個方面進行,包括調整配置文件、優化性能、增強安全性等。以下是一些常見的優化步驟:
/etc/apache2/apache2.conf或/etc/apache2/httpd.confKeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
MaxClients 150
StartServers 5
MinSpareServers 5
MaxSpareServers 10
通常位于/etc/apache2/sites-available/目錄下,例如yourdomain.com.conf。
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
mod_expires模塊設置靜態內容的過期時間。<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
</IfModule>
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /yourdomain.com
CacheRoot /var/cache/apache2/mod_cache_disk
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
<IfModule mod_socache.c>
<IfModule mod_socache_shmcb.c>
SocacheEnable shmcb /yourdomain.com
SocacheShmcbSize 5000000
SocacheShmcbTtl 300
</IfModule>
</IfModule>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /path/to/your/certificate.crt
SSLCertificateKeyFile /path/to/your/private.key
SSLCertificateChainFile /path/to/your/chainfile.pem
# 其他配置...
</VirtualHost>
ufw或iptables配置防火墻規則,限制不必要的訪問。sudo ufw allow 'Apache Full'
sudo apt update && sudo apt upgrade
sudo chown -R www-data:www-data /var/www/yourdomain.com
sudo chmod -R 755 /var/www/yourdomain.com
通過以上步驟,你可以顯著優化Ubuntu上的Apache2虛擬主機性能和安全性。根據具體需求和環境,可能還需要進行進一步的調整和優化。