Redis can be used as a key-value database or also as a cache and message broker. Some of Redis’ features are built-in transactions, replication, and support for a variety of data structures like strings, hashes, lists, sets, and so on. The Redis Sentinel makes Redis highly available and it supports automatic partitioning with Redis Cluster.

The Redis package which comes with the built in Debian repositories is pretty outdated and contains many vulnerabilities when it comes to security. To fix this in this guide, we are going to use the source version to install Redis. At the moment when this was written, the latest stable version of Redis was 4.0.10. The installation process of Redis on a Debian 9 Cloud VPS is a fairly easy task, but you have to follow the steps carefully as they are given in the tutorial below. Now let’s begin with the installation.

1. Update the OS packages

Let’s start by upgrading our server to the latest version of all packages. Let’s fetch the latest list of packages, then upgrade our server.

sudo apt-get update
sudo apt-get upgrade

2. Install dependencies

If we want to compile from source, the first thing we need to do is to install the build tools:

apt-get install build-essential tcl wget curl

You can use the official site to download the Redis source:

wget http://download.redis.io/redis-stable.tar.gz

Next, the compressed file needs to be extracted to a directory on your server.

cd opt/
tar -xvzf redis-stable.tar.gz
cd redis-stable

The older Redis versions (up until 4.0) required compiling separate components. But now with version 4, compiling Redis compiles the needed dependencies at the same time, saving us time and reduces the chance of errors.

Compile the source:

make

After you have compiled Redis, you then need to install the required binaries onto the server:

make install

If you want, you can also test if the Redis binaries and its dependencies have been met by using the command below.

make test

3. Starting Redis

We have downloaded the Redis source package which contains useful utilities that can automate the Redis startup process during system boot.

The next thing on the to-do list is to change into the utils directory and execute the build script. The script runs interactively, all you have to do is to accept the default values when prompted.

cd utils/
./install_server.sh

The relevant startup scripts which are recommended for the server are installed by the script above. Another thing you need to know is that you can locate the default Redis configuration file at /etc/redis.conf.

Don’t forget to enable Redis during start up:

systemctl enable redis_6379

Then you can start Redis like this:

systemctl start redis_6379

4. Configure Redis

At this point, the Redis development environment needs to be configurated. Therefore, you should first create a directory structure for Redis:

mkdir /etc/redis
mkdir /var/www/redis

Now, by using the following command you will create a Redis user and group:

adduser --system --group --no-create-home redis

Additionally, the Redis group and user ownership should be given to the Redis directory:

chown redis:redis /var/www/redis
chmod 770 /var/www/redis

Then you need to copy the Redis sample configuration file from the Redis source directory to the directory which was created above:

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

As your next step, you should open the Redis configuration file. From there, you can make some changes as per your requirements:

nano /etc/redis/redis.conf

Our suggestion is for you to make the following changes:

port  6379
supervised systemd
logfile /var/log/redis.log
dir  /var/www/redis/
bind your_server_IP_address

After you have finished with all of that, you can save and close the file.

5. Create a Redis Systemd File

Furthermore, a system file that will control and manage the Redis daemon should be created.
This can be done by creating a redis.service file:

nano /etc/systemd/system/redis.service

The following lines need to be added:

[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

Once you are done with this, you can save and close the file. Then you can start the Redis service using the following command:

systemctl start redis

Next thing you need to do is to check the Redis service status and see whether it is running or not:

systemctl status redis

If you did everything right, you are going to see the following output:

● redis.service - Redis In-Memory Data Store
   Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-07-28 01:10:03 EDT; 1min 6s ago
 Main PID: 1063 (redis-server)
    Tasks: 4 (limit: 4915)
   CGroup: /system.slice/redis.service
           └─1063 /usr/local/bin/redis-server 127.0.0.1:6379