Install and Setup Redis for WordPress

Install the Latest Redis Server

Run the command below to install Redis

sudo apt-get update
sudo apt-get install redis-server php-redis

Verify that you have version 4 of Redis installed with the following command:

redis-server --version

Allocate Memory to Redis and Setup an Eviction Policy

Now that Redis is installed, run the commands below to open its configuration file…

sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.bak
sudo nano /etc/redis/redis.conf

Then uncomment the lines below to allow memory and an eviction policy… You’ll have to scroll through the lines to find these… Then set then up as shown below:

maxmemory 256mb
maxmemory-policy allkeys-lru

Configure WordPress wp-config.php file

After the above steps run the commands below to open WordPress wp-config.php file in your WordPress root directory…

sudo nano /var/www/domain_name/dir/wp-config.php

Then add these lines just below WordPress unique Keys and Salts section…

define( 'WP_CACHE_KEY_SALT', 'dir.domain_name_' );
define( 'WP_CACHE', true );

Install a Redis Object Cache Plugin

The final step is to install WordPress Redis Object Cache Plugin

Manually secure port 6379

# Manually secure port 6379
sudo iptables -A INPUT -p tcp --dport 6379 -s 127.0.0.1 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 6379 -j DROP
sudo iptables -L

Redis commands:

Redis start/stop/restart

sudo service redis-server start
sudo service redis-server stop
sudo service redis-server restart

Redis status

sudo systemctl status redis-server

To monitor Redis, use the redis-cli command like so:

redis-cli monitor

Use ss or netstat to verify that Redis is listening on all interfaces on port 6379:

sudo ss -an | grep 6379
sudo iptables -L | grep 6379

source