Article directory
What is Redis?
English official website: https://redis.io/
Chinese document: http://redis.p2hp.com/
The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker. The open source, in-memory data store used by millions of developers as a database, cache
, streaming engine, and message broker.
Install Redis on CentOS 7
Using yum install redis directly on the terminal will prompt that there are no packages available.
Redis packages are not included in the default CentOS repositories. We will install Redis version 5.0.2 from the Remi repository.
Installation is very easy, just follow these steps:
-
First enable the Remi repository by running the following command in an SSH terminal:
sudo yum install epel-release yum-utils sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpmsudo yum-config-manager --enable remi
-
Install the Redis package by typing:
sudo yum install redis
3. After the installation is complete, start the Redis service and enable the following command to enable it to start automatically at boot time:
systemctl start redis
systemctl enable redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
To check the status of the service, enter the following command:
systemctl status redis
You should see something similar to:
● 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 Mon 2021-01-04 11:38:43 CST; 3min 54s ago
Main PID: 81547 (redis-server)
Tasks: 4 (limit: 11498)
Memory: 6.6M
CGroup: /system.slice/redis.service
└─81547 /usr/bin/redis-server 127.0.0.1:6379
1月 04 11:38:43 wywar systemd[1]: Starting Redis persistent key-value database...
1月 04 11:38:43 wywar systemd[1]: Started Redis persistent key-value database.
If IPv6 is disabled on your server, the Redis service will fail to start.
Congratulations, at this point you have Redis installed and running on your CentOS 7 server.
Configure Redis remote access
By default, Redis does not allow remote connections. You can only connect to the Redis server from the computer running Redis at 127.0.0.1 (localhost).
Follow the steps below only if you want to connect to the Redis server from a remote host.
If using a single server setup, and the application and Redis are running on the same machine, remote access should not be enabled.
To configure Redis to accept remote connections, open the Redis configuration file with a text editor:
sudo nano /etc/redis.conf
Find the line that starts with , bind 127.0.0.1
and add your server's private IP address after it 127.0.0.1
.
/etc/redis.conf
# 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.121.233
Make sure 192.168.121.233
to replace with your IP address. Save the file and close the editor.
Restart the Redis service for the changes to take effect:
sudo systemctl restart redis
# service redis restart
Use the following ss
command to verify that the Redis server is listening on the port on the private interface 6379
:
ss -an | grep 6379
You should see something similar to the following:
tcp LISTEN 0 128 192.168.121.233:6379 *:*
tcp LISTEN 0 128 127.0.0.1:6379 *:*
Next, you need to add firewall rules to enable traffic from the remote computer on the TCP port 6379
.
Assuming you are using FirewallD
a firewall to manage the firewall and you want to allow 192.168.121.0/24
access from a subnet, you can run the following command:
sudo firewall-cmd --new-zone=redis --permanentsudo firewall-cmd --zone=redis --add-port=6379/tcp --permanentsudo firewall-cmd --zone=redis --add-source=192.168.121.0/24 --permanentsudo firewall-cmd --reload
The above command creates a new zone named redis
, opens ports 6379
and allows access from the private network.
At this point, the Redis server will accept remote connections on TCP port 6379.
Make sure to configure your firewall to only accept connections from trusted IP ranges.
To verify that everything is set up correctly, you can try to redis-cli
ping the Redis server to the remote machine using the utility, which provides a command line interface to the Redis server:
redis-cli -h <REDIS_IP_ADDRESS> ping
The command should return the following response PONG
:
PONG
in conclusion
Congratulations, you have successfully installed Redis on your CentOS 7 server. To learn more about how to use Redis, visit its official documentation page.