Install and Configure Redis on Ubuntu 18.04 and Speed Up WordPress Using Caching

Redis is a very popular fast in-memory data structure storage engine, which is typically used as a database server, cache, and message broker. It is written in the C programming language. It also provides official PHP modules for communication between PHP scripts and the Redis server.

With redis, all data is stored in memory. That is what makes it totally different from other No-SQL storage engines. Usually,  when you access and read some data from the database, you don’t know if the data is in memory(cache) or not, but that is not the case when it comes to Redis.

It’s guaranteed that all data is in memory, however, writing to disk is one of the options you can enable. This means data on memory is the trunk and data on a disk is sort of the backup, You might therefore lost data after last saving to a disk if you suddenly turn off the server. This is actually what makes is perfect for a caching server.

In this article, we will help you with the installation of Redis server along with PHP Redis PHP extensions on an Ubuntu server. This tutorial also works on version 16.04 and 14.04 of Ubuntu. We will also look into how to connect Redis to WordPress to speed up our database queries by caching the responses.

Prerequisites

Log in to your system with sudo privilege account using shell access, to which you need to install Redis. Update the apt-get packages index files and also update existing packages to the newest versions by using the following commands:

ssh [email protected]
sudo apt-get update
sudo apt-get upgrade

Installing Redis with apt-get

With Ubuntu 18.04, the Redis packages are available under default apt repository. apt-get is the default package manager for Ubuntu. For the installation of Redis on an Ubuntu VPS. Run below command from the terminal to install Redis on your machine, after which you have to enable Redis to start on system boot. Also restart Redis service once.

sudo apt-get install redis-server
sudo systemctl enable redis-server.service

Testing the Connection to the Redis Server

Now that we have successfully installed the Redis server, we want to make sure its running properly and has no issues. Fortunately, the Redis binaries comes with a tool that allows us to do just that. It’s called the redis-cli tool. Use the redis-cli tool to verify the connection between Redis server and redis-cli.

redis-cli

127.0.0.1:6379> ping
PONG
127.0.0.1:6379>

Install Redis PHP Extension

Now, if you need to use Redis from PHP applications like WordPress database and page caching, you also need to install Redis PHP extension on your Ubuntu system. Run below command to install:

sudo apt-get install php-redis

The command above basically installs all the required PHP modules so that your website can leverage Redis as a caching engine. When using plugins like W3 Total Cache, the option to use Redis becomes available after installing this plugin.

Using Redis with WordPress

There are many ways to connect WordPress with Redis to speed up database queries. The easiest way to connect WordPress to Redis is by using a plugin called Redis Object Cache. We will go over more complex approaches in a separate article. For today, simply install the plugin via the WordPress dashboard and then navigate to the settings. You should see a screen like the image displayed below:


The plugin should automatically detect that you have Redis installed and should start using with your WordPress website. You can view more detailed configuration in the README file of the plugin and we will cover that in a separate article.

Conclusion

In conclusion we want to go back to the configuration of Redis. Like most Ubuntu binaries, Redis can be started without a config file, it just uses the out-of-the-box default configuration. Therefore to make any extra parameter changes you can use its configuration file that is: /etc/redis/redis.conf. Edit the Redis configuration file in a text editor to make changes

sudo vim /etc/redis/redis.conf

Update the following values in Redis configuration file according to your requirement. You can increase max memory limit as per available on your server. There are a lot more options to configure, this can be accessed on the redis website.

maxmemory 256mb
maxmemory-policy allkeys-lru

The above configuration tells Redis to remove any key using the LRU algorithm when the max memory of 256mb is reached. Save the changes to the configuration file and restart the Redis service by using the command below.

sudo systemctl restart redis-server.service

We hope you enjoyed reading this article and have learned some useful insights into using Redis to speed up your WordPress website on Ubuntu. Follow us on Twitter and Facebook, or just signup for our newsletter using the form below to get regular updates on WordPress, Linux and other interesting topics.

Install and Configure Redis on Ubuntu 18.04 and Speed Up WordPress Using Caching
Rate this post

Join over 20,000 users and get our best content

Each week we send 1 email to 20,000 smart entrepreneurs just like you. Enter your email below to join the crew.

Thank you for subscribing.

Something went wrong.