How to install and configure Redis on Ubuntu 20.04

Redis is an open source in-memory key-value data store. It can be used as a database, cache, and message broker, and supports various data structures such as strings, hashes, lists, sets, etc. Redis provides high availability through Redis Sentinel and automatic partitioning across multiple Redis nodes using Redis Cluster.

This article will walk you through how to install and configure Redis on Ubuntu 20.04.

Installing Redis on Ubuntu 20.04

Installing Redis on Ubuntu is a simple process.

Redis 5.0.x is included in the standard Ubuntu 20.04 repositories. To install it, run the following commands as root or a user with sudo privileges:

sudo apt update
sudo apt install redis-server

After the installation is complete, the Redis service will start automatically. To check the status of the service, enter the following command:

sudo systemctl status redis-server

You should see something like this:

● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2020-06-06 20:03:08 UTC; 10s ago
...

The Redis service will not start if your server has IPv6 disabled.

That’s all. Redis is installed and running on your Ubuntu 20.04 server.

Configuring Redis Remote Access

By default, the Redis server does not accept remote connections. You can only connect to Redis from 127.0.0.1 (localhost) – the computer that Redis is running on.

If you are using a single server installation where the client connecting to the database is also running on the same host, you should not allow remote access.

To configure Redis to accept remote connections, open the Redis configuration file in a text editor:

sudo nano /etc/redis/redis.conf

Find the line that starts with bind 127.0.0.1 :: 1 and comment it out.

/etc/redis/redis.conf

# bind 0.0.0.0 ::1

If your server has a private IP address and you want Redis to be accessible only from the private network, then leave the private IP after 127.0.0.1 instead of commenting the line.

Save the file and restart the Redis service for the changes to take effect:

sudo systemctl restart redis-server

Use the following command to make sure redis is listening on all interfaces on port 6379:

ss -an | grep 6379

You should see something like 0.0.0.0, which means all IPv4 addresses on the machine.

tcp  LISTEN 0   511   0.0.0.0:6379   0.0.0.0:*
tcp  LISTEN 0   511      [::]:6379      [::]:*  

Next, you need to configure your firewall to enable traffic from TCP port 6379.

Typically, you want to only allow access to the Redis server from a specific IP address or range of IP addresses. For example, to only allow connections from the 192.168.121.0/24 subnet, you would run the following command:

sudo ufw allow proto tcp from 192.168.121.0/24 to any port 6379

Make sure your firewall is configured to only accept connections from trusted IP ranges.

At this point, you should be able to connect to Redis over TCP port 6379 from remote locations.

To make sure everything is configured correctly, you can try pinging the Redis server from a remote machine using the redis-cli utility:

redis-cli -h <REDIS_IP_ADDRESS> ping

The command should return a PONG response:

PONG

Output

We showed you how to install Redis on Ubuntu 20.04. To learn more about how to manage your Redis installation, visit the Redis documentation page.

If you run into an issue or have feedback, please leave a comment below.

Sidebar