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. I’m too lazy to write a nginx + php-fpm installation guide now but you can refer to this article to setup the necessary applications on your 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. ^_^