Lightning Fast WordPress with Nginx + Redis

in WordPress


I had the good opportunity of ending up on Jim Westgren’s article about using Redis as a front end cache and it didn’t take long for me to try it on one of my virtual boxes. The results were unbelievable, pages that were taking about 0.2 to 1.2 seconds were now loading at 0.0025 second on average.

Unfortunately, Jim is a bit busy showing us how he configured his server to run on nginx + php-fpm + apc + cloudflare + redis. So in doing my share to make (a part of) the web faster and better for everyone here’s an install guide to get you started.

What You Need

A virtual private server (VPS), this unfortunately is required. If you are in a shared hosting environment, sorry but this isn’t for you. If you want to jump in managing your own VPS, I highly recommend DigitalOcean, cheapest and fastest that I’ve known so far, the one I currently use for all my sites.

The following applications, nginx, php-fpm, redis and predis, a flexible php client library for redis.

Finally, at least a very basic knowledge in using linux based systems. I prefer using CentOS and the server that this blog is on is using this distro.

Setting up Redis

If you’ve got all three covered, let’s get started. This redis config works well with this centos + nginx + php-fpm installation in a vps. Install nginx and php-fpm, make sure it’s configured properly to run WordPress.

Install redis on your server, here’s a clean and easy to follow guide for installing redis on CentOS. Make sure Redis is running on your server before continuing.

Setup a WordPress blog on your vps. While I don’t have a good guide yet based on my setup, I recommend you read this guide to get you started.

Installing WP Index Redis

Please download wp-index-redis.php, you’ll need this to interface WP with Redis.

I really don’t have a name for this script but, I’ll just call it it WP Index Redis for now. I’ve made a lot of changes to the script that Jim originally created but here are the main difference:

  • Pages are not cached when you are logged in.
  • Cached pages do not expire not unless explicitly deleted or reset (deleting the entire domain cache).
  • Appending a ?c=y (e.g. domain.com/?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.
  • Refreshing (F5) a page deletes the cache of that page.
  • Script still works even if allow_fopen is disabled in php.
  • Submitting a comment deletes the cache of that page.
  • Includes a debug mode, stats are displayed at the bottom most part after </html>. Won’t be deleted by CloudFlare.

Like always, use this at your own risk. Be comforted though that I use this myself on this very blog. So on to installing the redis cache interface:

Edit wp-index-redis.php and change some variables where appropriate. The default should work out of the box, but if you are using CloudFlare set $cf = 1, and if you’d like to see some cache generation times set $debug = 1. Otherwise leave everything as it is.

Upload wp-index-redis.php on your WP blog. Make sure you upload this where your main WordPress index.php resides.

Do the same for predis.php.

Rename the WordPress index.php to _index.php or any name that will indicate that this is the original index.php.

Rename wp-index-redis.php to index.php. That’s it you are all set.

Open your browser and access your site, if you’ve enabled debug, you should see some page execution status and figures at the bottom most part of each WordPress page.

For the curious, this blog is on CentOS with nginx + php-fpm + mysql + redis. I didn’t bother (for now) to install apc or xcache because I believe in the mantra, less is more. In the very near future I should be able to publish a guide on how I’ve setup my vps.

If nothing broke on your site, you can breathe now, congratulate yourself and let your neighbor know that your level of geek has just plus-oned (+1) today. ^_^

 

{ 63 comments… read them below or add one }

Paul July 31, 2014 at 1:18 pm

Does this replace / outperform Varnish as a front end cache?

Both this and Varnish appear to do the same thing (HTTP accelerator), correct me if i’m wrong but i can’t find many google results on comparing the two.

Jeedo November 1, 2014 at 4:15 pm

Hi Paul,

No idea either, haven’t used varnish yet, it’s just because it’s my preference for now.

Jeedo November 1, 2014 at 4:16 pm

Hi Stephen,

Thanks for sharing, sorry took a long time to get back. Got really busy with other things -_-

Jeedo November 1, 2014 at 4:17 pm

Hi Trevor,

Sorry can’t do anything much to help you. I’m doing most of my work on NGINX and we do have servers running Apache but we have other folks taking care of that for us.

Jeedo November 1, 2014 at 4:19 pm

Hi Waseem,

Sorry but this is about Redis and not W3 Total Cache.

Jeedo November 1, 2014 at 4:20 pm

Hi Waseem, this specific configuration works only on WordPress and not static html pages.

@yohanesbule November 9, 2014 at 5:44 am

Hi, any plan on testing with HHVM and pound for security as well?

Christopher December 13, 2014 at 6:03 am

Hey,

really great script !
It`s working great and wordpress is ultra fast with this redis cache.

Homeever is it possible to delete the frontpage cache if a new article is posted ?
Currently i have to delete the cache everytime manually and we post up to 5 posts a day. So it would be great to delete the cache of the frontpage automaticly if a new article is posted.

Nad by the way: You dont have to rename your index.php or what ever. If you`re using nginx you can set up an alias:

This code will load the wp-index-redis.php BEFORE the index.php
And you dont have to rename anything and it`s save for updates.

location /index.php {
alias /var/www/your/site/wp-index-redis.php;
}

location / {
index wp-index-redis.php;
try_files $uri $uri/ /wp-index-redis.php?$args;
}

location /wp-admin/ {
index index.php;
try_files $uri $uri/ /index.php$args;
}

Jeedo December 19, 2014 at 1:56 am

Hi Chris,

Thanks for the tip. Due to me being busy with business and other stuff, left this project for a while, I’m hoping I’ll have time to update this come Christmas break. I’ve noted all your suggestions in this thread.

Cheers,

Jeedo

Yudha January 26, 2015 at 7:36 am

Hi Jeedo

My configuration is nginx as frontend and apache as backend. Will your redis configuration work with my server configuration?

Note: I can’t dump apache. Several plugin need apache to work

Appreciated for your opinion
Yudha

Jeedo February 22, 2015 at 3:32 am

Hi Yuda, it might work but can’t give guarantees. I only tested it on systems running nginx as the webserver.

Mirko April 18, 2015 at 2:49 am

I’m getting encoding errors in firefox on first load of a uncached page. If I refresh the page or the page was already in the redist-storage there is no error. Have you experienced this issue and solved it?

Jeedo April 20, 2015 at 9:54 am

Hi Mirko, could be an error in your setup. Never had those types of error on my end. Sorry can’t help much.

Leave a Comment

Previous post:

Next post: