萨龙网络
登录
首页-WordPress文章-WordPress,软件系统-正文

AMH面板安装配置redis加速WordPress站点

萨龙龙萨龙龙
WordPress, 软件系统
9年前
0
0
2.57W
AMH面板安装配置redis加速WordPress站点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下载,手动下载并上传WordPress网站根目录下。

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

1、Redis前端缓存的PHP脚本来自:http://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/,为优化后的版本
  1. <?php
  2. /*
  3.     author: jeedo aquino
  4.     file: wp-index-redis.php
  5.     credit: jim westergren
  6.     updated: 2012-10-23
  7.     this is a redis caching system for wordpress inspired by jim westergren.
  8.     see more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/
  9.     some caching mechanics are different from jim's script which is summarized below:
  10.     - cached pages do not expire not unless explicitly deleted or reset
  11.     - appending a ?c=y to a url deletes the entire cache of the domain, only works when you are logged in
  12.     - appending a ?r=y to a url deletes the cache of that url
  13.     - script still works even if allow_fopen is disabled
  14.     - submitting a comment deletes the cache of that page
  15.     - refreshing (f5) a page deletes the cache of that page
  16.     - includes a debug mode, stats are displayed at the bottom most part after 
  17.     for setup and configuration see more here:
  18.     www.jeedo.net/lightning-fast-wordpress-with-nginx-redis/
  19.     use this script at your own risk. i currently use this albeit a slightly modified version
  20.     to display a redis badge whenever a cache is displayed.
  21. */
  22. // change vars here
  23. $cf = 0;                // set to 1 if you are using cloudflare
  24. $debug = 0;             // set to 1 if you wish to see execution time and cache actions
  25. $display_powered_by_redis = 0;  // set to 1 if you want to display a powered by redis message with execution time, see below
  26. $start = microtime();   // start timing page exec
  27. // if cloudflare is enabled
  28. if ($cf) {
  29.     if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
  30.         $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
  31.     }
  32. }
  33. // from wp
  34. define('WP_USE_THEMES', true);
  35. // init predis
  36. include("predis.php");
  37. $redis = new Predis\Client('');
  38. // init vars
  39. $domain = $_SERVER['HTTP_HOST'];
  40. $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  41. $url = str_replace('?r=y', ''$url);
  42. $url = str_replace('?c=y', ''$url);
  43. $dkey = md5($domain);
  44. $ukey = md5($url);
  45. // check if page isn't a comment submission
  46. (($_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0') ? $submit = 1 : $submit = 0);
  47. // check if logged in to wp
  48. $cookie = var_export($_COOKIE, true);
  49. $loggedin = preg_match("/wordpress_logged_in/"$cookie);
  50. // check if a cache of the page exists
  51. if ($redis->hexists($dkey$ukey) && !$loggedin && !$submit) {
  52.     echo $redis->hget($dkey$ukey);
  53.     if (!$debugexit(0);
  54.     $msg = 'this is a cache';
  55. // if a comment was submitted or clear page cache request was made delete cache of page
  56. else if ($submit || substr($_SERVER['REQUEST_URI'], -4) == '?r=y') {
  57.     require('./wp-blog-header.php');
  58.     $redis->hdel($dkey$ukey);
  59.     $msg = 'cache of page deleted';
  60. // delete entire cache, works only if logged in
  61. else if ($loggedin && substr($_SERVER['REQUEST_URI'], -4) == '?c=y') {
  62.     require('./wp-blog-header.php');
  63.     if ($redis->exists($dkey)) {
  64.         $redis->del($dkey);
  65.         $msg = 'domain cache flushed';
  66.     } else {
  67.         $msg = 'no cache to flush';
  68.     }
  69. // if logged in don't cache anything
  70. else if ($loggedin) {
  71.     require('./wp-blog-header.php');
  72.     $msg = 'not cached';
  73. // cache the page
  74. else {
  75.     // turn on output buffering
  76.     ob_start();
  77.     require('./wp-blog-header.php');
  78.     // get contents of output buffer
  79.     $html = ob_get_contents();
  80.     // clean output buffer
  81.     ob_end_clean();
  82.     echo $html;
  83.     // store html contents to redis cache
  84.     $redis->hset($dkey$ukey$html);
  85.     $msg = 'cache is set';
  86. }
  87. $end = microtime(); // get end execution time
  88. // show messages if debug is enabled
  89. if ($debug) {
  90.     echo $msg.': ';
  91.     echo t_exec($start$end);
  92. }
  93. // time diff
  94. function t_exec($start$end) {
  95.     $t = (getmicrotime($end) - getmicrotime($start));
  96.     return round($t,5);
  97. }
  98. // get time
  99. function getmicrotime($t) {
  100.     list($usec$sec) = explode(" ",$t);
  101.     return ((float)$usec + (float)$sec);
  102. }
  103. ?>
2、 你也可以直接点击备用下载: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和分类、首页等内容不会实时更新,我们需要手动更新或者利用自动更新命令。 文章来源:免费资源部落
标签:
本文原创,作者:萨龙龙,其版权均为萨龙网络所有。
如需转载,请注明出处:https://salongweb.com/amh-redis-wordpress.html
萨龙龙

萨龙龙

侠客
一个平平常常的人,热爱生活、旅行和摄影,骑行去过西藏,一直计划再去。14年从江西骑行来到大理,现栖居于洱海边,食人间烟火,过简单生活,做简约设计!
1342.20M456.65W3.85W
分享:
WordPress循环中判断为每篇文章设计不同的样式
WordPress循环中判断为每篇文章设计不同的样式上一篇
翻译WordPress强大实用的Redux框架选项下一篇
翻译WordPress强大实用的Redux框架选项
相关文章
总数:114

WordPress 主题 Slearn Pro 产品插件之属性设置

产品属性可在后台「产品——产品选项——属性」中单独设置(也可在编辑产品时再次添加),有三种类型:文本、颜色和图片,在前台也是以这三种方式来显示。…
萨龙龙萨龙龙
WordPress
3年前
0
0
2.45W
0

Adobe photoshop设计时如何使用图标字体

2014年萨龙龙都写了使用CSS3@font-face打造个性化web字体和字体设计软件FontCreator定制个性化的中文WEB字体这两篇…
萨龙龙萨龙龙
软件系统
9年前
0
0
1.50W
0

WordPress 限制上传媒体的文件类型

MNews和LensNews主题都有前台投稿的功能,用户可以上传图片等文件,管理员是不希望用户上传任意的媒体文件,这时我们可以对用户上传的…
萨龙龙萨龙龙
WordPress
7年前
0
0
1.18W
0

WordPress网站统计相关信息使用大全

很多个人博客都有一个小工具是来统计网站的一些信息,Deephoto主题企业版本首页就集成了网站统计,添加了工作天数、完成作品、新闻动态、摄影师、…
萨龙龙萨龙龙
WordPress
10年前
0
0
1.18W
0

WordPress获取当前分类的顶级分类ID

在开发吃货主题Chihuo时,要对产品和文章进行筛选,在一级分类下显示所有一级分类和当前一级分类下的所有二级分类,在二级分类下,显示所有一级分类…
萨龙龙萨龙龙
WordPress
9年前
0
0
1.77W
0

WordPress 插入页面函数 wp_insert_post

萨龙网络已经开发了很多WordPress主题,LensNews主题要创建的页面较多,所以导致很多用户来咨询如何设置用户中心,在最新上线的…
萨龙龙萨龙龙
WordPress
7年前
0
0
2.10W
0

Linux VPS云主机使用SSH安装PHP EXIF模块

因为开发了Deephoto主题,要获取照片EXIF信息,主机必须安装PHPEXIF模块才能使用,而之前用的虚拟主机没有安装这个模块,需要更换主…
萨龙龙萨龙龙
软件系统
10年前
0
0
1.51W
0

WordPress循环中判断为每篇文章设计不同的样式

新闻视频类Yewan主题的首页文章布局是不规则,文章的布局多样化,这是如何实现的?其实是很简单的,我们在循环中进行判断,每一篇文章都可以输出不同…
萨龙龙萨龙龙
WordPress
9年前
0
0
1.20W
0

WordPress MU多站点网络共享媒体插件Network Shared Media的使用包括特色图像

WordPressMU虽然是一个程序下可以创建多个站点,但每个站点下的文章、页面、媒体等还是独立的,MU也为不同的站点分配了不同的媒体目录。N…
萨龙龙萨龙龙
WordPress
10年前
0
0
1.79W
0

WordPress MU多站点设置子站点上传路径和文件的URL地址

为了提高网站的访问速度,萨龙网络也将所有的媒体文件放置在二级域名下,WordPress3.5以上的版本,隐藏了后台的媒体(Media)设置页面…
萨龙龙萨龙龙
WordPress
9年前
0
0
1.53W
0

修改Woocommerce商品固定链接其它自定义文章类型适用

在更新Concise主题时集成了前端用户中心和商城插件,发现插件中为商城产品文章设置好了固定链接,所以把萨龙网络的作品集与商城的两种自定义类型的…
萨龙龙萨龙龙
WooCommerce
10年前
0
0
1.36W
0

在WooCommerce中添加“立即购买”按钮直接跳转到结算页面

MNews主题集成了WooCommerce商城插件,有些用户要求增加一个『立即购买』按钮,这篇文章介绍下如何在WooCommerce中添加…
萨龙龙萨龙龙
WooCommerce
7年前
0
0
3.09W
0
评论表单游客 您好,欢迎参与讨论。
请输入昵称
请输入邮箱
请输入网址
0 / 0
评论列表
总数:0
萨龙网络
没有相关内容