What is Redis?
Redis is an in-memory data structure store. It can work as a database, cache mechanism, etc.
Any WordPress website stores all its data in a database. For loading a webpage, it runs a database query via PHP scripts.
With Redis running on a server, it stores these queries into system memory. As a result, when another user accesses the same page, it retrieves the results from the memory. In other words, there is no need to make a database query again. Thus, it saves time and improves website performance as such.
In this guide, we will be installing Redis on a Ubuntu server. The commands are the same for Debian.
Begin by logging onto your WordPress site via SSH:
Once logged in, update your server and install Redis:
apt-get update apt-get install redis-server php-redis
After the install completes, verify that Redis has installed and is listening on port 6379:
Verify that Redis is listening on port 6379:
netstat -lpan | grep :6379
We now need to configure the Redis configuration file.
Add the following lines to /etc/redis/redis.conf
maxmemory 256mb maxmemory-policy allkeys-lfu
Before these changes come into effect, you will need to restart Redis and PHP:
systemctl restart redis-server systemctl restart php7.2-fpm
We will need to add the cache variables into our WordPress configuration file found at /var/www/html/wordpress/wp-config.php – your installation path may vary.
define( 'WP_CACHE_KEY_SALT', 'yourdomain.com' ); define( 'WP_CACHE', true );
Enabling the Redis Plugin for WordPress
- Head to Plugins – Add New
- Search for Redis Object Cache – Install and then Activate
- Redis settings will now be available under the general Settings tab – open it
- Enable Redis and if all works it will say “Connected”