Ubuntu Apache2 网站内容优化实操指南
一 启用压缩与缓存
sudo a2enmod deflate headers expires/etc/apache2/conf-available/deflate.conf 并软链启用)
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
Header append Vary Accept-Encoding
</IfModule>
/etc/apache2/conf-available/expires.conf)
<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"
</IfModule>
二 启用 HTTP/2 与 KeepAlive
sudo a2enmod http2Protocols h2 http/1.1apache2.conf 的 <IfModule mpm_event_module> 或对应 MPM 段落):KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
三 内容层面优化
sudo apt update && sudo apt install certbot python3-certbot-apachesudo certbot --apache -d yourdomain.comsudo a2enmod rewriteRewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
<img> 添加 alt 属性;使用 WebP/AVIF 或工具(如 imagemin)压缩图片Sitemap: https://yourdomain.com/sitemap.xml<link rel="canonical" href="https://yourdomain.com/page-url">四 验证与上线
sudo apache2ctl configtestsudo systemctl restart apache2h2)