How to Install Redis on CentOS 7

A well-known means to improve the efficiency of modern computer applications is by enabling caching. Caching simply means keeping active data in a temporary store to make it easier and faster to access and Redis is one of the most popular caching solutions out there which supports most programming languages including Node.js, Python, PHP, Java, C, and much more.

Redis is an advanced and feature-rich key-value store that works in most POSIX systems such as Linux (which is the recommended platform for production deployment), *BSD, and OS X without external dependencies. It has three major uses: as a database, cache and message broker. It supports various data structures including strings, lists, sets, hashes, sorted sets with range queries, bitmaps and much more.

Read Also: How to Install Redis in RHEL 8

Some of its key features include built-in replication, cluster mode, partitioning (distributing data among multiple instances), transactions, notifications of key-space events, Lua scripting, mass insertion of data in a Redis instance in a short time, memory optimization, and so much more. Importantly, it supports an API which allows you to extend Redis functionality using external modules.

In this article, we will explain to you how to install, configure and test a Redis server on CentOS 7 Linux.

Installing Redis Server on CentOS 7

1. To install Redis package on CentOS 7, you need to install the EPEL repository on your system using the YUM package manager as shown.

# yum install epel-release
Install Epel in CentOS 7
Install Epel in CentOS 7

2. Once EPEL has been installed, you can now install Redis package from the repository as follows.

# yum install redis 
Install Redis in CentOS 7
Install Redis in CentOS 7

3. Once you have installed the package, you need to set up your server to achieve high performance when using Redis. You need to perform some settings in the kernel as explained.

First, ensure that you have set up swap space in the server. It is recommended to set up as much as swap as memory.

4. Next, set the Linux kernel overcommit memory setting to 1 by adding vm.overcommit_memory = 1 to /etc/sysctl.conf configuration file.

# sysctl vm.overcommit_memory=1
Set Kernel Parameter for Redis
Set Kernel Parameter for Redis

and then apply the change by rebooting the system Or enable the setting immediately by running the following command.

# sysctl vm.overcommit_memory=1
Set Kernel Parameter Using Sysctl Command
Set Kernel Parameter Using Sysctl Command

Also make sure that the transparent huge pages kernel feature is disabled because it negatively has an impact on both memory usage and latency using the following echo command.

# echo never > /sys/kernel/mm/transparent_hugepage/enabled

Configuring Redis Server on CentOS 7

5. The default configuration for Redis is /etc/redis.conf. Before you can edit it, create a back up of it as follows. This enables you to revert to the back up of the default configurations in case of any mistakes.

# cp /etc/redis.conf /etc/redis.conf.orig

6. Then open the original Redis configuration file for editing using your any of your favorite text-based editors as shown.

# vi /etc/redis.conf 

There are several configuration directives, and their meaning and intended usage is available and well explained in the file.

A typical configuration example is allowing remote access to the Redis server. By default, Redis is configured to accept connections only on the local server where it is running, i.e on the loopback interface (127.0.0.1) and it listens on port 6379.

7. To allow remote access, you can set it to listen to a specific interface or multiple selected interfaces using the "bind" configuration directive, followed by one or more IP addresses as follows.

bind  127.0.0.1
bind 10.0.2.15   192.168.0.105

8. To accept connections on a different port, change the value of the port directive.

port 5000
Configure Redis in CentOS 7
Configure Redis in CentOS 7

After making all the necessary changes, save the file and exit it.

9. At this point, you have set up your server to efficiently run Redis and configured the Redis server to work the way you want. Now you need to start the Redis service, for now, enable it to automatically start every time the system is rebooted, and check its status using the systemctl utility as shown.

# systemctl start redis
# systemctl enable redis
# systemctl status redis
Verify Redis Status
Verify Redis Status

10. To check the interface and port the Redis server is listening on, use the netstat command.

# netstat -tlpn
Check Redis Port
Check Redis Port

11. If you have the firewalld service running on your system, you need to open port 6379 in the firewall configuration to allow external connections to the Redis server.

# firewall-cmd --permanent --zone=public --add-port=6379/tcp 
# firewall-cmd  --reload
Open Redis Port
Open Redis Port

Testing Connectivity to Redis Server

12. To test connectivity to the Redis server, you can open the Redis client program and run a test command (in this case list connected clients) as follows.

# redis-cli
> client list	#list clients connected to the server
Test Redis Connection
Test Redis Connection

Now you can build fast, dynamic and modern applications on your CentOS 7 server using Redis. Consult the Redis documentation for more information and configuration options. If you have any queries or thoughts to share, use the feedback form below to reach us.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

2 thoughts on “How to Install Redis on CentOS 7”

  1. This is a great article! I have already a large WordPress woo commerce website running on CentOS with caching plugins (Wp Fast Cache) but I can’t find a way to speed up for more traffic. The AB ab -n1000 -c200 test is killing website and I been told that Redis is the solution for the large DB.

    Is it better to install Redis direct to the server as a Plugin? I’m using Flatsome theme Ngnix MariaDB. Also, I’m thinking about PHP-FPM for high performance and move from the current setup on-demand to static or dynamic. do you have any experience with PHP-FPM??

    Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

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