How to install and configure Redis on Ubuntu 18.04

s2 0

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

This tutorial describes how to install and configure Redis on an Ubuntu 18.04 server. The same instructions apply to Ubuntu 16.04 and any Ubuntu-based distribution.

requirements

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

Install Redis on Ubuntu

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

  1. Start by updating the apt package 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 complete, the Redis service will start automatically. Enter the following command to check the status of the service:

    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: https://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
The Redis service will not start if IPv6 is disabled on your server.

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

Configure Redis remote access

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

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 computer, 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

Find the line that starts 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 the changes to take effect:

sudo systemctl restart redis-server

Use the following command to check that redis is listening on port on all interfaces 6379:

ss -an | grep 6379

You should see something like below. 0.0.0.0 means all IPv4 addresses on the device.

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

Next, you need to add a firewall rule that allows traffic from your remote computers on the TCP port 6379.

Assuming you use UFW
to manage your firewall and you want access from 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, the Redis server will accept remote connections on TCP port 6379.

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

To check that everything is set up correctly, you can try running the Redis server from your remote machine with the redis-cli Utility:

redis-cli -h <REDIS_IP_ADDRESS> ping

The command should return a response from PONG:

PONG

diploma

Congratulations, you have successfully installed Redis on your Ubuntu 18.04 server. For more information on managing your Redis installation, see Redis documentation
Page.

If you run into any problem or have any feedback, please leave a comment below.