我下面提出的解决方案是在 Redis 中缓存所有输出的 HTML 内容而无需再让 WordPress 重复执行页面脚本。这里使用 Redis 代替 Varnish 设置简单,而且可能更快。
安装 Redis
如果你使用的是 Debian 或者衍生的操作系统可使用如下命令安装 Redis:
apt-get install redis-server
使用 Predis 作为 Redis 的 PHP 客户端
你需要一个客户端开发包以便 PHP 可以连接到 Redis 服务上。
这里我们推荐 Predis. 上传 predis.php 到 WordPress 的根目录。
前端缓存的 PHP 脚本
步骤1: 在 WordPress 的根目录创建新文件 index-with-redis.php ,内容如下:
| 代码如下 | 复制代码 |
|
// change vars here $start = microtime(); // start timing page exec // if cloudflare is enabled // from wp // init predis // init vars // check if page isn't a comment submission // check if logged in to wp // check if a cache of the page exists echo $redis->hget($dkey, $ukey); // if a comment was submitted or clear page cache request was made delete cache of page require('./wp-blog-header.php'); // delete entire cache, works only if logged in require('./wp-blog-header.php'); // if logged in don't cache anything require('./wp-blog-header.php'); // cache the page // turn on output buffering require('./wp-blog-header.php'); // get contents of output buffer // clean output buffer // Store to cache only if the page exist and is not a search result. $end = microtime(); // get end execution time // show messages if debug is enabled if ($cached && $display_powered_by_redis) { Page generated in ".t_exec($start, $end)." sec } // time diff // get time ?> |
|
你也可以在 Github 上查看 index-with-redis.php
步骤2:将上述代码中的 IP 地址和网站域名替换成你网站的 IP 地址和域名
步骤3:在 .htaccess 中将所有出现 index.php 的地方改为 index-with-redis.php ,如果你使用的是 Nginx 则修改 nginx.conf 中的 index.php 为 index-with-redis.php(并重载 Nginx : killall -s HUP nginx)。
性能测试
◦没有 Redis 的情况下,平均首页执行 1.614 秒,文章页 0.174 秒(无任何缓存插件)
◦使用 Redis 的情况下,平均页面执行时间 0.00256 秒
我已经在我的博客中使用了如上的方法进行加速很长时间了,一切运行良好。
其他建议
本文作者的 WordPress 环境是 Nginx + PHP-FPM + APC + Cloudflare + Redis. 安装在一个 VPS 中,无缓存插件。
请确认使用了 gzip 压缩,可加快访问速度。
访问 wp-admin
要访问 wp-admin 必须使用 /wp-admin/index.php 代替原来的 /wp-admin/.
本文其实在国内已经有很翻译过了,但我看到作者也一直在更新此文,反而国内译者都不怎么更新,我就自己去重新折腾了一遍。
模拟极限越野
模拟极限越野开着大巴在各种奇葩路况里送乘客。山路弯道多得让人
模拟火车
模拟火车让你当回真正的火车司机,得先学会看信号灯、操作控制杆
可口的披萨美味的披萨
可口的披萨美味的披萨带您体验经营一家披萨店的乐趣。游戏画风温
定制虚拟偶像最新版
定制虚拟偶像最新版是一款超级经典有趣的日本模拟装扮类型的手游
病娇模拟器手机版正版(yandere simulator)
病娇模拟器最新版手游是一款萌妹二次元模拟游戏,玩家在游戏中可