Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

How to install and configure Redis on Debian or Ubuntu

profile picture
Author
Patrick Kratzer
Published
2021-06-21
Time to read
5 minutes reading time

Introduction

With this tutorial, you will get an idea of how to install and set up Redis on Debian 10 (or Ubuntu).

Prerequisites

  • server with Debian or Ubuntu installed

  • Up-to-date system - the following commands will update your complete system:

    apt update ; apt upgrade
  • Running SSH session as root.

Step 1 - Installing Redis

The first step you need to do is installing Redis via the package manager:

apt install redis

You will then be asked if you really want to install redis and all the required packages. Please type in "yes" or "y".

Step 2 - Check if Redis is up and running

For connecting to a Redis server, you can use the tool "redis-cli":

redis-cli

After that you should see the Redis command line.

You can run a basic test of Redis by sending a "PING" via the command line. If everything is fine, Redis will reply with "PONG".

It is also recommended to set a test entry and get it back from Redis. For setting the test entry, please type:

set testKey thisIsATestValue

This will generate an entry with the key "testKey" and the value "thisIsATestValue". Redis should confirm setting the entry with "OK".

Now you should check if you get the entry back by typing:

get testKey

Redis should then show "thisIsATestValue".

Step 3 - Securing the Redis instance with a password

As for many other services running on a server it is recommended to add an authentication. Redis supports authentication by password per default. This password has to be set in the main configuration file. This should normally be located under "/etc/redis/redis.conf". To open this file for updating it, you can use nano:

nano /etc/redis/redis.conf

There you will find the following commented-out line:

# requirepass foobared

You need to uncomment this line by removing the "#" at the beginning and replacing "foobared" with your selected password. After that you should save and close the file.

The password is stored in clear text, since Redis as an in memory database should work really fast. Therefore, it is also recommended to add additional security (e.g. by binding Redis locally or adding a firewall).

To bring the changes in the config file to your live server, you need to restart it via the following command:

systemctl restart redis

To check if the authentication works properly, reconnect through redis-cli again and test setting and getting a key as described above. You should then get the following error message:

(error) NOAUTH Authentication required.

To authenticate with a password inside the redis type in:

AUTH [your-password]

Setting and getting key-value pairs should then be possible again.

Step 4 - Restricting access to localhost

If it is not necessary to connect to the Redis server from another host, the access should be restricted to localhost (127.0.0.1). Therefore open the main configuration file:

nano /etc/redis/redis.conf

Search for this line:

# bind 127.0.0.1 ::1

As with the password, you need to uncomment this line. The IP addresses can be left as they are. Then save and close the configuration file again.

As described above, restarting Redis is also necessary:

systemctl restart redis

After this Redis should only be reachable from the local machine. You can verify that by trying to connect (e.g. via redis-cli) from another host.

Conclusion

After that you should have a completely functional Redis server. You should think about some further changes for security reasons. These changes could e.g. be:

  • adding a firewall
  • securing the connections to Redis via SSL

There is no one-fits-all solution here, so the additional changes depend on your system.

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue

Discover our

Dedicated Servers

Configure your dream server. Top performance with an excellent connection at an unbeatable price!

Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more