[![AMH面板安装配置redis加速WordPress站点](https://pic.salongweb.com/2016/03/amh-redis-wordpress.jpg)](https://pic.salongweb.com/2016/03/amh-redis-wordpress.jpg)Redis是一个开源、支持网络、基于内存的key-value存储系统，类似memcached，性能极高，支持超过100K+ 每秒的读写频率，一些大型的网站例如ITeye(JavaEye)和CSDN现在都用到了Redis。

与memcached相比，Redis提供了持久化存储，重启了服务器后memcached需要重新创建缓存，而Redis依赖快照进行持久化，即使服务器刚开机启动也不会导致负载陡增。Redis缓存比较适合大流量的Wordpress。

当你的WordPress中的文章达到上万篇，随着流量的增加，Wordpress的服务器压力也随之不断加大，Wordpress发布文章和后台相关的操作都会变得缓慢，这时如果单从硬件上投入来提高Wordpress性能显然不划算。

利用Redis将WordPress页面直接缓存在服务器的内存中，这样在避免了PHP重复执行操作的同时，内存的极速响应能够最大限度地提升Wordpress页面的访问速度，实际测试发现页面执行时间可以降低到0.00X秒级别，比没有使用Redis缓存提升几倍甚至十几倍以上。

用Redis缓存来给WordPress站点加速-适用于Apache和Nginx。

## 一、AMH面板上安装redis插件

如果你使用的不是AMH面板就使用SSH进行安装redis，在AMH面板需要安装redis+pecl\_redis两个软件（支持PHP7），pecl\_redis是AMH面板Redis软件的php环境扩展模块。下载安装好后，在应用软件中点击pecl\_redis后面的管理，选择需要缓存的环境，安装即可。

## 二、用Predis.php作为Redis的PHP客户端

1、我们可以使用Predis.php来作为PHP客户端，直接将Predis.php下载并上传到Wordpress的根目录下，CD到Wordpress根目录，执行以下命令：

1. wget http://uploads.staticjw.com/ji/jim/predis.php

2、这是Predis.php的备份下载：[Predis.php](https://demo.salongweb.com/predis.php)下载，手动下载并上传WordPress网站根目录下。

## 三、安装Redis前端缓存的PHP脚本

1、Redis前端缓存的PHP脚本来自：http://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/，为优化后的版本[](https://pic.salongweb.com/2016/03/amh-redis-wordpress.jpg)

1. &lt;?php
2. 
3. /\*
4. author: jeedo aquino
5. file: wp-index-redis.php
6. credit: jim westergren
7. updated: 2012-10-23
8. 
9. this is a redis caching system for wordpress inspired by jim westergren.
10. see more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
11. some caching mechanics are different from jim’s script which is summarized below:
12. 
13. – cached pages do not expire not unless explicitly deleted or reset
14. – appending a ?c=y to a url deletes the entire cache of the domain, only works when you are logged in
15. – appending a ?r=y to a url deletes the cache of that url
16. – script still works even if allow\_fopen is disabled
17. – submitting a comment deletes the cache of that page
18. – refreshing (f5) a page deletes the cache of that page
19. – includes a debug mode, stats are displayed at the bottom most part after
20. 
21. for setup and configuration see more here:
22. 
23. www.jeedo.net/lightning-fast-wordpress-with-nginx-redis/
24. 
25. use this script at your own risk. i currently use this albeit a slightly modified version
26. to display a redis badge whenever a cache is displayed.
27. 
28. \*/
29. 
30. // change vars here
31. $cf = 0; // set to 1 if you are using cloudflare
32. $debug = 0; // set to 1 if you wish to see execution time and cache actions
33. $display\_powered\_by\_redis = 0; // set to 1 if you want to display a powered by redis message with execution time, see below
34. 
35. $start = microtime(); // start timing page exec
36. 
37. // if cloudflare is enabled
38. if ($cf) {
39. if (isset($\_SERVER\[‘HTTP\_CF\_CONNECTING\_IP’\])) {
40. $\_SERVER\[‘REMOTE\_ADDR’\] = $\_SERVER\[‘HTTP\_CF\_CONNECTING\_IP’\];
41. }
42. }
43. 
44. // from wp
45. define(‘WP\_USE\_THEMES’, true);
46. 
47. // init predis
48. include(“predis.php”);
49. $redis = new Predis\\Client(”);
50. 
51. // init vars
52. $domain = $\_SERVER\[‘HTTP\_HOST’\];
53. $url = “http://”.$\_SERVER\[‘HTTP\_HOST’\].$\_SERVER\[‘REQUEST\_URI’\];
54. $url = str\_replace(‘?r=y’, ”, $url);
55. $url = str\_replace(‘?c=y’, ”, $url);
56. $dkey = md5($domain);
57. $ukey = md5($url);
58. 
59. // check if page isn’t a comment submission
60. (($\_SERVER\[‘HTTP\_CACHE\_CONTROL’\] == ‘max-age=0’) ? $submit = 1 : $submit = 0);
61. 
62. // check if logged in to wp
63. $cookie = var\_export($\_COOKIE, true);
64. $loggedin = preg\_match(“/wordpress\_logged\_in/”, $cookie);
65. 
66. // check if a cache of the page exists
67. if ($redis-&gt;hexists($dkey, $ukey) &amp;&amp; !$loggedin &amp;&amp; !$submit) {
68. 
69. echo $redis-&gt;hget($dkey, $ukey);
70. if (!$debug) exit(0);
71. $msg = ‘this is a cache’;
72. 
73. // if a comment was submitted or clear page cache request was made delete cache of page
74. } else if ($submit || substr($\_SERVER\[‘REQUEST\_URI’\], -4) == ‘?r=y’) {
75. 
76. require(‘./wp-blog-header.php’);
77. $redis-&gt;hdel($dkey, $ukey);
78. $msg = ‘cache of page deleted’;
79. 
80. // delete entire cache, works only if logged in
81. } else if ($loggedin &amp;&amp; substr($\_SERVER\[‘REQUEST\_URI’\], -4) == ‘?c=y’) {
82. 
83. require(‘./wp-blog-header.php’);
84. if ($redis-&gt;exists($dkey)) {
85. $redis-&gt;del($dkey);
86. $msg = ‘domain cache flushed’;
87. } else {
88. $msg = ‘no cache to flush‘;
89. }
90. 
91. // if logged in don’t cache anything
92. } else if ($loggedin) {
93. 
94. require(‘./wp-blog-header.php’);
95. $msg = ‘not cached’;
96. 
97. // cache the page
98. } else {
99. 
100. // turn on output buffering
101. ob\_start();
102. 
103. require(‘./wp-blog-header.php’);
104. 
105. // get contents of output buffer
106. $html = ob\_get\_contents();
107. 
108. // clean output buffer
109. ob\_end\_clean();
110. echo $html;
111. 
112. // store html contents to redis cache
113. $redis-&gt;hset($dkey, $ukey, $html);
114. $msg = ‘cache is set’;
115. }
116. 
117. $end = microtime(); // get end execution time
118. 
119. // show messages if debug is enabled
120. if ($debug) {
121. echo $msg.’: ‘;
122. echo t\_exec($start, $end);
123. }
124. 
125. // time diff
126. function t\_exec($start, $end) {
127. $t = (getmicrotime($end) – getmicrotime($start));
128. return round($t,5);
129. }
130. 
131. // get time
132. function getmicrotime($t) {
133. list($usec, $sec) = explode(” “,$t);
134. return ((float)$usec + (float)$sec);
135. }
136. 
137. ?&gt;

2、 你也可以直接点击备用下载：[index-with-redis.php](https://demo.salongweb.com/index-with-redis.php)下载地址，再上传到WordPress根目录下。

3、 如果你正在使用cloudflare，请设置cf = 1; ，如果你想在页面上看到脚本执行时间和缓存加载时间，请设置$debug = 1; display\_powered\_by\_redis = 1表示显示powered\_by信息。

4、将index-with-redis.php上传到Wordpress的根目录，如果你使用的是nginx，重命令原来的index.php为任意其它名字，把index-with-redis.php重命名为index.php。

5、如果你使用的是Apache，则需要把.htaccess中出现的index.php替换成index-with-redis.php。

6、所有的操作完成后，你就可以刷新一下Wordpress页面，查看Redis缓存效果了。

## 四、Wordpress删除Redis页面缓存的方法

1、删除某一个页面的缓存：发表评论、按下F5刷新、在URL后面加上?r=y回车。

2、删除整站页面缓存：登录到Wordpress后台，在任意URL后面加上?r=y回车。

3、更新文章时自动刷新首页缓存：在你的Wordpress的Function.php中加入以下代码

1. function newPostRefresh() {
2. $temp=file\_get\_contents(“http://xxxxxxx/?r=y”); //xxxxxx自己替换
3. }
4. add\_action(‘publish\_post’, ‘newPostRefresh’);
5. add\_action(‘edit\_post’, ‘newPostRefresh’);
6. add\_action(‘delete\_post’, ‘newPostRefresh’);
7. add\_action(‘comment\_post’, ‘newPostRefresh’);
8. add\_action(‘edit\_comment’, ‘newPostRefresh’);
9. add\_action(‘delete\_comment’, ‘newPostRefresh’);
10. add\_action(‘wp\_set\_comment\_status’, ‘newPostRefresh’);
11. add\_action(‘switch\_theme’, ‘newPostRefresh’);

4、自动更新Wordpress缓存：在crontab中配置每10分钟用curl访问首页更新缓存，每天凌晨对首页或者分类页面进行更新，AMH可以安装amcrontab软件来进行设置。

1. \*/10 \* \* \* \* curl http://www.freehao123.com/?r=y
2. 1 1 \* \* \* curl http://www.freehao123.com/fenye/?r=y

## 五、Wordpress Redis缓存使用小结

WordPress Redis缓存加速效果无疑是明显的，特别页面多访问大的网站博客，在使用Wordpress Redis缓存加速时请禁止其它的所有缓存插件，以免造成不必要的冲突。经过测试Wordpress Redis缓存在使用过程中主要存在的问题就是文章更新时，相应的Tag和分类、首页等内容不会实时更新，我们需要手动更新或者利用自动更新命令。

文章来源：免费资源部落