How to Install and Configure Redis on Ubuntu

linux topic

January 7, 2023 by Ashley

Install and Configure Redis on Ubuntu

Developers install Redis on Ubuntu to speed up the projects while maps, lists, and sets will be stored. they also use Redis as a cache and message broker. Redis is an open-source in-memory data structure store used as a database, cache, message broker, and streaming engine. Join us with this article to learn how to install and configure Redis on Ubuntu. In the end, you will find Redis more useful than traditional databases because of its flexibility and performance, availability, automatic partitioning, and wide language support.

Prerequisites to Install Redis on Ubuntu

To let this tutorial works correctly, provide the options below and move on.

  • A system running Ubuntu.
  • A non-root user with sudo privileges.
  • Access to a terminal window/command line.
  • A firewall configured with ufw.

Tutorial Install and Configure Redis on Ubuntu

When you are done with the prerequisites, you are ready to start the process to install and configure Redis on Ubuntu. This guide works on Ubuntu 18.04 and Ubuntu 20.04. Let’s get started to review all the required steps to have a Redis server on your Ubuntu.

Step 1: Install Redis on Ubuntu

To install Redis on Ubuntu, run the command below to update the APT repository. Redis is included in the official package of Ubuntu, but you are recommended to start the installation process by updating the package repository.

sudo apt update

Now, use the following command to install Redis on Ubuntu and download its dependencies.

sudo apt install redis-server

When you are prompted, press Y and press Enter to move on.

Also, you can run the command below to check the installation status by viewing the Redis Version.

redis-cli --version

Clearly, you will see the version of the installed Redis server on your system in the output. To check if the Redis instance is running, type:

sudo systemctl status redis

When you see the “Active: active (running)” line in your output, it means installing Redis on Ubuntu is successfully completed, and you can continue to configure it on your Ubuntu.

Step 2: Configure Redis on Ubuntu

Install and Configure Redis on Ubuntu requires 4 main steps and one optional step that will be explained. Once you make sure the installation step is done, you can start to configure Redis. You need to modify the Redis configuration file. So, open your favorite text editor and do this. Here we use nano.

sudo nano /etc/redis/redis.conf

Then, find the supervised directive. This line is set tono by default. Since you are running Ubuntu, set the supervised directive tosystemd (Ubuntu’s init system) to manage Redis as a service.

/etc/redis/redis.conf

. . .

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd

. . .

You just need to save the changes and close the file. To do this press CTRL + X, Y, and then ENTER if you are using nano.

Finally, run the command below to restart the Redis service:

sudo systemctl restart redis.service

Step 3: Check Redis Installation on Ubuntu

When you are done with installing and configuring Redis, let’s verify whether the recent steps are passed correctly or not. Testing the status of the service running and connecting to the server is a reasonable idea. You can start by checking Redis’s service status to ensure that Redis is functioning as you planned.

Use the following command to check if the Redis service is running:

sudo systemctl status redis

Once your output displays that the service is active and running, Redis is set to start up every time the server boots. However, if you prefer to start up Redis manually every time the server boots, run the command below to configure Redis for that.

sudo systemctl disable redis

Also, you can check the Redis connection. So, to verify the connection with the Redis server, type:

redis-cli

Next, it moves you to the redis-cli command prompt. Run the following command to test the connectivity.

ping

Output:

PONG

The above output confirms that the server connection is still alive.

Now, you can test setting up Key-value pairs. Redis is an in-memory key-value NoSQL database. So, you can test if it retrieves assigned values based on the specified key. To do this, start with setting a key.

set key1 "You have successfully set up a key-value pair!"

As you see, the key is labeled as key 1 which has the value of “You have successfully set up a key-value pair!”.

when you press Enter, you see the prompt responds with OK. Then, use the command below to check if you have assigned the given value to the key correctly.

get key1

If the output displays the message you attached as the value, you’re all set, and you can exit out of the Redis shell.

quit

Step 4: Secure Redis on Ubuntu

In the middle of the guide on how to install and configure Redis on Ubuntu, you reviewed the required steps to install Redis on Ubuntu and set up it. It is time to set up Redis authentication. While Redis includes an authentication feature as an additional security layer, you need to enable the feature since it is inactive by default. To activate the authentication feature, modify the configuration file.

Open the Redis configuration file for editing:

sudo nano /etc/redis/redis.conf

Then, locate the requirepass directive under the SECURITY section and uncomment it. Replace your desired password with foobared after uncommenting the line. When you are finished, save and close the file. Again, you need to restart the Redis service:

sudo systemctl restart redis.service

After enabling the authentication feature of Redis, you will not be able to do any query without providing the required password. So, if you face the message of: (error) NOAUTH Authentication required in your output, provide the password defined in the configuration file to continue working in Redis.

Just run the following command to wait for the output to respond with OK.

auth [your_password]

Installing Redis on Ubuntu with the recent steps makes Redis accessible from localhost. To restrict connections to localhost (if you have changed the default setting), open the Redis configuration file for editing:

sudo nano /etc/redis/redis.conf

Then, scroll down and find the NETWORK section in the file. Locate this line and make sure it is uncommented (remove the # if it exists):

/etc/redis/redis.conf

. . .
bind 127.0.0.1 ::1
. . .

Save and close the file when finished and restart the service by running:

sudo systemctl restart redis

Also, you can use the following command to verify that the modification is now in effect.

sudo netstat -lnp | grep redis

Now, you can make sure that you have managed to bind Redis to localhost, restricting access to other connections.

Step 5: Rename Dangerous Commands (Optional)

The last step to complete Install and Configure Redis on Ubuntu is optional, but it is a security feature built into Redis that you can use to protect your data. To restrict normal users from using commands that would harm your system, you can use the useful security feature of renaming or killing dangerous commands.

Firstly, let’s see some of the commands that are considered dangerous:

FLUSHDB,FLUSHALL,KEYS,PEXPIRE,DEL,CONFIG,SHUTDOWN,BGREWRITEAOF,BGSAVE,SAVE,SPOP,SREM,RENAME, and DEBUG .

Depending on your individual requirements or those of your site, you may choose to disable or rename a command. You can disable a command if you are certain that you will never use it. If not, renaming it might be in your best interests. Open the Redis configuration file to disable or rename your desired command:

sudo nano /etc/redis/redis.conf

Locate the SECURITY section and scroll down to the #Command renaming line.

For example, to rename the SHUTDOWN command, add the line:

rename-command SHUTDOWN [new_command_name]

In this way, the mentioned command will be renamed to a new command which a normal user would not be able to guess.

You can rename the dangerous command into an empty string to remove certain commands and kill them. So, to disable the SHUTDOWN command, add the line:

rename-command SHUTDOWN ""

Rename or Kill Dangerous Commands

And finally, save the changes when you are finished with making the changes and restart the Redis service. I guess you agree with me that it was easy to Install and Configure Redis on Ubuntu especially if you follow the above steps properly.

Conclusion

In this article, you learned How to Install and Configure Redis on Ubuntu. If you are running a high-traffic website, Redis brings you high performance and speed. All the steps of this tutorial were the essential ones you need to install and configure Redis on Ubuntu. However, there are some methods for mitigating these vulnerabilities. You are recommended to review these optional steps to harden your system’s security. Refer to Official Redis Website to do this.

5/5 - (1 vote)

Helpful

​ ​ ​ ​

About the Author Ashley

Leave a Reply

Your email address will not be published. Required fields are marked *