在Ubuntu上优化Apache2虚拟主机可以从多个方面进行,包括调整配置文件、优化性能、增强安全性等。以下是一些常见的优化步骤:
/etc/apache2/apache2.conf
或/etc/apache2/httpd.conf
KeepAlive 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虚拟主机性能和安全性。根据具体需求和环境,可能还需要进行进一步的调整和优化。