How to Install Redis on CentOS 7

Introduction

In this article, we are going to perform, how to install Redis on CentOS 7. Before we proceed further, let me give you brief idea about redis.

Redis is an open source tools, which is used as a database and acts as in-memory data structure store, cache and message broker. Message brokers are building block of message-orianted-middleware(MOM), which transfers, route, respond, invoke and interact with sender/receiver end. It supports different data structures such as string, lists, sorted sets with range queries and etc. In redis, we can configure single data base structure and replication (Master-Slave) too.

Prerequisites

  • CentOS 7
  • 2 CPU’s, 3GB RAM
  • SSH access with sudo privileges
  • Open Firewall Port: 6379

Step 1: Install Redis on CentOS

Before installing redis, let’s add EPEL repository and update the YUM package.use below command to update epel,

$ sudo yum install epel-release -y

output:

Running transaction
Installing : epel-release-7-11.noarch 1/1
Verifying : epel-release-7-11.noarch 1/1
Installed:
epel-release.noarch 0:7-11
Complete!

Let’s install redis by using below command.

$ sudo yum install redis -y

output:

Running transaction
Installing : jemalloc-3.6.0-1.el7.x86_64 1/2
Installing : redis-3.2.12-2.el7.x86_64 2/2
Verifying : redis-3.2.12-2.el7.x86_64 1/2
Verifying : jemalloc-3.6.0-1.el7.x86_64 2/2
Installed:
redis.x86_64 0:3.2.12-2.el7
Dependency Installed:
jemalloc.x86_64 0:3.6.0-1.el7
Complete!

To enable the redis service.

$ sudo systemctl enable redis.service

To start the redis service.

$ sudo systemctl start redis.service

To check the status of redis service.

$ sudo systemctl status redis.service

output:

● 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 Sun 2019-07-21 00:03:36 CDT; 51s ago
Main PID: 31289 (redis-server)
CGroup: /system.slice/redis.service
└─31289 /usr/bin/redis-server 127.0.0.1:6379
Jul 21 00:03:36 fosstechnix systemd[1]: Starting Redis persistent key-value database…
Jul 21 00:03:36 fosstechnix systemd[1]: Started Redis persistent key-value database.

To check redis version, enter below command

$ redis-cli -v

output:

redis-cli 3.0.6

 

After confirming that redis is running. Let’s run below command.

$ redis-cli
127.0.0.1:6379> ping
PONG

If you get the output as “PONG”. Then its working fine.

Step 2: Configure Redis

Default configuration is located in /etc/redis.conf. Now we will change the configuration to access redis from remote servers, defaultly  it has access only to localhost. To enable remote access uncomment and change the bindIP as below.

#bind 127.0.0.1
 bind 0.0.0.0 

1. Redis as daemon

By default redis does not run as daemon and protection mode is enabled, edit the /etc/redis.conf file and change to daemonize as yes, protected-mode to yes

 daemonize yes 
protected-mode yes

To take effect of above configuration, restart the redis-server.

 $ sudo systemctl restart redis.service 

To check redis is listening on port 6379, use below command:

$ netstat -tulpn | grep LISTEN

output:

tcp6       0      0 :::6379                 :::*                    LISTEN    -

2. Test Redis using CLI

$ redis-cli
127.0.0.1:6379> ping
PONG

3. To set the value

  127.0.0.1:6379> SET <key> <value>

For example:

 127.0.0.1:6379> SET 'URL' https://wwww.fosstechnix.com 

output:

 127.0.0.1:6379> GET 'URL' 
"https://wwww.fosstechnix.com"

4.To check keys

127.0.0.1:6379> keys *

Step 3: Secure Redis

By default redis is not secured. In the configuration file /etc/redis.conf, go to SECURITY section and enable authentication

1. Enable Authentication

############ SECURITY #################
#requirepass foobared

Uncomment the above and change password from foobared to <your password> and restart redis-server.

 requirepass fosstechnix  
 $ sudo systemctl restart redis.service 

Now let’s test the new configuration. Open redis-cli

 $ redis-cli PING

output:

(error) NOAUTH Authentication required. 

Now use AUTH and provide your password.

 127.0.0.1:6379> AUTH <your password>
 127.0.0.1:6379> AUTH fosstechnix 

If authentication is successful the output will be OK.

2.Enable firewall

To aceess 6379 port from remote machine we have to add and enable firewall rule.

$ sudo firewall-cmd --permanent --new-zone=redis 
$ sudo firewall-cmd --permanent --zone=redis --add-port=6379/tcp .
$ sudo firewall-cmd --permanent --zone=redis --add-source=client_server_private_IP

Step 4: Start, Restart, Stop Redis Server

Use below commands to start,restart and stop redis server using command line,

$ sudo systemctl start redis.server

$ sudo systemctl restart redis.server

$ sudo systemctl stop redis.server

To learn more about Redis. Please refer official documentation.

Conclusion

In this article, we have installed redis on CentOS 7, configured bind-ip and daemonized redis. To protect our data from external users we enabled protection-mode. We created keys, secured the data by setting AUTH and using firewalld we open the port to access form remote servers.

Related Articles

How to Install Redis on Ubuntu 18.04/16.04 LTS

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

We promise not to spam you, and we don't usually send more than one email a week.

 

You have Successfully Subscribed!

0 Shares
Share via
Copy link