Debian Apache2 增强 SEO 的实操清单
一 基础配置与 HTTPS
- 启用核心模块:URL 重写与 SSL,分别执行命令:sudo a2enmod rewrite、sudo a2enmod ssl。
- 创建虚拟主机(示例:/etc/apache2/sites-available/example.com.conf),设置 ServerName、DocumentRoot,并在 中允许 AllowOverride All 以便使用 .htaccess 做 URL 美化。
- 启用站点:sudo a2ensite example.com.conf && sudo systemctl reload apache2。
- 部署免费 HTTPS:安装 certbot python3-certbot-apache,执行 sudo certbot --apache -d example.com -d www.example.com,按提示自动配置 HTTP→HTTPS 跳转与证书续期。
- 安全与合规:在 <VirtualHost *:443> 中配置 SSLEngine on 与证书路径;隐藏版本信息(如 ServerTokens Prod、ServerSignature Off);对外仅开放 80/443(如 sudo ufw allow 80,443/tcp && sudo ufw enable)。
二 性能优化 直接影响 Core Web Vitals
- 启用压缩(mod_deflate):执行 sudo a2enmod deflate,在配置中加入:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json application/xml image/svg+xml
验证:响应头出现 Content-Encoding: gzip。
- 浏览器缓存(mod_expires + mod_headers):执行 sudo a2enmod expires headers,在配置中加入:
ExpiresActive On
ExpiresByType text/html “access plus 1 hour”
ExpiresByType text/css “access plus 1 month”
ExpiresByType application/javascript “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 year”
ExpiresByType image/png “access plus 1 year”
ExpiresByType image/gif “access plus 1 year”
ExpiresByType image/svg+xml “access plus 1 year”
ExpiresByType font/woff2 “access plus 1 year”
Header set Cache-Control “public, max-age=31536000”
说明:将 HTML 设为较短时间以便内容更新,将 静态资源 设为长期并配合文件名哈希或版本号实现“强缓存 + 更新可控”。
- 连接与会话:适度降低 KeepAliveTimeout(如 5 秒)与 MaxKeepAliveRequests(如 100),减少长连接占用,提高并发与首包时间表现。
- 可选页面级缓存(mod_cache_disk):执行 sudo a2enmod cache cache_disk,在 或 <Location “/static”> 中加入:
CacheEnable disk /static
CacheRoot “/var/cache/apache2/mod_cache_disk”
CacheDirLevels 2
CacheDirLength 1
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod On
CacheDefaultExpire 86400
提示:动态内容慎用页面缓存,避免用户看到过期页面。
三 URL 结构与可爬取性
- 开启 mod_rewrite 并在 设置 AllowOverride All,使用 .htaccess 实现“美化 URL”:
RewriteEngine On
RewriteRule ^product/([0-9]+)/?$ product.php?id=$1 [L]
作用:将动态参数转为可读路径,利于排名与点击。
- 规范站点地图与 robots:在站点根目录提供 /robots.txt(如 User-agent: * Disallow: /admin/)与 /sitemap.xml,并确保可被爬虫抓取。
- 语义化与结构化数据:在 HTML 中使用语义标签(),并添加 Schema.org 微数据/JSON-LD,帮助搜索引擎理解内容类型与关系。
四 验证与持续运维
- 配置校验与热重载:sudo apache2ctl configtest && sudo systemctl reload apache2。
- 压缩验证:浏览器开发者工具或 curl -I 查看 Content-Encoding: gzip。
- 缓存验证:检查响应头 Cache-Control、Expires 是否符合策略。
- 安全与可达性:定期更新系统与软件包(sudo apt update && sudo apt upgrade),并监控 access.log/error.log 与关键指标(如 CPU/内存/磁盘 IO),必要时引入 CDN 加速静态资源与降低源站负载。