Tutorial

How To Configure Redis Caching to Speed Up WordPress on Ubuntu 14.04

How To Configure Redis Caching to Speed Up WordPress on Ubuntu 14.04

Introduction

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.

For 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. This guide applies to Ubuntu 14.04.

While every site is different, below is an example benchmark of a default Wordpress installation home page with and without Redis, as configured from this guide. Chrome developer tools were used to test with browser caching disabled.

Default WordPress home page without Redis:

804ms page load time

Default WordPress home page with Redis:

449ms page load time

Note: This implementation of Redis caching for WordPress relies on a well-commented but third-party script. The script is hosted on DigitalOcean’s asset server, but was developed externally. If you would like to make your own implementation of Redis caching for WordPress, you will need to do some more work based on the concepts presented here.

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.

How does the caching work?

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.

The Redis implementation used in this guide works as a persistent object cache for WordPress (no expiration). An object cache works by caching the SQL queries in memory which are needed to load a WordPress page.

When a page loads, the resulting SQL query results are provided from memory by Redis, so the query does not have to hit the database. The result is much faster page load times, and less server impact on database resources. If a query is not available in Redis, the database provides the result and Redis adds the result to its cache.

If a value is updated in the database (for example, a new post or page is created in WordPress) the Redis value for that query is invalidated to prevent bad cached data from being presented.

If you run into problems with caching, the Redis cache can be purged by using the flushall command from the Redis command line:

redis-cli

Once you see the prompt, type:

flushall

Additional Reference: WordPress Object Cache Documentation

Prerequisites

Before starting this guide, you’ll need to set up a sudo user and install WordPress.

  • Ubuntu 14.04 Droplet (1 GB or higher recommended)
  • Add a sudo user
  • Install WordPress. This guide has been tested with these instructions, although there are many ways to install WordPress

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:

sudo apt-get install redis-server php5-redis

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:

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.

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:

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.

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

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.

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:

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.

Step 5 — Restart Redis and Apache

Finally, restart redis-service and apache2.

Restart Redis:

sudo service redis-server restart

Restart Apache:

sudo service apache2 restart

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

sudo service php5-fpm restart 

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:

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.

Below is example output from a WordPress site configured per this guide using Redis:

OK
1412273195.815838 "monitor"
1412273198.428472 "EXISTS" "example.comwp_:default:is_blog_installed"
1412273198.428650 "GET" "example.comwp_:default:is_blog_installed"
1412273198.432252 "EXISTS" "example.comwp_:options:notoptions"
1412273198.432443 "GET" "example.comwp_:options:notoptions"
1412273198.432626 "EXISTS" "example.comwp_:options:alloptions"
1412273198.432799 "GET" "example.comwp_:options:alloptions"
1412273198.433572 "EXISTS" "example.comwp_site-options:0:notoptions"
1412273198.433729 "EXISTS" "example.comwp_:options:notoptions"
1412273198.433876 "GET" "example.comwp_:options:notoptions"
1412273198.434018 "EXISTS" "example.comwp_:options:alloptions"
1412273198.434161 "GET" "example.comwp_:options:alloptions"
1412273198.434745 "EXISTS" "example.comwp_:options:notoptions"
1412273198.434921 "GET" "example.comwp_:options:notoptions"
1412273198.435058 "EXISTS" "example.comwp_:options:alloptions"
1412273198.435193 "GET" "example.comwp_:options:alloptions"
1412273198.435737 "EXISTS" "example.comwp_:options:notoptions"
1412273198.435885 "GET" "example.comwp_:options:notoptions"
1412273198.436022 "EXISTS" "example.comwp_:options:alloptions"
1412273198.436157 "GET" "example.comwp_:options:alloptions"
1412273198.438298 "EXISTS" "example.comwp_:options:notoptions"
1412273198.438418 "GET" "example.comwp_:options:notoptions"
1412273198.438598 "EXISTS" "example.comwp_:options:alloptions"
1412273198.438700 "GET" "example.comwp_:options:alloptions"
1412273198.439449 "EXISTS" "example.comwp_:options:notoptions"
1412273198.439560 "GET" "example.comwp_:options:notoptions"
1412273198.439746 "EXISTS" "example.comwp_:options:alloptions"
1412273198.439844 "GET" "example.comwp_:options:alloptions"
1412273198.440764 "EXISTS" "example.comwp_:options:notoptions"
1412273198.440868 "GET" "example.comwp_:options:notoptions"
1412273198.441035 "EXISTS" "example.comwp_:options:alloptions"
1412273198.441149 "GET" "example.comwp_:options:alloptions"
1412273198.441813 "EXISTS" "example.comwp_:options:notoptions"
1412273198.441913 "GET" "example.comwp_:options:notoptions"
1412273198.442023 "EXISTS" "example.comwp_:options:alloptions"
1412273198.442121 "GET" "example.comwp_:options:alloptions"
1412273198.442652 "EXISTS" "example.comwp_:options:notoptions"
1412273198.442773 "GET" "example.comwp_:options:notoptions"
1412273198.442874 "EXISTS" "example.comwp_:options:alloptions"
1412273198.442974 "GET" "example.comwp_:options:alloptions"

Press CTRL-C to stop the output.

This is useful for seeing exactly what queries Redis is processing.

Conclusion

After following this guide, WordPress will now be configured to use Redis as a cache on Ubuntu 14.04.

Below are some additional security and administration guides for WordPress that may be of interest:

How To Configure Secure Updates and Installations in WordPress on Ubuntu

How To Use WPScan to Test for Vulnerable Plugins and Themes in Wordpress

How To Use WP-CLI to Manage your WordPress Site from the Command Line

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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);

WP_CACHE is for PAGE CACHING ONLY

Switching it on , results in a warning that wordpress can not find the relative cache plugin.

The object-cache.php file from this tutorial slowed down my site very much. Use this object-cache.php file instead for super quick load times! WP Redis

After installing the plugin;

  1. Move the object-cache.php from the plugins’ directory to wp-content
  2. Add $redis_server = array( 'host' => '127.0.0.1', 'port' => 6379); to wp-config.php, along with the “defines” from this tutorial. (WP_CACHE_KEY_SALT and WP_CACHE)
  3. Restart PHP/HHVM, Apache/NGINX and Redis Server
  4. UP UP AND AWAAAY!

Using this live here: https://sverige.science/

I’m getting DOMContentLoaded in approx 500ms now! :)

Slowed down my wp site (512mb) by more than 200% Disabled all other server caching options as recommended - reboot - filled cache - no change

P.S.: we need a “thumbs down” button so other people know beforehand that this doesn’t work for “many people”

Awesome article! First redis-wordpress tutorial that actually worked properly for me.

UPDATE: For some weird reason, it slowed down my website??? Derp.

Hi,

Products are not adding to the cart. Flushing Redis cache solves this problem. But over the time it recreates.

Any bypass options for cart and checkout?

Installation Done perfectly,But after adding into wp-config.php im getting 505 error…Looking at error_log shows the below :

[11-May-2017 10:43:18 UTC] PHP Fatal error: Class ‘Redis’ not found in /home/acctname/public_html/wp-content/object-cache.php on line 332

Please how to fix this ?

I’ve got this up and running on my site and everything seems much faster. However, I’m wondering what’s the difference between implementing Redis this way, and Jim Westergreen’s method, which uses a script for index-with-redis and Predis:

https://www.jimwestergren.com/wordpress-with-redis-as-a-frontend-cache/

Is one better than the other? Can both be used at the same time? Westergreen claims he uses no caching plugins, which would be great (I’ve got w3tc set up pretty well, but…it has its issues).

I’m also wondering, will this method cause issues with shopping carts, FB like counters, and such?

Thanks!

Hi. Nice post but where i must include the Redis-DB Password for more security?

There are many advance caching systems available that can be used to speed up WordPress. Redis has a caching mechanism that can be used to speed up object cache of WordPress thus improving database queries. Above Setup is pre configure in Cloudways platform. DigitalOcean Servers are integrated in Cloudways. Integrating it with DigitalOcean WordPress is easy, follow this tutorial for a quick guide: Redis Cache with WordPress

This comment has been deleted

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.