How To Install Redis on Ubuntu 16.04

Redis is an open source in-memory key-value store known for its performance and flexibility. Redis has built-in replication and can be set up in a master-slave configuration. For more information read here.

Background

I want to add an object cache for my php based blog on AWS LightSail and I choose Redis for this. Amazon already has ElastiCache service allowing us to create a Redis server. However, what if you just wanted to experiment with Redis or did not want to pay for an AWS Redis service, in which case you could install Redis yourself on your server.

Procedure

I am assuming you are running an AWS LightSail with Ubuntu 16.04 as the base operating system and you have full root access to the server. Also, it is better to make a Snapshot of your server before you go any further. This is in case things go horribly wrong and you wish to rollback.

The suggested way of installing Redis is to compile it from source as the software has no dependency other than a GCC compiler and libc. The Redis website has a special URL that always points to the latest stable version of Redis. We will use this in our install.

Build and Test dependencies

Login to your server and prepare the server to satisfy build dependencies. We need to confirm we have software to compile and run tests for Redis. The tcl package is a requirement for us to be able to run make test

sudo apt install update
sudo apt install build-essential tcl

Make a temporary work directory and cd to it. This directory will serve as our base for compiling the software. After installation is done, you can go ahead and remove the temporary work directory. Use wget to download the software, extract it using tar and cd into the source directory.

mkdir ~/work && cd ~/work 
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make

After the binaries are successfully compiled it is a good idea to run make test from the command line.

make test
make test passed

After all tests are passed you can go ahead and install the binaries by typing

sudo make install

Server Configuration

Next step is to copy the server configuration file. We will use the standard location /etc/redis/redis.conf

sudo mkdir /etc/redis 
cp ~/work/redis-stable/redis.con /etc/redis

Use your favorite editor to edit this configuration file to make some minor edits

sudo vi /etc/redis/redis.conf

Find the working directory section and set the dir to /var/lib/redis. We will create this directory after we create the redis user.

# Note that you must specify a directory here, not a file name.
dir /var/lib/redis

Also, change supervision method to system as we will setup auto start using systemd.

# Note: these supervision methods only signal "process is ready." 
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd

Next, we will create a user, group and directory for redis. Change the ownership of the directory so that redis user can write to it. Also set the permissions such that other users cannot access the files in that directory.

sudo adduser --system --group --no-create-home redis
sudo mkdir -p /var/lib/redis
sudo chown redis:redis /var/lib/redis
sudo chmod 770 /var/lib/redis

To start the service on boot we will set a systemd script. Edit the file /etc/system/system/redis.service and aste the following in it.

[Unit]
Description=Redis Server
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

Save the script and do a service reload. Enable and start the Redis Server. If you make changes to the service file you should do a daemon reload.

systemctl daemon-reload
systemctl enable redis.service
systemctl start redis.service

To test the Redis server you can run a ping command and you should get a message back PONG if the service is running.

/etc/systemd/system# redis-cli ping
PONG

Another way to see messages related to the service is via journalctl as shown below.

journalctl | grep redis-server
redis-server[1214]: 1214:M 21 Dec 2018 18:19:14.882 # Server initialized

You should see a server initialized message.

At this point our Redis server is ready to be used. If possible, you can also boot the server to verify if the service starts up automatically.

Further reading

Leave a Reply