WordPress功能很强大,但是性能确实比较慢, WordPress采用的是动态数据库查询技术。通俗的说,就是用户访问每篇文章或页面,都会向数据库发送一条查询命定,数据库根据命定查询之后,反送查询结果(这个结果不考虑任何缓存技术)。显然,如果访问量大的时候,会出现频繁的查询。所以这会减慢网站速度。如果服务器性能不高,瞬间网站就崩溃了。
所以需要一种技术,来减少数据库查询次数。而数据库缓存技术就是其中之一。Redis技术是其中的佼佼者。Redis是key-value分布式存储系统。简单的说,就是根据关键词值进行查询,这在很大程度上弥补了Memcached的短板。通过Redis进行数据库缓存,查询速度会更快,并发数更多。
本次教程使用宝塔面板,后台的软件管理里面就可以直接安装Redis,不用去ssh下面敲命令来下载安装,提供的这种方法,更适合小白,毕竟谁都不是大神,用最简单的方法,让我们的博客变的飞起来,那何乐而不为呢。
第一:安装Redis扩展
安装过程也是很快的,稍等会安装好了,我们开始下一步。顺带介绍下redis。Redis是一个高级的key-value存储系统,类似memcached,所有内容都存在内存中,因此每秒钟可以超过10万次GET操作。如果流量很大的站我们可以用在redis缓存来解决流量太大给服务器带来的压力。
安装完最好重启下php服务
用宝塔面板远程下载的功能来完成下载所需要的redis文件
http://uploads.staticjw.com/ji/jim/predis.php
然后,我们点击确定可以了,我们可以刷新下这个目录,就可以看到下载的东西了。下载好,可以安装前面说的修改下权限
后面的前端缓存的PHP脚本index-with-redis.php也可以安装这个方法来下载,然后修改权限和用户。
复制以下代码创建index-with-redis.php
是修改了bug,这个版本也是我目前在用的版本。
<?php
/*
author: jeedo aquino
file: wp-index-redis.php
credit: jim westergren
updated: 2012-10-23
this is a redis caching system for wordpress inspired by jim westergren.
see more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
some caching mechanics are different from jim's script which is summarized below:
- cached pages do not expire not unless explicitly deleted or reset
- 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
- script still works even if allow_fopen is disabled
- submitting a comment deletes the cache of that page
- refreshing (f5) a page deletes the cache of that page
- includes a debug mode, stats are displayed at the bottom most part after
for setup and configuration see more here:
www.jeedo.net/lightning-fast-wordpress-with-nginx-redis/
use this script at your own risk. i currently use this albeit a slightly modified version
to display a redis badge whenever a cache is displayed.
*/
// change vars here
$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
$start = microtime(); // start timing page exec
// if cloudflare is enabled
if ($cf) {
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
}
// from wp
define('WP_USE_THEMES', true);
// init predis
include("predis.php");
$redis = new Predis\Client('');
// init vars
$domain = $_SERVER['HTTP_HOST'];
$url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$url = str_replace('?r=y', '', $url);
$url = str_replace('?c=y', '', $url);
$dkey = md5($domain);
$ukey = md5($url);
// check if page isn't a comment submission
(($_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0') ? $submit = 1 : $submit = 0);
// check if logged in to wp
$cookie = var_export($_COOKIE, true);
$loggedin = preg_match("/wordpress_logged_in/", $cookie);
// check if a cache of the page exists
if ($redis->hexists($dkey, $ukey) && !$loggedin && !$submit) {
echo $redis->hget($dkey, $ukey);
if (!$debug) exit(0);
$msg = 'this is a cache';
// if a comment was submitted or clear page cache request was made delete cache of page
} else if ($submit || substr($_SERVER['REQUEST_URI'], -4) == '?r=y') {
require('./wp-blog-header.php');
$redis->hdel($dkey, $ukey);
$msg = 'cache of page deleted';
// delete entire cache, works only if logged in
} else if ($loggedin && substr($_SERVER['REQUEST_URI'], -4) == '?c=y') {
require('./wp-blog-header.php');
if ($redis->exists($dkey)) {
$redis->del($dkey);
$msg = 'domain cache flushed';
} else {
$msg = 'no cache to flush';
}
// if logged in don't cache anything
} else if ($loggedin) {
require('./wp-blog-header.php');
$msg = 'not cached';
// cache the page
} else {
// turn on output buffering
ob_start();
require('./wp-blog-header.php');
// get contents of output buffer
$html = ob_get_contents();
// clean output buffer
ob_end_clean();
echo $html;
// store html contents to redis cache
$redis->hset($dkey, $ukey, $html);
$msg = 'cache is set';
}
$end = microtime(); // get end execution time
// show messages if debug is enabled
if ($debug) {
echo $msg.': ';
echo t_exec($start, $end);
}
// time diff
function t_exec($start, $end) {
$t = (getmicrotime($end) - getmicrotime($start));
return round($t,5);
}
// get time
function getmicrotime($t) {
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
?>
调试修改index-with-redis.php的一些参数
$cf = 1; // 如果开了cloudflare这里设置为1,否则为0
$debug = 0; // 是否开启底部调试信息,显示执行时间等
$display_powered_by_redis = 1; // 这个是原版文件带的功能,修改版已经取消,以上1位显示powered by redis , 0 关闭。
安装 WP Redis 插件 (Redis Object Cache)
在 WordPress 后台插件管理安装Redis Object Cache插件
在Wordpress后台启用 Redis Object Cache插件,然后点击Connect。
禁用某个Redis缓存
使用了Redis缓存后,有些主题或者是插件可能出现不兼容的问题,例如挖站否论坛使用的插件Anspress就不兼容Redis,所以我们需要在wp-config.php中排除插件或者主题用到的Groups,代码如下:
define('WP_REDIS_IGNORED_GROUPS',array('comment','counts','plugins','themes','anspress','ap','ap_prefetch_activities','ap_q_id_activity','ap_users_reputation','ap_qameta','ap_prefetch_activities','ap_subscriber','ap_votes_queries','ap_vote'));
多站点启用Redis缓存
如果你的服务器上运行了多个Wordpress,这个时候就需要WP_CACHE_KEY_SALT来给每个网站设置一个值,这样多个网站就可以共同启用Redis缓存了(字符串值可以使用上面的随机生成网址生成)。
define( 'WP_CACHE_KEY_SALT', 'hpWQo8vfb3' );
# define('WP_CACHE', true);
远程使用Redis缓存
如果你想要让你的Wordpress调用远程的Redis缓存(像阿里云、腾讯云就提供此类的服务),编辑wp-config.php指定Redis缓存的地址、端口、密码以及数据库等,如果配合上面的WP_CACHE_KEY_SALT设置,你又可以实现远程+多站点Redis缓存了。向网站根目录下的wp-config.php文件中添加以下内容,
代码如下:
define('WP_REDIS_HOST', '127.0.0.1'); # redis 服务器地址
define('WP_REDIS_PORT', '6379'); # redis 服务器端口
define('WP_REDIS_DATABASE', '0'); # redis 数据库(第一个0 ,第一个1,依次类推)
define('WP_REDIS_PASSWORD', '123456'); # redis 数据库密码
Redis完整配置命令如下,请根据自己的需要使用:
#Redis完整配置如下:
define('WP_REDIS_CLIENT', 'pecl'); // 指定用于与Redis通信的客户端, pecl 即 The PHP Extension Community Library
define('WP_REDIS_SCHEME', 'tcp'); // 指定用于与Redis实例进行通信的协议
define('WP_REDIS_HOST', '127.0.0.1'); // Redis服务器的IP或主机名
define('WP_REDIS_PORT', '6379'); // Redis端口
define('WP_REDIS_DATABASE', '0'); // 接受用于使用该SELECT命令自动选择逻辑数据库的数值
define('P_REDIS_PASSWORD', ''); // Redis密码
define('WP_CACHE_KEY_SALT', 'wp_'); // 设置所有缓存键的前缀(Wordpress多站点模式下使用)
define('WP_REDIS_MAXTTL', '86400');
使用Redis缓存也有一段时间了,遇到的问题主要有无法登录后台,清除Redis缓存正常了。另外,Redis缓存还可以导致插件和主题修改不生效
登录后台网站url后面加上?c=y即可刷新整个网站
可以在网站页面后面加上?r=y即可手工刷新
提交评论会自动刷新页面
刷新(f5)页面也可以刷新页面
如果您觉得删除不够彻底,可以写一个php放到网站上,每访问一次,即可全部清空redis数据库,代码如下:
<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
//执行命令清空数据库
$redis->flushall();
?>
——————————————————————————————————————————————————————————————
ssh安装教程:
SSH工具登陆VPS后,定位到你的网站根目录,比如网站目录:/www/wwwroot/www.httple.net;运行下方命令定位到所需网站的根目录
cd /www/wwwroot/www.httple.net #请修改成你自己网站的实际目录)
然后运行下方命令下载所需的文件
wget http://uploads.staticjw.com/ji/jim/predis.php
再运行下方命令修改权限
chown www.www ./predis.php
再运行下方命令下载前端缓存的PHP脚本
wget https://gist.githubusercontent.com/JimWestergren/3053250/raw/d9e279e31cbee4a1520f59108a4418ae396b2dde/index-with-redis.php
修改权限
chown www.www ./index-with-redis.php
再用下方命令替换原网站目录的文件index.php,回车确定(这里说下,你可以备份下你的原来的index.php文件 ,如果你要还原可以直接删除掉这个然后用原来的还原。)
mv index-with-redis.php index.php
至此大功告成,Redis缓存成功开启!做完这步,就开启了Redis来加速WordPress,这时远在美国机房的网站的WordPress速度照样飞快。