WordPress給網站配置Redis 緩衝的例子

來源:互聯網
上載者:User

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


安裝 Redis

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

 apt-get install redis-server

使用 Predis 作為 Redis 的 PHP 用戶端
你需要一個用戶端開發包以便 PHP 可以串連到 Redis 服務上。

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

前端緩衝的 PHP 指令碼

步驟1: 在 WordPress 的根目錄建立新檔案 index-with-redis.php ,內容如下:

 

 代碼如下 複製代碼

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

$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 PredisClient('');

// init vars
$domain = $_SERVER['HTTP_HOST'];
$url = "http://".$_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
(isset($_SERVER['HTTP_CACHE_CONTROL']) && $_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 && !strpos($url, '/feed/')) {

echo $redis->hget($dkey, $ukey);
$cached = 1;
$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 to cache only if the page exist and is not a search result.
if (!is_404() && !is_search()) {
// 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);
}

if ($cached && $display_powered_by_redis) {
// You should move this CSS to your CSS file and change the: float:right;margin:20px 0;
echo "<style>#redis_powered{float:right;margin:20px 0;background:url(yun_qi_img/redis.png) 10px no-repeat #fff;border:1px solid #D7D8DF;padding:10px;width:190px;}
#redis_powered div{width:190px;text-align:right;font:10px/11px arial,sans-serif;color:#000;}</style>";
echo "<a href="http://www.aips.me/wordpress-with-redis-as-a-frontend-cache/" style="text-decoration:none;"><div id="redis_powered"><div>Page generated in<br/> ".t_exec($start, $end)." sec</div></div></a>";
}

// 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);
}

?>


你也可以在 Github 上查看 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 秒
我已經在我的部落格中使用了如上的方法進行加速很長時間了,一切運行良好。

其他建議
本文作者的 WordPress 環境是 Nginx + PHP-FPM + APC + Cloudflare + Redis. 安裝在一個 VPS 中,無快取區外掛程式。

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

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


本文其實在國內已經有很翻譯過了,但我看到作者也一直在更新此文,反而國內譯者都不怎麼更新,我就自己去重新折騰了一遍。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.