Knowledgebase

How to Setup Redis Caching for WordPress with Ubuntu 20.04 and Nginx Print

  • 0

Introduction

Redis is an open-source key-value data store. Redis stores data in RAM, so accessing the data is quite fast. Because of that Redis is often used as a cache. This guide explains how to set up Redis as a cache for WordPress websites to boost performance.

Prerequisites

Before you begin, you should:

1. Install Redis

We need to install Redis and the PHP extension that will allow PHP applications such as WordPress to communicate with Redis.

sudo apt update
sudo apt install redis-server php-redis

After that, restart PHP FPM and Nginx.

sudo systemctl restart php7.4-fpm
sudo nginx -s reload

2. Setup Redis as Cache

Edit the /etc/redis/redis.conf file :

sudo nano /etc/redis/redis.conf

After the last line, add these :

maxmemory 128mb
maxmemory-policy allkeys-lru

Set the maxmemory according to the amount of RAM available in your server.

Restart Redis server :

sudo systemctl restart redis-server

3. Configure WordPress Cache Salt

Edit your wp-config.php file and add the following lines at the end of the "Authentication Unique Keys and Salts" section.

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

Note that you can set the value of WP_CACHE_KEY_SALT with any string you like but it must be unique for every WordPress site on your server. The recommended way is to use your domain name.

4. Install Redis Object Cache Plugin

Visit the Plugin > Add New page inside your WordPress admin and search the "Redis Object Cache" plugin. Click "Install Now" and then activate the plugin.

You can also download the plugin from the WordPress repository and upload it yourself.

After installing the plugin, you also need to copy the object-cache.php file from the plugin directory wp-content/plugins/redis-cache/includes/object-cache.php to the wp-content directory.

cp /your-wordpress-directory/wp-content/plugins/redis-cache/includes/object-cache.php /your-wordpress-directory/wp-content/

5. Fix Race Conditions

Redis Object Cache plugin cache the output of wp_load_alloptions() function. It contains all options where the autoload column is equal to yes. This will help boost your website performance, but there is an issue that you may end up with a race condition when updating your options. So, when you want to update your WordPress setting, it may not be reflected right away or even won't be saved.

The solution for that is to add the following script in your functions.php file.

function vultr_maybe_clear_alloptions_cache( $option ) {
    if ( ! wp_installing() ) {
        $alloptions = wp_load_alloptions(); 
        if ( isset( $alloptions[ $option ] ) ) { 
            wp_cache_delete( 'alloptions', 'options' );
        }
    }
}
add_action( 'added_option',   'vultr_maybe_clear_alloptions_cache' );
add_action( 'updated_option', 'vultr_maybe_clear_alloptions_cache' );
add_action( 'deleted_option', 'vultr_maybe_clear_alloptions_cache' );

This script will delete the alloptions cache whenever you add, update or delete an option.

6. Testing Your Configurations

Visit the Settings > Redis page in your WordPress admin area. You should see the status is connected in the overview tab.

Run this command in your server and then navigate around your WordPress site.

redis-cli monitor

If you see logs appear and change as you navigate around, that means WordPress is successfully connected to Redis, and you can enjoy the performance boost from using Redis as cache.

Introduction Redis is an open-source key-value data store. Redis stores data in RAM, so accessing the data is quite fast. Because of that Redis is often used as a cache. This guide explains how to set up Redis as a cache for WordPress websites to boost performance. Prerequisites Before you begin, you should: Deploy a fully updated Ubuntu 20.04 server. Create a non-root user with sudo privileges. Install LEMP stack Install WordPress on LEMP stack 1. Install Redis We need to install Redis and the PHP extension that will allow PHP applications such as WordPress to communicate with Redis. sudo apt update sudo apt install redis-server php-redis After that, restart PHP FPM and Nginx. sudo systemctl restart php7.4-fpm sudo nginx -s reload 2. Setup Redis as Cache Edit the /etc/redis/redis.conf file : sudo nano /etc/redis/redis.conf After the last line, add these : maxmemory 128mb maxmemory-policy allkeys-lru Set the maxmemory according to the amount of RAM available in your server. Restart Redis server : sudo systemctl restart redis-server 3. Configure WordPress Cache Salt Edit your wp-config.php file and add the following lines at the end of the "Authentication Unique Keys and Salts" section. define('WP_CACHE', true); define('WP_CACHE_KEY_SALT', 'yourwebsite.com'); Note that you can set the value of WP_CACHE_KEY_SALT with any string you like but it must be unique for every WordPress site on your server. The recommended way is to use your domain name. 4. Install Redis Object Cache Plugin Visit the Plugin > Add New page inside your WordPress admin and search the "Redis Object Cache" plugin. Click "Install Now" and then activate the plugin. You can also download the plugin from the WordPress repository and upload it yourself. After installing the plugin, you also need to copy the object-cache.php file from the plugin directory wp-content/plugins/redis-cache/includes/object-cache.php to the wp-content directory. cp /your-wordpress-directory/wp-content/plugins/redis-cache/includes/object-cache.php /your-wordpress-directory/wp-content/ 5. Fix Race Conditions Redis Object Cache plugin cache the output of wp_load_alloptions() function. It contains all options where the autoload column is equal to yes. This will help boost your website performance, but there is an issue that you may end up with a race condition when updating your options. So, when you want to update your WordPress setting, it may not be reflected right away or even won't be saved. The solution for that is to add the following script in your functions.php file. function vultr_maybe_clear_alloptions_cache( $option ) { if ( ! wp_installing() ) { $alloptions = wp_load_alloptions(); if ( isset( $alloptions[ $option ] ) ) { wp_cache_delete( 'alloptions', 'options' ); } } } add_action( 'added_option', 'vultr_maybe_clear_alloptions_cache' ); add_action( 'updated_option', 'vultr_maybe_clear_alloptions_cache' ); add_action( 'deleted_option', 'vultr_maybe_clear_alloptions_cache' ); This script will delete the alloptions cache whenever you add, update or delete an option. 6. Testing Your Configurations Visit the Settings > Redis page in your WordPress admin area. You should see the status is connected in the overview tab. Run this command in your server and then navigate around your WordPress site. redis-cli monitor If you see logs appear and change as you navigate around, that means WordPress is successfully connected to Redis, and you can enjoy the performance boost from using Redis as cache.

Was this answer helpful?
Back

Powered by WHMCompleteSolution