How to Install Redis on CentOS 8

Install Redis on CentOS 8

In this article, we will have explained the necessary steps to install and configure Redis on CentOS 8. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Redis is an open-source, in-memory, data structure store with optional disk writes for persistence. It can be used as a key-value database, or as a cache and message broker. Redis features built-in transactions, replication, and support for a variety of data structures such as strings, hashes, lists, sets, and others. Redis can be made highly available with Redis Sentinel and supports automatic partitioning with Redis Cluster.

Install Redis on CentOS

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo dnf install epel-release
sudo dnf update

Step 2. Install Redis on CentOS 8.

The good news, Redis version 5 is included in the default CentOS 8 repositories. To install it run the following commands:

sudo dnf install redis-server

Once the installation is completed, start the Redis service and enable it to start automatically on boot with:

sudo systemctl enable --now redis
sudo systemctl status redis

Step 3. Configure Redis Remote Access.

By default, you can not access Redis from another host because it’s by default bound to the localhost only. To confirm its bound to localhost (127.0.0.1), follow the below instructions:

sudo nano /etc/redis.conf

Locate the line that begins with bind 127.0.0.1 and add your server private IP address after 127.0.0.1:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 192.168.77.36

To take changes effect, restart the Redis server by running the below command:

sudo systemctl restart redis-server

Use the following ss command to verify that the Redis server is listening on your private interface on port 6379:

ss -an | grep 6379

You should see something like below:

tcp    LISTEN     0      128    192.168.77.36:6379            *:*
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*

Then, Allow access to the Redis server only from a specific IP address or IP range. For example, to allow connections only from 192.168.121.0/24, run the following commands:

sudo firewall-cmd --new-zone=redis --permanent
sudo firewall-cmd --zone=redis --add-port=6379/tcp --permanent
sudo firewall-cmd --zone=redis --add-source=192.168.77.0/24 --permanent

Finally, reload FirewallD to tale changes affect:

sudo firewall-cmd --reload

To verify that everything is setup properly, you can try to ping the Redis server from your remote machine using the redis-cli utility which provides a command-line interface to a Redis server:

redis-cli -h <REDIS_IP_ADDRESS> ping

You should see exact below output:

PONG

Congratulation, you have learned how to install and configure Redis on CentOS 8. If you have any questions, please leave a comment below.