歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 讓 WordPress 使用 Redis 緩存來進行加速

讓 WordPress 使用 Redis 緩存來進行加速

日期:2017/3/1 10:17:49   编辑:Linux編程

Redis 是一個高級的 key-value 存儲系統,類似 memcached,所有內容都存在內存中,因此每秒鐘可以超過 10 萬次 GET 操作。

我下面提出的解決方案是在 Redis 中緩存所有輸出的 HTML 內容而無需再讓 WordPress 重復執行頁面腳本。這裡使用 Redis 代替 Varnish 設置簡單,而且可能更快。

安裝 Redis

如果你使用的是 Debian 或者衍生的操作系統可使用如下命令安裝 Redis:

1 apt-get install redis-server

或者閱讀 安裝指南

使用 Predis 作為 Redis 的 PHP 客戶端

你需要一個客戶端開發包以便 PHP 可以連接到 Redis 服務上。

這裡我們推薦 Predis. 上傳 predis.php 到 WordPress 的根目錄。

前端緩存的 PHP 腳本

步驟1: 在 WordPress 的根目錄創建新文件 index-with-redis.php ,內容如下:

001 <?php 002 003 // Change these two variables: 004 005 $seconds_of_caching = 60*60*24*7; // 7 days. 006 007 $ip_of_this_website = '204.62.14.112'; 008 009 010 011 /* 012 013 - This file is written by Jim Westergren, copyright all rights reserved. 014 015 - See more here: www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/ 016 017 - The code is free for everyone to use how they want but please mention my name and link to my article when writing about this. 018 019 - Change $ip_of_this_website to the IP of your website above. 020 021 - Add ?refresh=yes to the end of a URL to refresh it's cache 022 023 - You can also enter the redis client via the command prompt with the command "redis-cli" and then remove all cache with the command "flushdb". 024 025 */ 026 027 028 029 // Very necessary if you use Cloudfare: 030 031 if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { 032 033 $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; 034 035 } 036 037 038 039 // This is from WordPress: 040 041 define('WP_USE_THEMES', true); 042 043 044 045 // Start the timer: 046 047 function getmicrotime($t) { 048 049 list($usec, $sec) = explode(" ",$t); 050 051 return ((float)$usec + (float)$sec); 052 053 } 054 055 $start = microtime(); 056 057 058 059 // Initiate redis and the PHP client for redis: 060 061 include("predis.php"); 062 063 $redis = new Predis\Client(''); 064 065 066 067 // few variables: 068 069 $current_page_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 070 071 $current_page_url = str_replace('?refresh=yes', '', $current_page_url); 072 073 $redis_key = md5($current_page_url); 074 075 076 077 // This first case is either manual refresh cache by adding ?refresh=yes after the URL or somebody posting a comment 078 079 if (isset($_GET['refresh']) || substr($_SERVER['REQUEST_URI'], -12) == '?refresh=yes' || ($_SERVER['HTTP_REFERER'] == $current_page_url && $_SERVER['REQUEST_URI'] != '/' && $_SERVER['REMOTE_ADDR'] != $ip_of_this_website)) { 080 081 require('./wp-blog-header.php'); 082 083 $redis->del($redis_key); 084 085 086 087 // Second case: cache exist in redis, let's display it 088 089 } else if ($redis->exists($redis_key)) { 090 091 $html_of_current_page = $redis->get($redis_key); 092 093 echo $html_of_current_page; 094 095 echo "<!-- This is cache -->"; 096 097 098 099 // third: a normal visitor without cache. And do not cache a preview page from the wp-admin: 100 101 } else if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website && strstr($current_page_url, 'preview=true') == false) { 102 103 require('./wp-blog-header.php'); 104 105 $html_of_current_page = file_get_contents($current_page_url); 106 107 $redis->setex($redis_key, $seconds_of_caching, $html_of_current_page); 108 109 echo "<!-- Cache has been set -->"; 110 111 112 113 // last case: the normal WordPress. Should only be called with file_get_contents: 114 115 } else { 116 117 require('./wp-blog-header.php'); 118 119 } 120 121 122 123 124 125 // Let's display some page generation time (note: CloudFlare may strip out comments): 126 127 $end = microtime(); 128 129 $t2 = (getmicrotime($end) - getmicrotime($start)); 130 131 if ($_SERVER['REMOTE_ADDR'] != $ip_of_this_website) { 132 133 echo "<!-- Cache system by Jim Westergren. Page generated in ".round($t2,5)." seconds. -->"; 134 135 } 136 137 ?>

或者直接下載 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 秒

我已經在我的博客中使用了如上的方法進行加速很長時間了,一切運行良好。

其他建議

我的環境是 Nginx + PHP-FPM + APC + Cloudflare + Redis. 安裝在一個 nano VPS 中,無緩存插件。

請確認使用了 gzip 壓縮,可加快訪問速度。

訪問 wp-admin

要訪問 wp-admin 必須使用 /wp-admin/index.php 代替原來的 /wp-admin/.

Copyright © Linux教程網 All Rights Reserved