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缓存提升几倍甚至十几倍以上。


环境说明:centos6.6 LNMP环境

redis官网下载源码:http://redis.io/download

[root@localhost src]# wget http://download.redis.io/releases/redis-3.0.2.tar.gz [root@localhost src]# tar zxvf redis-3.0.2.tar.gz  [root@localhost redis-3.0.2]# cd redis-3.0.2 [root@localhost redis-3.0.2]# make

 #redis的安装非常简单,已经有现成的Makefile文件,直接运行make命令即可

安装完成后在src目录下,会生成几个可执行文件:redis-benchmark,redis-check-aof,redis-check- dump,redis-cli,redis-sentinel,redis-server。这几个文件,加上一个redis.conf就构成了整个redis的最终可用包。


下面你可以把这几个可执行文件和redis.conf文件复制到你所希望的地方,比如/usr/local/redis/bin 和/usr/local/redis/etc 下面的,命令如下:

[root@localhost src]# cd redis-3.0.2 [root@localhost redis-3.0.2]# mkdir -p /usr/local/redis/{bin,var,etc} [root@localhost redis-3.0.2]# cd src/ [root@localhost src]# cp redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server /usr/local/redis/bin/ [root@localhost redis-3.0.2]# cp /usr/local/src/redis-3.0.2/redis.conf /usr/local/redis/etc [root@localhost redis-3.0.2]# ln -s /usr/local/redis/bin/* /usr/bin/

修改redis.conf配置文件:

[root@localhost redis-3.0.2]# sed -i 's#pidfile.*$#pidfile /var/run/redis.pid#' /usr/local/redis/etc/redis.conf  [root@localhost redis-3.0.2]# sed -i 's#logfile.*$#logfile /usr/local/redis/var/redis.log#' /usr/local/redis/etc/redis.conf  [root@localhost redis-3.0.2]# sed -i 's#^dir.*$#dir /usr/local/redis/var#' /usr/local/redis/etc/redis.conf  [root@localhost redis-3.0.2]# sed -i 's#daemonize no#daemonize yes#' /usr/local/redis/etc/redis.conf

注意,默认复制过去的redis.conf文件的daemonize参数为no,所以redis不会在后台运行,这时要测试,我们需要重新开一个终端。修改为yes则为后台运行redis。另外配置文件中规定了pid文件,log文件和数据文件的地址,如果有需要先修改,默认log信息定向到标准输出。

[root@localhost redis-3.0.2]# echo 'vm.overcommit_memory = 1'>> /etc/sysctl.conf  [root@localhost redis-3.0.2]# sysctl -p

配置开机启动redis-server

[root@localhost src]# wget https://raw.githubusercontent.com/lj2007331/lnmp/master/init/Redis-server-init-CentOS [root@localhost src]# mv Redis-server-init-CentOS /etc/init.d/redis-server [root@localhost src]# chmod +x /etc/init.d/redis-server  [root@localhost src]# chkconfig --add redis-server [root@localhost src]# chkconfig redis-server on

启动redis

[root@localhost src]# service redis-server start


测试:

[root@localhost src]# /usr/local/redis/bin/redis-cli 127.0.0.1:6379> set 123 baby OK 127.0.0.1:6379> get 123 "baby" 127.0.0.1:6379> exit

关闭redis

[root@localhost src]# service redis-server stop

安装redis php客户端

[root@localhost src]# wget http://pecl.php.net/get/redis-2.2.3.tgz [root@localhost src]# tar zxf redis-2.2.3.tgz  [root@localhost src]# cd redis-2.2.3


执行phpize命令,生成configure可执行文件

[root@localhost redis-2.2.3]# /usr/local/php-fpm/bin/phpize  [root@localhost redis-2.2.3]# ./configure --with-php-cOnfig=/usr/local/php-fpm/bin/php-config  [root@localhost redis-2.2.3]# make && make install

php.ini配置文件,添加extension

[root@localhost ~]# sed -i '/; extension_dir = "ext"/a\extension = "redis.so"' /usr/local/php-fpm/etc/php.ini  [root@localhost ~]# service php-fpm restart


使wordpress支持redis

你需要一个客户端开发包以便PHP可以连接到redis服务端 ,这里我们推荐predis. 加入WordPress的根目录,执行下面

[root@localhost src]# wget http://uploads.staticjw.com/ji/jim/predis.php [root@localhost src]# chown php-fpm:php-fpm predis.php

前端缓存的PHP脚本,加入WordPress的根目录,执行下面

[root@localhost src]# wget https://gist.githubusercontent.com/JimWestergren/3053250/raw/d9e279e31cbee4a1520f59108a4418ae396b2dde/index-with-redis.php [root@localhost src]# chown php-fpm:php-fpm index-with-redis.php  [root@localhost src]# mv predis.php index-with-redis.php /data/www/blog

根据自己需求修改index-with-redis.php,修改如下:

$cf = 0;                        // set to 1 if you are using cloudflare $debug = 1;                     // set to 1 if you wish to see execution time and cache actions $display_powered_by_redis = 0;  // set to 1 if you want to display a powered by redis message with execution time, see below

如果你正在使用cloudflare,请设置cf = 1; ,

如果你想在页面上看到脚本执行时间和缓存加载时间,请设置$debug = 1; 浏览器最下方会显示this is cache:

display_powered_by_redis = 1表示显示powered_by信息。如下图右下角图标:

替换index.php

[root@localhost blog]# mv index.php index.php.bak [root@localhost blog]# mv index-with-redis.php index.php

缓存问题

index-with-redis.php中有注释

    - appending a ?c=y to a url deletes the entire cache of the domain, only works when you are logged in      - appending a ?r=y to a url deletes the cache of that url     - submitting a comment deletes the cache of that page     - refreshing (f5) a page deletes the cache of that page

登录后台网站url后面加上?c=y即可刷新整个网站 

可以在网站页面后面加上?r=y即可手工刷新

提交评论会自动刷新页面

刷新(f5)页面也可以刷新页面

刷新网页查看缓存效果,查看源代码

360浏览器页面最下角会显示类似:this is a cache: 0.04534

F5刷新页面缓存时间会变化


注意事项

1、注意,Wordpress Redis缓存PHP版本在5.3以上

2、Wordpress Redis缓存加速效果无疑是明显的,特别页面多访问大的网站博客,在使用Wordpress Redis缓存加速时请禁止其它的所有缓存插件,以免造成不必要的冲突。