Install & configure Redis on Ubuntu

Upasana | October 01, 2020 | 3 min read | 103 views


This tutorial demonstrates how to install, configure (memory, systemd, networking), and secure Redis 5.x on Ubuntu 20.04 LTS server.

Prerequisites

  • Ubuntu 20.04 LTS (or 18.04 LTS)

  • non-root user with sudo privileges

Installing Redis

We will install Redis from the official Ubuntu repository.

Update your local apt package cache and install Redis by typing:

sudo apt update
sudo apt install redis-server

Now your system should have Redis installed with default configuration. You can check status of redis-server using the below command:

sudo systemctl status redis

Configuring Redis

Redis automatically generates and save config file which we will modify to suite our requirements:

Open this file using nano or any other editor of your choice:

sudo nano /etc/redis/redis.conf
sudo vi /etc/redis/redis.conf

Configure memory

We can configure the amount of RAM reserved for your redis-server installation using below config parameters:

/etc/redis/redis.conf
maxmemory 256mb
maxmemory-policy allkeys-lru

Configure supervisord

For Ubuntu, we can safely select systemd as the supervised so that Redis can interact with your supervision tree.

 # Note: these supervision methods only signal "process is ready."
 # They do not enable continuous liveness pings back to your supervisor.

 supervised systemd

Binding to localhost

By default, due to security reasons, Redis listens only on 127.0.0.1 interface. But if you want to make it available to other machines on network, then just find the below line and uncomment it. If your machine is exposed to internet, then make sure you set a very strong password for Redis before doing so.

/etc/redis/redis.conf
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1  (1)
1 We have removed IPv6 support as well (::1), which might cause issues in booting up Redis on certain servers.

Optionally, you can also specify a given network interface’s address to bind parameter, so that Redis is only exposed on that ip address.

Restart service

We can use systemctl for managing redis-server i.e. we can start, restart, stop and check status

Restart Redis instance
sudo systemctl restart redis
Stop Redis instance
sudo systemctl stop redis
Start Redis instance
sudo systemctl start redis
Check status of Redis instance
sudo systemctl status redis

Secure Redis

Password authentication is disabled in default Redis Configuration

/etc/redis/redis.conf
# requirepass foobared

You can generate secure password using openssl,

openssl rand 60 | openssl base64 -A
output
a5f+F5YP4Qk6wD+xHrpQa5AoF+ku5c9payqL2LjnhXRLHufdJQWZhDrW78fr4Z9X72NYikZlBXCQcaRh

Now use this password in redis configuration

./etc/redis/redis.conf
requirepass a5f+F5YP4Qk6wD+xHrpQa5AoF+ku5c9payqL2LjnhXRLHufdJQWZhDrW78fr4Z9X72NYikZlBXCQcaRh

Restart the server and security will be in place for redis.

Test Redis

Redis provides redis-cli utility to connect to Redis server.

redis-cli
Output
127.0.0.1:6379> ping
PONG

Check server information

redis-cli info server
Output
# Server
redis_version:5.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:636cde3b5c7a3923
redis_mode:standalone
os:Linux 5.4.0-40-generic x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:9.2.1
process_id:1427
run_id:4e4a787dd07f5fd940c621797d0064369a04909f
tcp_port:6379
uptime_in_seconds:1399
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:604960
executable:/usr/bin/redis-server
config_file:/etc/redis/redis.conf

Top articles in this category:
  1. Install ElasticSearch 7 on Ubuntu 20.04
  2. Install Cassandra 4 on Ubuntu 20.04
  3. Install OpenJDK 11 on Ubuntu 18.04 LTS
  4. MySql 8 installation on Ubuntu 20
  5. Install RabbitMQ and Erlang 23 on Ubuntu 20
  6. Upgrade Jenkins on Ubuntu 18.04 LTS
  7. Installing nginx on macOS Mojave using brew

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.