Menu

How to Install and Configure Redis on Ubuntu 16.04

5 years ago 2

Redis is a key value storage used widely for its support and flexibility. It is an open source application mainly used in Database for fast accessing of Data in a Database.

Redis is the fastest database and a lot of people love it for its speed. And most of the people’s common question is about Redis Persistence.

Also, there is a myth around that and people think that they can only use Redis for storing temporary data and won’t give persistence like MySQL and MongoDB.

But:

It is not true. You can store the data securely for a long time in Redis as same as MySQL and MongoDB.

Today in this article, we are going to see how to install and configure Redis on Ubuntu server.

Requirement:

You should have Ubuntu 16.04 configured according to this article.

Installing Redis

First, update the Ubuntu Package Repository using the below command.

$ sudo apt-get update

Also, you have to install the build-essential and tcl package on your Ubuntu server. The below command will install both packages.

$ sudo apt-get install build-essential tcl

Download the Redis source

After that, you have to download the Redis from their official page.

First, we will download the Redis source code to a temporary file.

Then, We will extract the file and install it.

First, change the directory

$ cd /tmp

Now:

Download the Redis source code file from their official website.

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

Make sure that you have installed the curl function to do this.

$ tar xzvf redis-stable.tar.gz

The file will be extracted to the Redis-stable file.

After that change the directory to redis-stable

$ cd redis-stable

Installing Redis

You have to use the make command utility to compile all the Redis binaries.

Now you are in the redis-stable folder. Execute the below command.

Since the redis key values need not be text strings, They are in binary status and processed so fast.

$ make

After the compilation of Redis binary, you have to check whether everything is working correctly or not.

Execute the below command

$ make test

Then, install the binaries using the below command. Make sure that you are staying in the same folder and not on home directory.

$ sudo make install

Redis Configuration

Here, we are going to create a configuration directory for redis and we will copy the original configuration file from the tmp folder.

First, create the directory to store configuration file.

$ sudo mkdir /etc/redis

Then copy the configuration file to that directory.

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

Now, edit the configuration file.

$ sudo nano /etc/redis/redis.conf

In that configuration file, you have to find the supervised directive. It would be set to ‘no’ by default and you have to set it to systemd.

After the change, the file will look like below one

/etc/redis/redis.conf
. . .

# 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

Previously we have talked about the Redis Persistence. To make the redis data persistence, you have to store them in a particular place.

Also, the Redis should have the Permission to read and write in that location,

Find the dir directory in the configuration. And set the location to /var/lib/redis

This is how the file will look like after the change.

/etc/redis/redis.conf

. . .
# 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
. . .

Systemd Unit file Creation of Redis

Now, you have to create a systemd unit file for Redis. To do that, execute the below command.

Open redis.service file using nano editor.

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

The network should be available before the initiation of service. For that, use the following code.

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

After that we have to define the service. The service section will contain how a server should behave.

The service cannot be run on the root for some security reasons. For that, we will use a username and user group.

The user group will be entirely dedicated the service.

To start the service, we will use the Redis-server binary and to stop the service; we will use the Redis-cli binary.

Also, set the Restart directive to ‘always’ to make it recover from any failure.

/etc/systemd/system/redis.service

[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
At last, define the [install] section. Here, you will set the target to which the service has to be applied. 

[Install]
WantedBy=multi-user.target

After that, save and close the service file.

Creating the Redis User, Group and Directories

Since we cannot use the root for the Redis service to run, we are going to create user and user group.

Use the below command to create a user and user group.

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

Then, you have to create the directory.

$ sudo mkdir /var/lib/redis

The directory is created and now you have to give the ownership of the directory to the newly created user and user group.

$ sudo chown redis:redis /var/lib/redis

Now:

You have to block the user or group which doesn’t have ownership towards the directory.

$ sudo chmod 770 /var/lib/redis

Check the Redis status

First, you have to start the Redis. Use the below command to start the Redis.

$ sudo systemctl start redis

Now, check the status of Redis

$ sudo systemctl status redis

If the output is active, then it means that you have not made any errors.

Output

● redis.service - Redis Server
   Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2017-04-16 12:25:18 EDT; 1min 43s ago
  Process: 3115 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=0/SUCCESS)
 Main PID: 3124 (redis-server)
    Tasks: 3 (limit: 512)
   Memory: 864.0K
      CPU: 179ms
   CGroup: /system.slice/redis.service
           └─3124 /usr/local/bin/redis-server 127.0.0.1:6379       
Test the Redis Server

You have to check whether the Redis server is working properly or not. For that, connect to the server using the client comment.

$ redis-cli

By default, the redis will be connected to 127.0.0.1. Enter the below address to check the connectivity.

127.0.0:6379> ping
You will get the output

PONG

Again test the Redis server.

You will get following output

“It’s working!”

After that, return to to the shell by typing

Exit

That’s all. Now you are done with all the configurations and testing process. The next task is you have to make the Redis start at the boot.

The below command is enough to do that.

$ sudo systemctl enable redis

You will get the following output

Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.
Conclusion:

Now you have learned how to install and configure redis on Ubuntu.

If you still have the doubt in the execution of Redis, just ask them in the comment section.

I will help you to fix it.