You are here:

Redis installation on an Ubuntu 16.04 VPS server

Redis installation on an Ubuntu 16.04 VPS server

Overview

What is Redis?

It is an open-source, BSD-licensed, in-memory data structure store known as Redis[1]. It serves various purposes, functioning as a database (DB), cache, and message broker. Redis supports a versatile set of data structures, including strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, and geospatial indexes with radius queries.

Requirements

Blendhosting.com offers Ubuntu 16 servers, available for purchase at https://www.blendhosting.com/vps/[2].

To manage and connect to your VPS server, you’ll use an SSH client.

Ensure you have the necessary knowledge in Linux command line utilities and basics to make the most of your server.

Update and Install Dependencies

As with any Ubuntu packages, Redis has dependencies that need to be downloaded and installed before it can be utilized. Key dependencies for Redis include build-essential and tcl. Use the following commands to install these necessary packages:

suo apt-get update
sudo apt-get install build-essential tcl curl make

With the essential packages in place, we are now ready to proceed with the installation of Redis.

Redis Installation Guide

To install Redis, obtain the latest package from the Redis website using the following command and execute it using curl:

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

Unpack the stable version using the appropriate commands:

tar xzvf redis-stable.tar.gz

Extract the Redis source files from the downloaded package using the necessary extraction commands:

cd redis-stable

Compile the binaries first by entering the following command:

make

After the compilation process is completed, proceed to install Redis on your server:

make install

Setting Up Redis

Now that Redis is installed, it’s time to configure it for use. Create a configuration directory to store Redis configuration files. In this case, let’s create it at /etc/redis:

sudo mkdir /etc/redis

Copy the sample Redis configuration file, included in the Redis source, to the newly created directory:

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

After copying the sample Redis configuration file to the new directory, open the configuration file to add necessary configuration statements:

sudo nano /etc/redis/redis.conf

In the configuration file, locate the ‘supervised’ directive. Currently, this directive is set to ‘no.’ As we are running a system that uses systemd init, update this directive to reflect systemd.

# 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

After making the necessary updates, save and close the file.

Redis Unit File Creation

To enable the init system to manage the Redis process, create and open the ‘redis.service’ file using the following command:

sudo nano /etc/systemd/system/redis.service

Once inside, copy and paste the details below, and include an appropriate description along with networking requirements:

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

Next, add a [Service] section to specify the service’s behavior. For security considerations, it’s recommended to use a dedicated user and group, which we will refer to as ‘redisUser.’ Copy the details below and append them at the bottom of the [Unit] section added earlier.

[Service]
User=redisUser
Group=redisUser
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

Finally, include the [Install] section in the service file. This section defines the systemd target to which the service should attach.

[Install]
WantedBy=multi-user.target

Once you have made the necessary changes, save and close the file.

Establishing Redis Groups, Users, and Directories

Next, create a Redis user and group, along with the necessary directories, before starting Redis.

Begin by adding the ‘redisUser’ user and group using the following command:

sudo adduser --system --group --no-create-home redisUser

After creating the ‘redisUser’ user and group, proceed to create the ‘/var/lib/redis’ directory by:

sudo mkdir /var/lib/redis

Next, assign ownership of the above directory ‘/var/lib/redis’ to the ‘redisUser’ user and group:

sudo chown redisUser:redisUser /var/lib/redis

Adjust the permissions to ensure seamless access to this directory without encountering any issues:

sudo chmod 770 /var/lib/redis

Launching and Verifying Redis

Now that Redis is configured and ready for use, you can initiate and test your Redis installation. To start Redis, execute the following command:

sudo systemctl start redis

To verify if Redis starts up without errors, execute the following command:

sudo systemctl status redis

You should observe output similar to the following:

Output
● redis.service - Redis Server
Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2016-11-01 20:12:07 EDT; 1min 2s ago
Process: 2165 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=0/SUCCESS)
Main PID: 2166 (redis-server)
Tasks: 3 (limit: 512)
Memory: 752.0K
CPU: 125ms
CGroup: /system.slice/redis.service
└─2166 /usr/local/bin/redis-server 127.0.0.1:6379

. . .

To test Redis functionality, you can launch the Redis command line client by typing and running:

redis-cli

In the prompt, execute:

127.0.0.1:6379> ping

You should observe:

output
PONG

Now, verify if you can set a key by executing

127.0.0.1:6379> set test "Hello!"

The output should display:

output
OK

To retrieve the value that was just stored, execute:

127.0.0.1:6379> get test

The output should show:

output
"Hello!"

Exit the Redis CLI, and proceed to restart the Redis instance:

127.0.0.1:6379> exit
sudo systemctl restart redis

Now, reconnect to the Redis client and confirm whether the test values are still present:

redis-cli

To retrieve the ‘test value’, execute:

127.0.0.1:6379> get test

The output should remain:

output
"Hello!"

Redis Running at Boot

Once you have confirmed that all tests are working as expected, you can enable Redis to start at boot time by running:

sudo systemctl enable redis

Now that Redis is operational, you can utilize it to store values from your application. In our next article, we will delve into securing Redis for production environments. We appreciate and welcome your suggestions and comments; please feel free to share them below.

[1]: https://redis.io/
[2]: https://www.blendhosting.com/vps/

Was this article helpful?
Dislike 0