Debian 上用 Apache2 做 SEO 的实操清单
一 基础与环境准备
- 更新系统并安装组件:sudo apt update && sudo apt upgrade;sudo apt install apache2 php libapache2-mod-php。
- 启用关键模块:sudo a2enmod rewrite deflate expires headers。
- 创建并检查虚拟主机(示例):/etc/apache2/sites-available/example.com.conf,启用站点:sudo a2ensite example.com.conf && sudo a2dissite 000-default.conf;检查语法:sudo apache2ctl configtest;重载:sudo systemctl reload apache2。
- 目录权限与索引:在 中设置 AllowOverride All(以便 .htaccess 生效)、Require all granted,并按需配置 DirectoryIndex。以上为后续 SEO 配置的基础。
二 URL 结构与 HTTPS
- 开启重写并配置语义化 URL:
- 通用 PHP 站点(将所有非文件/目录请求交给前端控制器):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
- WordPress 常用规则:
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
- 可选:将动态参数转为可读路径,如将 /product/123 映射到 product.php?id=123。
- 全站 HTTPS 与自动续期:sudo apt install certbot python3-certbot-apache;sudo certbot --apache -d example.com -d www.example.com;建议开启 HSTS(在虚拟主机 443 段添加:Header always set Strict-Transport-Security “max-age=63072000; includeSubDomains; preload”)。
- 站点地图与 robots:放置 sitemap.xml 与 robots.txt(如:User-agent: * Disallow: /private/ Allow: /public/),便于抓取与索引控制。
三 性能与缓存控制
- 启用压缩(mod_deflate):
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json application/xml application/xml+rss
- 资源过期与浏览器缓存(mod_expires + mod_headers):
ExpiresActive On
ExpiresByType text/html “access plus 1 week”
ExpiresByType text/css “access plus 1 month”
ExpiresByType application/javascript “access plus 1 month”
ExpiresByType image/jpeg “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType image/gif “access plus 1 month”
Header set Cache-Control “public, max-age=31536000”
- KeepAlive 调优(在 /etc/apache2/apache2.conf 的 MPM 段落):KeepAlive On;KeepAliveTimeout 5;MaxKeepAliveRequests 100,以降低连接开销、提升并发体验。
- 可选页面级缓存:启用 mod_cache 与合适的 Cache-Control 策略,缓存可缓存内容,进一步缩短 TTFB。
四 安全头与爬虫管理
- 安全响应头(提升安全评分与可信度):
Header set X-Content-Type-Options “nosniff”
Header set X-Frame-Options “SAMEORIGIN”
Header set X-XSS-Protection “1; mode=block”
Header set Referrer-Policy “no-referrer-when-downgrade”
- 防火墙与端口:sudo apt install ufw;sudo ufw allow 80/tcp;sudo ufw allow 443/tcp;sudo ufw enable。
- 日志分析与监控:定期分析访问日志,使用 GoAccess 等工具洞察爬虫与用户路径,及时修复 404 等错误页面,减少流失。
五 内容与技术 SEO 协同
- 页面级优化:确保每个页面拥有唯一且包含核心关键词的 与 ;URL 简短、可读、少参数;图片添加 alt 并使用 WebP 等现代格式;采用响应式设计提升移动友好性。
- 结构化数据:在页面加入 JSON-LD 或 Microdata(如 Article/Product),增强搜索结果展示(评分、面包屑、富摘要等)。
- 持续迭代:定期更新高质量原创内容,监控排名与性能,避免“黑帽”技术。