Install Redis Caching to Speed Up WordPress on Ubuntu

In this tutorial, Redis will be configured as a cache for WordPress to alleviate the redundant and time-consuming database queries used to render a WordPress page. The result is a WordPress site which is much faster, uses less database resources, and provides a tunable persistent cache.

redis_cashing_for_wordpress

Redis is an open-source key value store that can operate as both an in-memory store and as cache. Redis is a data structure server that can be used as a database server on its own, or paired with a relational database like MySQL to speed things up, as we’re doing in this tutorial.

Default WordPress home page without Redis:

804ms page load time

Default WordPress home page with Redis:

449ms page load time

Redis vs. Memcached

Memcached is also a popular cache choice. However, at this point, Redis does everything Memcached can do, with a much larger feature set. This Stack Overflow page has some general information as an overview or introduction to persons new to Redis.

Step 1 — Install Redis

In order to use Redis with WordPress, two packages need to be installed: redis-server and php5-redis. The redis-server package provides Redis itself, while the php5-redis package provides a PHP extension for PHP applications like WordPress to communicate with Redis.

Install the softare:

Step 2 — Configure Redis as a Cache

Redis can operate both as a NoSQL database store as well as a cache. For this guide and use case, Redis will be configured as a cache. In order to do this, the following settings are required.

Edit the file /etc/redis/redis.conf and add the following lines at the bottom:

Add these lines at the end of the file:

When changes are complete, save and close the file.

Step 3 — Obtain Redis Cache Backend Script

This PHP script for WordPress was originally developed by Eric Mann. It is a Redis object cache backend for WordPress.

Download the object-cache.php script. This download is from DigitalOcean’s asset server, but this is a third-party script. You should read the comments in the script to see how it works.

Download the PHP script:

Move the file to the /wp-content directory of your WordPress installation:

Depending on your WordPress installation, your location may be different.

Step 4 — Enable Cache Settings in wp-config.php

Next, edit the wp-config.php file to add a cache key salt with the name of your site (or any string you would like).

Add this line at the end of the * Authentication Unique Keys and Salts. section:

You can use your domain name or another string as the salt.

Note: For users hosting more than one WordPress site, each site can share the same Redis installation as long as it has its own unique cache key salt.

Also, add the following line after the WP_CACHE_KEY_SALT line to create a persistent cache with the Redis object cache plugin:

All together, your file should look like this:

* Authentication Unique Keys and Salts.

Step 5 — Restart Redis and Apache

Finally, restart redis-service and apache2.

Restart Redis:

Restart Apache:

Restart php5-fpm if you are using it; this is not part of the basic installation on DigitalOcean:

That’s it! Your WordPress site is now using Redis caching. If you check your page load speeds and resource use, you should notice improvements.

Monitor Redis with redis-cli

To monitor Redis, use the redis-cli command like so:

When you run this command, you will see the real-time output of Redis serving cached queries. If you don’t see anything, visit your website and reload a page.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

CDN by MaxCDN