How To Configure and Install Redis on Ubuntu Linux


In this article we will learn how to configure Redis, Redis is an in-memory Key-value store which is popular for its flexibility, performance and used with wide language support. We will configure this on the Ubuntu Linux server. To do this demo we need a non-root user and we will set Sudo privileges to the user to perform.

Install the Build and Test Dependencies

To get the latest version of Redis, we will get the latest source code and will compile and install the software. For that, we needed to install the dependencies for the software compiling.

We also need to install build-essential meta-package from the Ubuntu repositories, and download ‘tcl’ packages which are used to test the binaries.

# sudo apt-get update
# sudo apt-get install build-essentials tcl

Redis Download, Compile & Install

Downloading the Source code and Extract. Create a folder in temp

# mkdir /tmp/redis
# cd /tmp/redis

Download the latest stable version of Redis, thus we can get using the below command

# curl -O http://download.redis.io/redis-stable.tar.gz
# tar xzvf redis-stable.tar.gz
# cd redis-stable

Build, Install Redis

We will compile and install the Redis binaries with the below command

# make

After the compilation of the source code, we will get binaries we run the below command to test the suit

# make test

We can install all the binaries on the system using the below command.

# make install

Configure Redis on Ubuntu

As we just installed the Redis, we can begin the configuration of Redis. We needed to create the directory /etc/redis

# mkdir /etc/redis

Copy the redis configuration files included in the Redis source archive

# cp /tmp/redis/redis-stable/redis.conf /etc/redis

Open the configuration file using an editor

# vi /etc/redis/redis.conf

In the configuration file, we will find a directive called ‘supervised’ which is set to no, as we are using systemd to init the system so that we can change this to systemd.

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#    supervised no      - no supervision interaction
#    supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#    supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#    supervised auto    - detect upstart or systemd method based on
#                      UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#    They do not enable continuous liveness pings back to your supervisor.
supervised systemd

Next, find the dir directive and change the dire directive to the /var/lib/redis, where this option is used to specify the directory that Redis will be used to dump the persistent data. This location has the write permission and is not viewable by normal users.

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis

Save the changes and close the file

Create a systemd Unit file for Redis

We can create a systemd file so that init system can manage the process.

# touch /etc/systemd/system/redis.service
# vi /etc/systemd/system/redis.service

We start with [Unit] section by adding the description and define a requirement that we needed networking to be the most available to start this service.

[Unit] Description=Redis In-Memory Data Store After=network.target

In [service] section we will specify the services behavior. For security reasons, we will not run the service as root user. We will dedicate a user and group which will call the Redis with simplicity.

To start the services we need to call the redis-server binary which is pointed at our configuration. To stop the service we will use Redis shutdown command and which will be executed with redis-cli binary.

[Unit] Description=Redis In-Memory Data Store After=network.target [Service]
User=redis Group=redis ExecStart=/usr/local/bin/redis-server
/etc/redis/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install] section, we define the systemd to target the service to attach to
enable (configured to start at boot time).
[Unit] Description=Redis In-Memory Data Store After=network.target [Service]
User=redis Group=redis ExecStart=/usr/local/bin/redis-server
/etc/redis/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always [Install] WantedBy=multi-user.target

Creating Redis User,Group and Directories

# add users --system --group --no-create-home redis
# mkdir /var/lib/redis
# chown redis:redis /var/lib/redis
# chmod 770 /var/lib/redis

Start and Test Redis

We are ready to start the Redis Server

# systemctl start redis

To check the services running with no errors we can run the below command –

# systemctl status redis
redis.service - Redis Server
Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2016-05-13 11:42:00 EDT; 5min 03s ago
Process: 7127 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=0/SUCCESS)
Main PID: 7143 (redis-server)
Tasks: 6 (limit: 512)
Memory: 10864.0K
CPU: 79ms
CGroup: /system.slice/redis.service
└─7143 /usr/local/bin/redis-server 127.0.0.1:6379

Enable Redis to Start at Boot time

# sudo systemctl enable redis

We should have a Redis instance installed and configured in our environment so that we can use it for in-memory data structure store and also used as a database, cache and as message broker.

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 21-Jan-2020

441 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements