WordPress 后台 TTFB 时间过长的问题
发现问题
偶然间发现 wp-admin 打开十分缓慢,打开 chrome 控制台一看,发现 TTFB(首个字节响应的时间)竟长达 3s ,属实不能忍。
![](https://rmb.ee/wp-content/uploads/2022/03/8e0a24cf-4baf-959e-840f-5dca0ea6aa2d.png)
问题排查
下载插件 Query Monitor ,发现有几个 HTTP 请求占用了大量的时间
![](https://rmb.ee/wp-content/uploads/2022/03/f302ff44-3ecc-b5df-035d-58beb2ba7dfe.png)
原来是 wordpress 检查更新的 functions 导致的问题
![](https://rmb.ee/wp-content/uploads/2022/03/a54b78bb-0082-fc61-3d75-398f6de973a0.png)
解决方法
移除所有自动更新(包括WordPress核心、主题、插件)
// 关闭 自动更新
add_filter('automatic_updater_disabled', '__return_true');
// 关闭 更新检查 定时任务
remove_action('init', 'wp_schedule_update_checks');
// 移除 已有的版本检查 定时任务
wp_clear_scheduled_hook('wp_version_check');
// 移除 插件更新 定时任务
wp_clear_scheduled_hook('wp_update_plugins');
// 移除 主题更新 定时任务
wp_clear_scheduled_hook('wp_update_themes');
// 移除 已有的自动更新 定时任务
wp_clear_scheduled_hook('wp_maybe_auto_update');
// 移除 后台内核 更新检查
remove_action('admin_init', '_maybe_update_core');
// 移除 后台插件 更新检查
remove_action('load-plugins.php', 'wp_update_plugins');
remove_action('load-update.php', 'wp_update_plugins');
remove_action('load-update-core.php', 'wp_update_plugins');
remove_action('admin_init', '_maybe_update_plugins');
// 移除 后台主题 更新检查
remove_action('load-themes.php', 'wp_update_themes');
remove_action('load-update.php', 'wp_update_themes');
remove_action('load-update-core.php', 'wp_update_themes');
remove_action('admin_init', '_maybe_update_themes');
至此,TTFB 已大量减少
![](https://rmb.ee/wp-content/uploads/2022/03/bde05a16-fedb-56df-0228-3b23adfe5c86.png)
但是,0.7s 依然有点慢,还可以更快。
接下来,我们为 php 安装 opcache 和 redis 扩展
![](https://rmb.ee/wp-content/uploads/2022/03/66de0241-00ae-bd09-40d5-75b27bf9f341.png)
并且安装 Redis Object Cache 插件用于连接 redis
可以看到,TTFB 已经缩短到 200+ms
![](https://rmb.ee/wp-content/uploads/2022/03/a75e3de4-3726-3b0a-cb69-270b13132cae.png)