Read in 3 minutes

last updated 

How To Install and Configure Redis on Ubuntu 18.04

Redis is an open-source in-memory data structure store. It can be used as a database, cache and message broker and supports various data structures such as Strings, Hashes, Lists, Sets etc.

This tutorial describes how to install and configure Redis on an Ubuntu 18.04 server.

Prerequisites

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

Installing Redis on Ubuntu

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

  1. Start by updating the apt packages list by running the following command in your SSH terminal:

    sudo apt update
  2. Install Redis by typing:

    sudo apt install redis-server
  3. Once the installation is completed, 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 Sun 2018-10-28 05:10:45 PDT; 2h ago
         Docs: http://redis.io/documentation,
               man:redis-server(1)
      Process: 2197 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
      Process: 2201 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
     Main PID: 2226 (redis-server)
        Tasks: 4 (limit: 2319)
       CGroup: /system.slice/redis-server.service
               `-2226 /usr/bin/redis-server 0.0.0.0:6379

Redis service will fail to start if IPv6 is disabled on your server.

Congratulations, at this point you have Redis installed and running on your Ubuntu 18.04 server.

Configure Redis Remote Access

By default, Redis doesn’t allow remote connections. You can connect to the Redis server only from 127.0.0.1 (local host) - the machine where redis is running.

Perform 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 nano /etc/redis/redis.conf

Locate the line that begins with bind 127.0.0.1 ::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 ::1

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. 0.0.0.0 means all IPv4 addresses on the machine.

tcp  LISTEN 0   128   0.0.0.0:6379   0.0.0.0:*                                  
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

At this point Redis server will accept remote connections on TCP port 6379.

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

To verify that everything is setup 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 Ubuntu 18.04 server.