Configure Redis Caching to Speed Up WordPress on Ubuntu

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.

The first time a WordPress page is loaded, a database query is performed on the server. Redis remembers, or caches, this query. So, when another user loads the WordPress page, the results are provided from Redis and from memory without needing to query the database.

Install Redis

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

sudo apt install redis-server php-redis

Configure Redis

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.

sudo nano /etc/redis/redis.conf

Add these lines at the end of the file:

maxmemory 256mb
maxmemory-policy allkeys-lru

When changes are complete, save and close the file.

Redis 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.

wget https://assets.digitalocean.com/articles/wordpress_redis/object-cache.php

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

sudo mv object-cache.php /var/www/html/wp-content/

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

Enable Cache Settings in wp-config.php

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

sudo nano /var/www/html/wp-config.php

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

define('WP_CACHE_KEY_SALT', 'example.com');

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

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

define('WP_CACHE', true);

All together, your file should look like this:

 * Authentication Unique Keys and Salts.

. . .

define('NONCE_SALT',       'put your unique phrase here');

define('WP_CACHE_KEY_SALT', 'example.com');
define('WP_CACHE', true);

Save and close the file.

Finally, restart redis-service and apache2.

Monitor Redis

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

sudo redis-cli monitor

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.

Ref: https://www.digitalocean.com/community/tutorials/how-to-configure-redis-caching-to-speed-up-wordpress-on-ubuntu-14-04

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.