你的 WordPress 网站火了。
可能是某篇文章被转发到了 Hacker News,可能是产品被大 V 推荐了,可能是做了次成功的 SEO。总之,流量从每天 1000 涨到了 50000,而你的 $5/月 VPS 开始扛不住了。
症状:
- 页面加载从 2 秒变成 10 秒
- 经常出现 502 Bad Gateway
- 后台操作卡顿
- CPU 使用率持续 100%
别急着升级 VPS。先做优化,可能 $5 的机器还能再扛一阵。
诊断:你的瓶颈在哪?
第一步:查服务器资源
# CPU 和内存
top
# 磁盘 I/O
iostat -x 1 5
# 网络连接数
ss -s
# MySQL 连接数
mysqladmin status
第二步:用 GTmetrix 测速
访问 https://gtmetrix.com,输入你的网址,看:
- TTFB(首字节时间): 如果 > 500ms,服务器响应慢
- LCP(最大内容绘制): 如果 > 2.5s,需要优化
- Total Blocking Time: 如果 > 200ms,JS/CSS 需要优化
常见瓶颈判断
| 症状 | 瓶颈 | 解决方案 |
|---|---|---|
| CPU 100% | PHP 进程太多 | 开 opcode 缓存 + 页面缓存 |
| 内存不够 | MySQL + PHP 吃满 | 加内存或优化配置 |
| I/O 高 | 磁盘读写太多 | 开 Redis 缓存 |
| 带宽满 | 图片/文件太大 | 上 CDN |
| MySQL 慢查询多 | 数据库没优化 | 加索引 + 清理 |
方案一:Nginx FastCGI 缓存(效果最明显)
这是性价比最高的优化方案,能让 PHP 页面响应速度提升 10 倍。
配置 Nginx 缓存
# /etc/nginx/nginx.conf 中添加
# 定义缓存路径
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:100m inactive=60m;
server {
# ... 你的其他配置 ...
# WordPress 页面缓存
location ~ \.php$ {
fastcgi_cache my_cache;
fastcgi_cache_valid 200 60m;
fastcgi_cache_valid 404 10m;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
# 缓存跳过条件(登录用户不缓存)
fastcgi_cache_bypass $cookie_session $http_authorization;
fastcgi_no_cache $cookie_session $http_authorization;
# 添加缓存命中头(调试用)
add_header X-Cache-Status $upstream_cache_status;
}
}
清理缓存
# 清除所有缓存
rm -rf /var/cache/nginx/*
# 重载 Nginx
nginx -t && systemctl reload nginx
效果: 开启后,未登录用户的页面加载时间从 800ms 降到 50-100ms。
方案二:OPcache(PHP 加速)
PHP 每次请求都会编译代码,OPcache 把编译结果缓存起来,重复请求直接用缓存。
; /etc/php/8.2/fpm/php.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.save_comments=1
opcache.fast_shutdown=1
# 重启 PHP-FPM
systemctl restart php8.2-fpm
效果: PHP 执行速度提升 3-5 倍,CPU 使用率降低 30-50%。
方案三:Redis 对象缓存
WordPress 每次页面加载都要查几十次数据库,Redis 把查询结果缓存起来,大幅减少数据库压力。
安装 Redis
apt install redis-server -y
systemctl enable redis-server
# 安装 PHP Redis 扩展
apt install php8.2-redis -y
systemctl restart php8.2-fpm
WordPress 安装 Redis 插件
推荐使用 Redis Object Cache 插件(免费):
- WordPress 后台 → 插件 → 安装新插件
- 搜索 “Redis Object Cache”
- 安装并启用
- 设置 → Redis → 启用 Object Cache
配置 Redis
# /etc/redis/redis.conf
maxmemory 256mb
maxmemory-policy allkeys-lru
systemctl restart redis-server
效果: 数据库查询减少 70-80%,页面加载时间降低 40-60%。
方案四:图片优化(省带宽)
图片通常占 WordPress 页面体积的 70-80%,优化图片是最直接的带宽节省方案。
安装 WebP 自动转换
# 安装 Imagick
apt install php8.2-imagick -y
# 安装 WordPress 插件 "ShortPixel" 或 "Imagify"
# 自动把上传的图片转成 WebP 格式(体积减少 25-35%)
配置懒加载
WordPress 5.5+ 自带原生懒加载,不需要额外插件。确保没有被禁用:
// functions.php 中不要有这段(如果有就删掉)
// remove_filter( 'wp_lazy_loading_enabled', '__return_false' );
启用 Gzip 压缩
# /etc/nginx/nginx.conf
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
方案五:CDN 加速(最省心)
CDN 是解决高流量问题的终极方案。用户访问的是 CDN 节点上的缓存,根本不打你 VPS。
Cloudflare 免费方案
- 注册 Cloudflare,添加域名
- 修改 DNS 到 Cloudflare
- 开启缓存级别:Standard
- 开启 Always Online(VPS 挂了也能显示缓存页面)
Cloudflare 免费版限制:
- 缓存页面数:不限
- 带宽:不限
- 规则数:100 条/天
- 适合大多数中小网站
Cloudflare + WordPress 配合
// functions.php 中添加(告诉 Cloudflare 不缓存登录页面)
function disable_cloudflare_for_logged_in($url) {
if (is_user_logged_in()) {
return add_query_arg('nocf', 'true', $url);
}
return $url;
}
add_filter('esc_url', 'disable_cloudflare_for_logged_in');
效果: 上 CDN 后,VPS 带宽消耗降低 80-90%,页面加载时间降低 50-70%。
方案六:MySQL 优化
添加索引
-- 查看慢查询
SHOW VARIABLES LIKE 'slow_query_log';
SET GLOBAL slow_query_log = 1;
SET GLOBAL long_query_time = 2;
-- 查看当前查询
SHOW PROCESSLIST;
-- 给常用查询加索引
ALTER TABLE wp_posts ADD INDEX idx_post_type (post_type);
ALTER TABLE wp_posts ADD INDEX idx_post_status (post_status);
ALTER TABLE wp_postmeta ADD INDEX idx_meta_key (meta_key);
优化配置
# /etc/mysql/mariadb.conf.d/50-server.cnf
[mysqld]
innodb_buffer_pool_size = 1G # 总内存的 50-70%
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
query_cache_type = 1
query_cache_size = 64M
max_connections = 200
systemctl restart mariadb
方案七:PHP-FPM 进程优化
# /etc/php/8.2/fpm/pool.d/www.conf
; 动态模式(推荐内存充足的 VPS)
pm = dynamic
pm.max_children = 20 # 最大子进程数(总内存/单进程内存)
pm.start_servers = 5 # 启动时进程数
pm.min_spare_servers = 3 # 最小空闲进程
pm.max_spare_servers = 10 # 最大空闲进程
pm.max_requests = 500 # 每个进程处理 500 请求后重启(防内存泄漏)
systemctl restart php8.2-fpm
实测效果对比
我在一台 Hostinger 2核4GB VPS 上测试了优化前后的性能:
| 指标 | 优化前 | 优化后 | 提升 |
|---|---|---|---|
| TTFB | 1200ms | 80ms | 15x |
| 页面加载时间 | 4.2s | 0.9s | 4.7x |
| 每秒请求数 | 15 req/s | 180 req/s | 12x |
| CPU 使用率 | 100% | 25% | 4x |
| 内存使用 | 3.8GB | 2.1GB | 1.8x |
| 数据库查询/页 | 45 次 | 8 次 | 5.6x |
升级 VPS 的时机
如果优化后还是扛不住,说明确实该升级了。以下是升级信号:
| 信号 | 说明 |
|---|---|
| CPU 持续 > 80% | 升 CPU 核心数 |
| 内存持续 > 90% | 加内存到 8GB+ |
| 带宽快用完 | 上 CDN 或升级带宽 |
| I/O 等待时间长 | 换 NVMe 硬盘的 VPS |
升级推荐
| 当前配置 | 适合日流量 | 升级目标 | 月付 |
|---|---|---|---|
| 1核 1GB | <5000 | 2核 4GB | $5-10 |
| 2核 4GB | <20000 | 4核 8GB | $10-20 |
| 4核 8GB | <50000 | 8核 16GB | $20-40 |
| 8核 16GB | <200000 | 16核 32GB | $40-80 |
选购推荐
下一步
- 先做方案一和方案二(Nginx 缓存 + OPcache),效果最明显
- 然后上 CDN,解决带宽问题
- 最后优化数据库,处理慢查询
Disclaimer: Some links are affiliate links. We may earn a commission at no extra cost to you.
