INSTALL AND CONFIGURE REDIS ON UBUNTU 16.04
Redis is an in-memory data structure store primarily used as a database and as a cache.
In this tutorial, We are going to install Redis on Ubuntu 16.04 in an easy way.
You just need a Linux server with SSH access it with Ubuntu16.04.
In this article, We are installing Redis from the source.Make sure your system is up-to-date.
In order to get the latest version of Redis, We are going to compile and install the software from the source.
Note: Before compiling We need to make sure the server meets the build dependencies.
We need to install build-essential packages from the local Ubuntu repository and install Tcl packages to test our binaries after installation.
Installation of Redis
Run the below command to update the local apt packages and then install dependencies,
#sudo apt-get update -y
#sudo apt-get install build-essential tcl -y
Download, Compile & Install
Now We are going to download the redis and then compile,
Download & Extract the Source Code of Redis
To download the latest stable version of Redis:
#curl -o http://download.redis.io/redis-stable.tar.gz
[Alternatively, you can download the stable version here http://download.redis.io/redis-stable.tar.gz]
Now extract the .tar file:
#tar xvzf redis-stable.tar.gz
Move the source directory of Redis We have just extracted.
Build & Install Redis
Compile the redis binaries by running the below commands.
After the binaries are compiled, Run the test suite to ensure that all are installed/built correctly.
This will take around 5 to 10 minutes.Once it is completed, You can install binaries using the below command.
Configuration of Redis
Next, We need to configure redis.First We need to create a Configuration Directory, We will use the conventional directory /etc/redis directory.
To create a directory:
Copy the Redis sample configuration file which is included in Redis source archive.
#cp /tmp/redis-stable/redis.conf /etc/redis
Next, We need to make certain changes in the Configuration file.
Fine the supervised directive, Currently this is set to no.We need to change this to systemd Because We are running systemd init system in our Operating system.
. . . # 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 directory, This is the location where redis will use to store the persistent data.
. . . # 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 . . .
We will use /var/lib/redis directory to store persistent data which we will create in the upcoming steps.
Save and close the file when the changes are done.
Create Redis Systemd Unit File
We are creating systemd unit file for redis So that init system can manage the redis process.
create a new file, redis.service under /etc/systemd/system/ directory.
Open the file.We can begin with [Unit] section.
We need to add description and define a requirement that networking be available before starting the service
[Unit] Description=Redis In-Memory Data Store After=network.target
Next, In the [Service] section, We need to specify the behavior of services.For the purpose of security, We are running services as Redis user/ group Instead of using root user.We will redis user and group in some time.
For starting the service, We need to call the binary of Redis-server and Shutdown to stop the service.
To recover the redis from the failures, We will set the Restart directive to Always.
[Service] User=redis Group=redis ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf ExecStop=/usr/local/bin/redis-cli shutdown Restart=always
Finally, in the [Install] section, We have to define systemd target that the service is configured to start at boot.
Save the file and exit.
Create the Redis User, Group & Directory
We have to create redis user and group which we have mentioned above using the below command:
#add-user –system -group –no-create-home redis
Now We have to create redis directory to store the persistent Data:
We have to change the ownership of redis directory to redis user.
#chown redis: /var/lib/redis
To deny access to /var/lib/redis directory for regular users.
#chmod 770 /var/lib/redis
Start & Test Redis
Use the below command to start and stop the redis service and to check the status of the redis service
#systemctl start redis
#systemctl status redis
#systemctl stop redis
To make sure whether the Redis service is functioning as expected.Type the below command.
Test the connectivity by tying ping:
You should see the output as PONG
To Enable the Redis Service to Start on Boot
#systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.