Installing and Setting Up Redis on CentOS 7: Complete Tutorial

Installing and Setting Up Redis on CentOS 7: Complete Tutorial

Redis is a free, memory-based data storage system that optionally saves data on disk. It acts as a key-value database, a cache, and a message system. Redis has features like transactions and copying data to other servers while supporting different types of data such as strings, hashes, lists, and sets. It can be made very easily with Redis Sentinel and automatically divide data using Redis Cluster.

This blog provides steps to set up the Redis server and manage Redis on CentOS 7. Let us help you learn how to install and set up Redis on CentOS 7!

What You Need

For this tutorial, here’s what you’ll need:

  • One CentOS 7 Droplet is set up with our Initial Server Setup for CentOS 7.

With those prerequisites, we can install Redis on CentOS 7 and perform initial configuration tasks.

Step 1: Installing Redis

To install Redis, first add the EPEL repository to our server’s package lists. EPEL is a package repository that includes various open-source add-on software packages, many of which are maintained by the Fedora Project.

We can install EPEL using yum:

After EPEL installation, install Redis with yum:

sudo yum install redis -y

This may take a few minutes. After installation, start the Redis service

sudo systemctl start redis.service

When you want to make sure Redis starts automatically on boot, just use the enable command:

 

sudo systemctl enable redis

Run the following command to check the status of Redis:

sudo systemctl status redis.service

Once Redis is running, test it with this command:

redis-cli ping

This means Redis is up and running and has an excellent opportunity to secure it.

Step 2: Secure with a Firewall

Step 2: Secure with a Firewall

To keep your Redis secure, it’s best to bind it to a local host or a private IP, and don’t forget to set up a firewall for extra protection. If you’ve set up a Redis cluster using this tutorial, you may have allowed connections from anywhere, which is less secure than limiting access to a local host or a private IP.

Open the Redis configuration file:

sudo vi /etc/redis.conf

Locate the line starting with bind and ensure it’s uncommented:

/etc/redis.conf

bind 127.0.0.1

Bind Redis to a private IP to limit external access.

/etc/redis.conf

bind your_private_ip

If you’ve taken the steps to install a firewall on your server and don’t intend to connect to Redis from another host, you’re in good shape. You can rest easy knowing that you won’t need to add any extra firewall rules for Redis.

By default, any incoming traffic will be blocked unless the firewall rules specifically allow it. Since a standard Redis server setup only listens on the loopback interface (127.0.0.1 or localhost), you can rest easy knowing there’s no risk of incoming traffic on its default port.

 

If you plan to access Redis from a different host, adjust your firewall settings with the firewall-cmd command. Allow access only from specific hosts using their private IPS to keep your service secure addresses.

Add a Redis zone to your firewall.

sudo firewall-cmd –permanent –new-zone=redis

Then, specify which port you’d like to have open. Redis uses port 6379 by default:

sudo firewall-cmd –permanent –zone=redis –add-port=6379/tcp

Specify private IP addresses allowed through the firewall to access Redis:

sudo firewall-cmd –permanent –zone=redis –add-source=client_server_private_IP

Reload the firewall to implement the new rules:

sudo firewall-cmd –reload

When the firewall detects a packet from your client’s IP address in this setup, it automatically follows the rules established in the dedicated Redis zone for that connection. For all other connections, the default public zone comes into play. The wonderful thing about the services in the default zone is that they apply to every connection.

This means that you do not need to stress about manually adding specific services, such as SSH, to the Redis zone. The best part is that the necessary rules for these connections will be automatically applied without requiring you to take any extra steps or do additional work.

To set up a firewall with Iptables, allow your secondary hosts access to Redis’s port using these commands:

sudo iptables -A INPUT -i lo -j ACCEPT

sudo iptables -A INPUT -m conntrack –ctstate ESTABLISHED, RELATED -j ACCEPT

sudo iptables -A INPUT -p tcp -s client_servers_private_IP/32 –dport 6379 -m conntrack –ctstate NEW, ESTABLISHED -j ACCEPT

sudo iptables -P INPUT DROP

Don’t forget to save your Iptables firewall rules using the method provided by your distribution. If you’re curious about Iptables, please check out our Iptables essentials guide. Remember, both firewall tools are practical, but the key point is to ensure your firewall is up and running to keep those unknown individuals from accessing your server. In the next step, we’ll ensure Redis is configured to be accessible only with a strong password.

Step 3: Configuring Redis Server on CentOS 7

The default configuration for Redis is easily found at /etc/redis.conf. Before making any changes, it’s a good idea to create a backup, just like this. This way, you can easily return to the backup of the default configurations if anything goes wrong.

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

Next, open the original Redis configuration file with any of your favorite text-based editors, as we showed you.

# vi /etc/redis.conf

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

You can allow remote access to the Redis server. By default, Redis only accepts connections on the local server (loopback interface) at the port.

To enable remote access, use the “bind” directive with one or more IP addresses for specific or multiple interfaces.

bind  127.0.0.1

bind 10.0.2.15   192.168.0.105

Change the port to a new one.

port 5000

Save and exit the file.

Now that you’ve successfully set up your server to run Redis efficiently and customized the Redis server to your liking, it’s time to get things moving. Let’s start the Redis service, ensure it automatically kicks off every time your system reboots, and check the status using the systemctl utility as demonstrated.

# systemctl start redis

# systemctl enable redis

# systemctl status redis

If you want to see the interface and the port where the Redis server is listening, just use the netstat command.

# netstat -tlpn

If your system has the firewall service running, you’ll want to open port 6379 in your firewall configuration. This makes access easy for external connections to your Redis server.

# firewall-cmd –permanent –zone=public –add-port=6379/tcp

# firewall-cmd  –reload

Step 4: Testing Connectivity to Redis Server

If you’d like to check your connection to the Redis server, simply open the Redis client program and run a quick test command (in this case, listing the connected clients) like this.

# redis-cli

> client list       #list clients connected to the server

You’re all set to create quick, dynamic, modern applications on your CentOS 7 server with Redis. The Redis documentation provides more insights and configuration options.

When it comes to servers, if you’re searching for a DDoS-protected dedicated server, there’s no need to stress. OffshoreServers.NET is ready to offer the best hosting service you will ever come across. Check out our offerings today!

FAQs

Discover the answers to the questions we often hear from you:

What is Redis, and why should I use it on CentOS 7?

Redis is a free, in-memory key-value data store used as a database, cache, and message broker. It supports multiple data types, such as strings, hashes, lists, and sets. Its speed, flexibility, and support for advanced features like transactions, replication, and clustering make it an excellent choice for high-performance applications on CentOS 7.

How do I install Redis on a CentOS 7 server?

First, install the EPEL repository using yum, then install Redis by running the following:

sudo yum install redis -y 

After that, start and enable the Redis service with:

sudo systemctl start redis 

sudo systemctl enable redis 

How can I check if Redis is running correctly?

Use the following command to check the status:

sudo systemctl status redis 

You can also test if Redis is responsive with:

redis-cli ping 

If it returns PONG, Redis is working correctly.

How do I secure Redis on CentOS 7?

For security, bind Redis to localhost or a private IP in /etc/redis.conf:

bind 127.0.0.1 

Also, set up your firewall to allow access only from trusted IPs and avoid exposing Redis to the public internet.

Which firewall rules should I set for Redis access?

Using a firewall, create a custom zone and allow port 6379 for specific IPs:

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=YOUR_PRIVATE_IP 

sudo firewall-cmd –reload 

Can I allow remote access to Redis?

Can I allow remote access to Redis?

Yes, but it’s strongly recommended that it be secured. In /etc/redis.conf, add the remote IPs under the bind directive and optionally change the default port for added security. Don’t forget to update your firewall accordingly.

How do I verify which port Redis is listening on?

You can use the netstat command:

netstat -tlpn 

This will show the IP address and port (typically 6379) that Redis is listening to.

Final Thoughts

If you’ve always wanted to install Redis on CentOS 7 but weren’t sure where to begin, there’s no need to worry. We’ve created this guide to make the process smoother and more enjoyable. Gather what you’ll need for the installation and setup, then follow along from the first step to the last. We’re confident that you’ll successfully install Redis on CentOS with ease.

If you haven’t decided on hosting solutions yet and are feeling a bit uncertain about where to start, don’t worry. OffshoreServers.NET is here to help you find the perfect offshore server hosting solutions, along with dedicated servers tailored just for your business.