How to Setup Redis Object Cache on a WordPress Site
By Adarsh Sojitra on May 19th, 2019
Redis is one of the most popular caching tool for a WordPress website. Redis is used for many purposes, However, We can also use it as caching system for our WordPress blog/website. In this guide, we will configure Redis Object cache on a WordPress site, And we will test the caching system to make sure it is working. It is not so hard to configure Redis cache on WordPress.
Before we get started with the actual step-by-step tutorial, Here are some prerequisites to follow this guide.
- A VPS/Dedicated Server with Redis Installed.
- A WordPress website hosted on that server.
If you have a WordPress website hosted on a server with Redis installed, we can get started.
Install Redis Object Cache Plugin
First of all, go to the admin panel of your WordPress site and navigate to “Add plugin” page. On the page, you will see lots of plugins from WordPress’s plugin repository.
Now, Search for “Redis Object Cache” plugin and install the one given in the screenshot below.
Once installed, Activate it by clicking on the “Activate” button. Before we can take advantage of the powerful caching system, we first have to make sure that we have PHP Redis extension installed on our server.
Depending on your server and PHP version you are using, Execute a command in following format to install php-redis extension.
sudo apt-get install php7.0-redis -y
sudo service php7.0-fpm restart
Once installed, We can finally activate our Redis caching system.
Activate Redis Object Cache
Now, Click on the “Settings” menu from the right sidebar of the WordPress admin panel. And then click on the “Redis” sub-menu inside Settings just like the following image.
Now, we can enable Redis Object Cache by clicking on the Enable Object Cache plugin. But we also have to check if we can login to our Redis server using the information provided in the “Servers” table.
It’s easy to check if we can access Redis or not. Open up your terminal, connect with the server using SSH and then execute the following command to log in to Redis CLI.
Once logged in, execute:
set test working
If you can set the key-value pair without the requirement of the password, you can go ahead and click the Enable Object Cache button to enable caching. But, if you need password to set key-value pairs in your Redis configuration, Append the following code to the end of the wp-config.php file in your WordPress installation directory.
Now, Reload the plugin page and see if password is changed from No to Yes. If it is, we are good to go with the password too! Click on the Enable Object Cache button and enable the caching!
Verify Redis Object Cache
To verify if the Redis object cache is actually working, we can monitor activities in Redis-cli. If it is being used, we will be able to monitor requests coming in.
Open up Redis Command line interface using the command given below:
To monitor requests, execute the following command in Redis Command line interface.
Now, It’s time to monitor! Open up a new window in your browser and open the URL of your WordPress site. In the redis monitor, you must see the logs as given below.
If you can see new logs forming after opening any page on your Website, Congratulations! You are now utilising the power of Redis Object Caching on your WordPress website!
If you want to set up Object caching with Remote Redis Server, You can also define Redis Host, Port, and more directives in the wp-config.php file. It is also possible to connect with a Remote redis master-slave replications and clusters.
If you are facing any issue setting up Redis Object Cache in your WordPress file, Please re-read the guide and follow it step-by-step.