Redis is a free, open-source, in-memory data structure store widely used as a database, cache, and message broker.
In this tutorial, we will explore commands to install Redis on our machine. Not just that, we will also see how to configure it. Rocky Linux 8 is the operating system we have used in this guide.
Let’s go!
Installation Guide:
Following these steps, we will install and configure Redis:
Step 1: Install Redis
To install Redis, run this command:
sudo dnf install redis
Step 2: Edit Redis Configuration file
Next, we will make some changes to the Redis configuration file.
Open the file using the vim editor:
sudo vim /etc/redis.conf
Locate supervised directive in the file. It will be written as:
Change it to:
Step 3: Start Redis
After saving changes, start Redis like this:
sudo systemctl start redis
Step 4: Enable Redis
Similarly, we will enable Redis with this command:
sudo systemctl enable redis
Step 5: Check Redis status
Now we will confirm if Redis service is running fine or not. Do that with this command:
sudo systemctl status redis
Step 6: Test Redis Installation
In this step, we will test Redis by running the command mentioned below. If it returns with PONG, this means redis is working fine.
$ sudo redis-cli ping
Protect Redis with the help of a Password
To secure the database, we will assign it a password to prevent unauthorized access.
Step 1: Edit Redis configuration file
First of all, open the Redis configuration file again using this command:
sudo vim /etc/redis.conf
Find the following directive in the file to uncomment it and assign your password:
requirepass strong_password
Step 2: Restart Redis
To update changes, restart Redis.
sudo systemctl restart redis
Step 3: Enter Redis-cli
Now, we will check if the password authentication is working fine or not. To do that, access Redis client with this command:
redis-cli
Step 4: Set Key to a value
Now set a key to a value like this:
set keystudent Tom
This will give an error, telling you that authentication is required to set the key.
To get access, push your password with auth command like this:
auth your_password
Once the password is accepted, you will be able to set a key to the value.
Step 5: Retrieve the value
To retrieve the value you just assigned to the key, use the get command like this:
get keystudent
Step 6: Exit Redis
To exit the database, just type quit and hit enter.
Quit
Configure Redis for remote access
Here, we will see how to configure Redis to be accessed remotely from another machine.
Step 1: Edit Redis configuration file
First of all, open the redis.conf file with this command:
sudo nano /etc/redis.conf
Search for the bind directive, which is set to listen to localhost. Comment it:
Next, give the remote server’s ip address like this
Bind private_ip
To grant access to Redis over the public internet, set the bind directive to 0.0.0.0.
bind 0.0.0.0
Step 2: Disable protected mode
Now change protected mode yes to no as shown below:
Save the changes and exit.
Step 3: Restart Redis
Now, restart the database with this command to update changes in the Redis configuration file:
sudo systemctl restart redis
Configure Firewall for Redis
In this part, we will configure the default firewall in Rocky Linux, namely firewalld, to allow Redis to listen on port 6379 which is its default port.
Step 1: Allow Redis port 6379
Run the following command so that Redis can listen on its default port:
sudo firewall-cmd --add-port=6379/tcp --permanent
Step 2: Reload firewall
To update changes, reload the firewall with this command:
sudo firewall-cmd --reload
Step 3: Test new settings
Now that we have configured the firewall for Redis, let’s test if a remote machine can access Redis or not. Do that by running the following command from a remote machine:
redis-cli -h server_IP
server_IP is the IP address of the machine on which Redis is deployed. In my case, it is 10.128.1.2.
redis-cli -h 10.128.1.2
That was all for today. In this document, we saw in detail how to install and configure Redis on Rocky Linux 8.
To see how you can install Redis on Ubuntu 20.04, visit:
https://linuxways.net/ubuntu/how-to-install-and-configure-redis-in-ubuntu-20-04/