Like caching, the front-end static files play an important role in improving the site speed. Similarly, we need to cache the backend to enhance server performance. Therefore, we will use the Redis Object Cache.
The Redis Object Cache helps store the complex MySQL queries on RAM. Hence, next time any similar query requests, the Object cache will serve the output stored in RAM rather than hitting the new MySQL query. Thus, enhancing the overall speed and performance.
In this article, we will cover how to install and configure the Redis Server and enable the Redis Object Cache using the WordPress plugin.
Let’s dive into configuring the Redis object cache for the WordPress blog —
Note: The post has been updated to support the latest PHP version, however, the screenshot/images still reflect the older version.
Install Redis Server
Execute the below command to update the APT repository of our Ubuntu Cloud Server.
sudo apt-get update
Install the redis-server as well as the php7.4-redis module for supporting the WordPress Object caching.
sudo apt-get install redis-server php7.4-redis
Configure Redis Server
Now that we have installed the Redis server, we will adjust the configuration file to improve its default configuration. Edit the redis.conf using below command:
sudo nano /etc/redis/redis.conf
And add the below lines to set the maximum memory limit the Redis Server should use for processing the Object Cache.
#You can adjust this value as you see fit - 200mb or 20000mb #it depends on how much RAM you have. On a 1GB server, I use 100mb. maxmemory 100mb # this forces old keys to be deleted using first-in-first-out maxmemory-policy allkeys-lru
Further down in the same configuration file. Comment these lines in Redis to use only memory instead of the storage disk.
#save 900 1 #save 300 10 #save 60 10000
The SSD disk writing speed is relatively low compared to RAM; hence we will be saving the Redis in the RAM cache instead of the disk.
Now, close the configuration file and restart the Redis server and php7.4-fpm using the below command:
sudo service redis-server restart sudo service php7.4-fpm restart
The Redis server has been configured and running on the Ubuntu Cloud server.
Setup Redis Object Cache for WordPress
We have completed the backend setup of the Redis Server. Now we will set up the Redis to run as Object Cache for WordPress Database queries.
Follow the below steps for Redis Object Cache on WordPress:
- Login to WordPress Dashboard
- Navigate to Add New under the Plugin menu
- Search and Install Redis Object Cache by Till Kruss
- Activate the Plugin and navigate to Settings > Redis
- Hit on Enable Object Cache command button
- That’s it!
The Plugin will automatically add the required files in our WordPress server directory and enable the Redis Cache for MySQL database optimization.
Monitoring Redis Object Cache
We have completed the setup with Redis Server configuration and used it on WordPress for Object Cache. To monitor Redis Object Cache performance, we can run the following command in the SSH console.
This will output the real-time data of Object Caching used by WordPress.
Video of Redis Object Cache for WordPress
Watch the video demo of the Redis Object Cache setup for the WordPress blog. I’ve recorded the step-by-step guide of the entire Redis Server guide in this tutorial.
I hope you liked the video; please subscribe to our channel.
What’s next in the #CloudServer series?
Now that we’ve learned about setting up Redis Object Cache with WordPress. Next, we will manage the WordPress MySQL database using WP-Optimize Plugin.