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 NeedA 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 RedisIf 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 RedisPlease 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.