How to Install and Secure Redis on AlmaLinux 9

Redis (or remote dictionary server) is an open-source in-memory data store, NoSQL database that can be used primarily as an application cache or database.

Redis stores its data in memory so that it can deliver unparalleled speed, reliability, and performance. Because of its ability to deliver super-fast response time, Redis is commonly used on many websites like social media and gaming websites.

Since it’s a NoSQL database, it does not have all features of a traditional database like MySQL or MongoDB. This tutorial will show you how to install Redis on AlmaLinux 9.

Prerequisites

  • An AlmaLinux VPS
  • root SSH access or a regular user with sudo privileges

Step 1: Log in to Your Server via SSH

To start this, you will need to log in to your AlmaLinux 9 VPS via SSH as the root user:

ssh root@IP_Address -p Port_number

You will need to replace ‘IP_Address’ and ‘Port_number’ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the system user with sudo privileges.

You can check whether you have the proper AlmaLinux version installed on your server with the following command:

# cat /etc/almalinux-release

You will get an output like this:

AlmaLinux release 9.1 (Lime Lynx)

In this article, we are using ‘root’ to execute the shell commands. If you want to use your regular user with sudo privileges to run the commands, make sure to append ‘sudo’ in front of them.

Step 2: Update the System

Before starting, you have to ensure that all AlmaLinux OS packages are up to date. You can do this by executing the following commands:

# dnf update
# dnf upgrade

Step 3. Install Redis

There are several ways to install Redis, from the operating system repository, compiling it from source, using docker, etc. In this tutorial, we are going to install Redis from the default AlmaLinux 9 repository, so the installation is straightforward.

# dnf install redis

Once installed, Redis will not automatically run. Let’s execute this command below to enable and start Redis.

# systemctl enable --now redis

You can check the Redis service status with this command.

# systemctl status redis

The command will show you an output like this:

● 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 2023-03-15 23:52:57 CDT; 5h 48min ago
Main PID: 2153 (redis-server)
Status: "Ready to accept connections"
Tasks: 5 (limit: 12328)
Memory: 6.7M
CPU: 31.654s
CGroup: /system.slice/redis.service
└─2153 "/usr/bin/redis-server *:16379"

Mar 15 23:52:57 redis.rosehosting.com systemd[1]: Starting Redis persistent key-value database...
Mar 15 23:52:57 redis.rosehosting.com systemd[1]: Started Redis persistent key-value database.

Step 4. Configure Redis

Now that Redis is running on your server, we will customize our Redis configuration. To customize your Redis configuration, we need to edit the configuration file. For example, by default, Redis is running on port 6379; if we want to change the port to, let’s say 16379, we can edit /etc/redis/redis.conf and find the line.

port 6379

Edit or replace the line, to be like this

port 16379

Then, to apply the changes, we need to restart the Redis service

# systemctl restart redis

Now that Redis is running and listening on port 16379. We can log in to Redis CLI and execute a command to check it.

# redis-cli -p 16379

The command above will bring you to Redis CLI, and since Redis is listening on port 16379 now, we need to specify the port number in the command or else it will not be connected.

While in Redis CLI, we can execute this command to check its status:

127.0.0.1:16379> info server

Both commands above will send an output like this:

[root@rh ~]# redis-cli -p 16379
127.0.0.1:16379> info server
# Server
redis_version:6.2.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:ec192bdd77ecd321
redis_mode:standalone
os:Linux 5.14.0-162.18.1.el9_1.x86_64 x86_64
arch_bits:64
monotonic_clock:POSIX clock_gettime
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:11.3.1
process_id:2096
process_supervised:systemd
run_id:f0594ef263f551b29236e6a5a56e9dfb713789a4
tcp_port:16379
server_time_usec:1678939600623971
uptime_in_seconds:157
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:1218000
executable:/usr/bin/redis-server
config_file:/etc/redis/redis.conf
io_threads_active:0
127.0.0.1:16379>

In the same /etc/redis/redis.conf configuration file, we can also specify the listening interface by replacing this line

bind 127.0.0.1 -::1

with

bind *

Let’s restart Redis to apply the configuration changes.

# systemctl restart redis

Step 5. Secure Redis

In the previous step, we show how to configure Redis to listen not only on localhost. Please note that binding Redis to your server’s public IP without an authentication interface is dangerous. So, if you really need to bind your Redis server to your server’s IP address, then you can add the authentication by adding this line in your redis.conf file.

requirepass YOURSTRONGPASSWORD

Make sure to replace YOURSTRONGPASSWORD with your own strong password, and do not forget to restart Redis each time you modify the configuration file.

# systemctl restart redis

Now, you can connect to your Redis server with this command from another server:

# redis-cli -h YOUR_REDIS_SERVER_IP_ADDRESS -p 16379 -a 'YOURSTRONGPASSWORD'

Another step you would like to take is to configure the firewall only to accept connections from specific IP addresses and block all others.

# iptables -A INPUT -s [YOUR_EXTERNAL_IP_ADDRESS] -p tcp --dport [YOUR_REDIS_PORT] -j ACCEPT

By now, we hope you’ve learned how to smoothly install and secure Redis on AlmaLinux 9 from our post. Now, it’s your opportunity to share:

Did you find any of the steps confusing, or do you think we left something out?

What other detailed instructional tutorials would you appreciate seeing on our blog?

We look forward to hearing your thoughts, so please leave a comment below.

Leave a Comment