How To Install Redis on CentOS 7 / RHEL 7 & Ubuntu 18.04 / Ubuntu 16.04

0

Redis is an open-source (BSD licensed), in-memory data structure store. It can be used as a database, cache and message broker. It supports different kinds of abstract data structures such as Strings, Hashes, Lists, Sets, sorted sets, hyperlogs, bitmaps, streams, and spatial indexes.

This post explains to you how to install Redis on CentOS 7 / RHEL 7 & Ubuntu 18.04 / Ubuntu 16.04. Also, help you to set up master/slave replication on Redis.

Install Redis on CentOS 7 / RHEL 7 & Ubuntu 18.04/ Ubuntu 16.04

Redis package available in EPEL repository for CentOS / RHEL and OS repository on Ubuntu is quite a bit old. So, we will be installing the latest version of Redis (v5.0.2) from the Remi repository and chris lea PPA on CentOS / RHEL and Ubuntu respectively.

Set up Repository

Start adding repositories based on the operating systems you are using.

### CentOS 7 / RHEL 7 ###

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh https://rpms.remirepo.net/enterprise/remi-release-7.rpm

# for RHEL only
subscription-manager repos --enable=rhel-7-server-optional-rpms

### Ubuntu 18.04 / Ubuntu 16.04 ###

sudo add-apt-repository ppa:chris-lea/redis-server
sudo apt update

Install Redis

Install the Redis package by typing following command in the terminal.

### CentOS 7 / RHEL 7 ###

yum install -y redis --enablerepo=remi

### Ubuntu 18.04 / Ubuntu 16.04 ###

sudo apt install -y redis-server

Now, start the Redis server and enable it to start automatically on system boot.

### CentOS 7 / RHEL 7 ###

systemctl start redis
systemctl enable redis

### Ubuntu 18.04 / Ubuntu 16.04 ###

sudo systemctl start redis-server
sudo systemctl enable redis-server

Enter the below command to check the status of Redis service.

### CentOS 7 / RHEL 7 ###

systemctl status redis

### Ubuntu 18.04 / Ubuntu 16.04 ###

sudo systemctl status redis-server

You should see something like below.

 redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/redis.service.d
└─limit.conf
Active: active (running) since Wed 2018-11-28 06:57:30 UTC; 14s ago
Main PID: 1898 (redis-server)
CGroup: /system.slice/redis.service
└─1898 /usr/bin/redis-server 127.0.0.1:6379

Nov 28 06:57:30 redisc7 systemd[1]: Starting Redis persistent key-value database...
Nov 28 06:57:30 redisc7 systemd[1]: Started Redis persistent key-value database.

Verify Redis Installation

Verify that Redis is running with redis-cli (command line interface of Redis).

redis-cli ping

If Redis is running, it will return a PONG.

PONG

You now have Redis running on your server.

Configure Redis Remote Access (Optional)

By default, Redis doesn’t allow remote connections. You can connect to the Redis only from 127.0.0.1 (localhost) – the machine where Redis is running.

Perform the below steps only if you want to connect to your Redis server from remote hosts.

Edit the Redis configuration file.

### CentOS 7 / RHEL 7 ###

vi /etc/redis.conf

### Ubuntu 18.04 / Ubuntu 16.04 ###

sudo nano /etc/redis/redis.conf

Find the line that starts with bind 127.0.0.1 and add your server IP address after 127.0.0.1. Then save and close the file.

bind 127.0.0.1 192.168.1.10

Ensure you replace 192.168.1.10 with your IP address.

Restart the Redis server for changes to take effect.

### CentOS 7 / RHEL 7 ###

systemctl restart redis

### Ubuntu 18.04 / Ubuntu 16.04 ###

systemctl restart redis-server

Use the following netstat command to verify that the Redis server is listening on your server interface on port 6379.

netstat -antup | grep -i 6379

You should see something like below.

tcp 0 0 10.142.0.10:6379 0.0.0.0:* LISTEN 2081/redis-server 1

READ: netstat Command not found on CentOS 7 / RHEL 7 – Quick Fix

READ: netstat Command not found on Debian / Ubuntu / Linux Mint – Quick Fix

Firewall (CentOS / RHEL)

You will have to add a firewall rule that allows traffic from remote machines to the Redis server on TCP port 6379 in case you have enabled remote access.

Run the following commands in the terminal

firewall-cmd --permanent --add-port=6379/tcp 
firewall-cmd --reload

Verify Remote Access

To verify that remote access is set up properly, you can try to ping the Redis server from your remote machine using the redis-cli (command-line interface).

redis-cli -h <REDIS_IP_ADDRESS> ping

The command should return a PONG.

PONG

Set Up Redis Master/Slave Replication

On Master Node

Step 1: Install Redis
Step 2: Configure Remote Access

On Slave Node

Step 1: Install Redis

Configure Slave

Configure a slave instance by adding the replicaof directive into redis.conf to set up the replication.

### CentOS 7 / RHEL 7 ###

vi /etc/redis.conf

### Ubuntu 18.04 / Ubuntu 16.04 ###

sudo nano /etc/redis/redis.conf

Again replace 192.168.1.10 with the master node IP address.

replicaof 192.168.1.10 6379

Restart the service.

### CentOS 7 / RHEL 7 ###

systemctl restart redis

### Ubuntu 18.04 / Ubuntu 16.04 ###

systemctl restart redis-server

Verify Reds Master/Slave Replication

Test that the replication works. On your master node, run redis-cli and execute command set 'name' Raj.

redis-cli
127.0.0.1:6379> set 'name' Raj

Output:

OK

Exit from redis-cli prompt by typing exit or pressing Ctrl-C.

Next, run redis-cli on the slave node and execute get 'name', which should return the same value as that on the master.

redis-cli
127.0.0.1:6379> get 'name'

Output:

"Raj"

This output confirms that the master/slave replication setup is working properly.

To learn more about how to use Redis, visit Redis’s official documentation page.

That’s All.

Further Reading

You might also like

How To Install Redis on CentOS 7 / RHEL 7 & Ubuntu 18.04 / Ubuntu 16.04

0