People are doing a lot to boost their WordPress website’s performance. Many are spending money on buying plugins, many are purchasing pricy hostings and whatnot. Here in this tutorial, I will show you how you can install and configure Redis as an object cache for WordPress.
Prerequisites
To set up Redis, you need a configured WordPress server and a root or sudo privileged user. If you haven’t set up your server yet, don’t worry, follow our Intial server configuration guide.
When you are done with the initial server configuration, log in to your server with sudo user and follow the steps below.
Step 1 – Installing Redis
The first thing before installing any package is to update the repositories.
$ sudo apt update
As the updation completes, run the following command to install redis-server
$ sudo apt install redis-server -y
The above command will install Redis on your server. Now, we will have to configure Redis to use our server. Open redis.conf by running the command given below
$ sudo nano /etc/redis/redis.conf
As the file opens, search for “supervised” directive and make changes as shown below
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd
Press Ctrl+x, “y” and hit “Enter” to save
Enable and restart Redis service using the command given below
$ sudo systemctl restart redis.service
Step 2 – Testing Redis setup
To ensure whether Redis is working absolutely fine, let’s test it. First, let us check its status by the following command
$ sudo systemctl status redis
Output
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2018-06-27 18:48:52 UTC; 12s ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Process: 2421 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 2424 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
Main PID: 2445 (redis-server)
Tasks: 4 (limit: 4704)
CGroup: /system.slice/redis-server.service
└─2445 /usr/bin/redis-server 127.0.0.1:6379
To test whether our Redis setup is functioning properly, let’s connect with Redis using the following command
$ redis-cli
127.0.0.1>ping
Output
PONG
As we gave the “ping” command to Redis, it replied to us with “Pong”. This shows that our Redis setup is responding and is working absolutely fine.
Step 3 – Binding Redis to localhost
Since we have configured Redis to work on our localhost, we will have to bind Redis to our localhost port.
$ sudo nano /etc/redis/redis.conf
Find the line given below and if it is commented (#), then remove # from the front.
bind 127.0.0.1 ::1
Press Ctrl+x, “y” and hit “Enter” to save
Step 4 – Securing Redis using password (optional)
Although security is very important, still we would say that the given below steps are optional. So, if you don’t want to secure your Redis, then you can ignore the given below steps.
But if you want to secure, then follow the steps below
Open redis.conf file by
$ sudo nano /etc/redis/redis.conf
Search for the directive given below
# requirepass foobared
Change it as given below and enter high security password of your choice
requirepass password
Press Ctrl+x, “y” and hit “Enter” to save
Restart the Redis service
$ sudo systemctl restart redis-service
Now, open your “wp-config.php” file and add the constant given below to the end of the file. Make sure to replace “password” with the redis.conf “password” that you have configured in step 4
define('WP_REDIS_PASSWORD', 'password');
That’s all you have to do at your server end.
Now, open your WordPress admin and install the “Redis Object Cache” plugin, activate it, and click on “Enable” so that you can manage the Redis object cache right from your WordPress dashboard.