How to Install and Configure Redis on Debian 9

Updated on

3 min read

Install and Configure Redis on Debian 9

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 via Redis Sentinel including monitoring, notifications Automatic failover. It also provides automatic partitioning across multiple Redis nodes with Redis Cluster.

This tutorial explains how to install and configure Redis on a Debian 9 server.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges .

Installing Redis on Debian

Redis package is included in the default Debian 9 repositories. The installation is pretty straightforward, just follow the steps below:

  1. Start by updating the apt packages list by issuing the following command:

    sudo apt update
  2. Install Redis by typing:

    sudo apt install redis-server
  3. When the installation is complete, the Redis service will start automatically. You can check the status of the service by typing:

    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 Wed 2018-12-05 08:54:49 PST; 4min 52s ago
        Docs: http://redis.io/documentation,
            man:redis-server(1)
    Main PID: 1569 (redis-server)
    CGroup: /system.slice/redis-server.service
            └─1569 /usr/bin/redis-server 127.0.0.1:6379
Redis service will fail to start if IPv6 is disabled on your server.

At this point Redis is installed and running on your Debian 9 server and you can start using it.

Configure Redis Remote Access

By default, Redis cannot be accessed from a remote location. You can connect to the Redis server only from 127.0.0.1 (localhost) - the machine where Redis is running.

Complete the following steps only if you want to connect to your Redis server from remote hosts. If you are using a single server setup, where the application and Redis are running on the same machine then you should not enable remote access.

To configure Redis to accept remote connections open the Redis configuration file with your text editor:

sudo vim /etc/redis/redis.conf

Search for a line that begins with bind 127.0.0.1 and replace 127.0.0.1 with 0.0.0.0.

/etc/redis/redis.conf
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0

Save the file and close the editor.

Restart the Redis service for changes to take effect:

sudo systemctl restart redis-server

Use the following command to verify that Redis is listening on all interfaces on port 6379:

ss -an | grep 6379

You should see something like below:

tcp    LISTEN     0      128       *:6379                  *:*

Next, you’ll need to add a firewall rule that enables traffic from your remote machines on TCP port 6379.

Assuming you are using UFW to manage your firewall and you want to allow access 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 accept connections only from trusted IP ranges.

To verify that everything is set up properly, you can try to ping the Redis server from your remote machine using the redis-cli utility:

redis-cli -h <REDIS_IP_ADDRESS> ping

The command should return a response of PONG:

PONG

Conclusion

Congratulations, you have successfully installed Redis on your Debian 9 server. To find more information about how to manage your Redis installation visit the Redis documentation page.

If you have questions, feel free to leave a comment below.