Redis is an open-source in-memory data structure store. It can be used as a DB and cache. It supports various data structures and can be used as a DB server. You can use Redis with a relational database like MYSQL to speed up your website.
In this article, we will configure Redis as a cache on a Wordpress website. It will load your Wordpress website faster. Redis will optimize the time-consuming database queries using its cache.
A Default Wordpress page website without Redis can load around 800 ms and with Redis, you can lower it down to around 450 ms.
How does caching work?
When the WordPress page loads for the first time, a database query will be performed on your server. Redis will remember or cache the same query. Now, when another user tries to load the same Wordpress page, the results will be provided from Redis without querying to the database. It results in faster performance of your website. The object cache of Redis will cache the SQL queries which are required to load the wordpress pages.
Let us start with installing Redis on Ubuntu 18.04 server.
- Run the below update command at the first step.
sudo apt update
- Install Redis with the below command.
sudo apt install redis-server
- Once Redis is installed, we will need to configure it as a cache.
- Add the below line at the end of your redis.conf file. Use the below command to perform this action.
- Save and Close the redis.conf file.
- Now, we will download the object-cache.php script file. It is a third-party script. You can read the comments to see how it works.
- Move this file to /wp-content folder of Wordpress installation using the below command.
sudo mv object-cache.php /var/www/html/wp-content/
- We will enable the cache settings in your Wordpress config file. To do this edit the wp-config.php
- Add the below line after the * Authentication Unique Keys and Salts. Section:
- To create a persistent cache with the Redis object cache plugin, add the below line after the define('WP_CACHE_KEY_SALT', 'yourwebsite.com'); line.
- Save and close the file.
- Let us restart the Redis and Apache file.
- Restart Redis:
sudo service redis-server restart
- Restart Apache
sudo service apache2 restart
This is it! Your WordPress website is now using Redis caching. Your Wordpress web page will load much faster now.