Redis 3.0 Compile/Install HOWTO

I'm going to assume this install takes place on a stock install of Ubuntu 15.10 without the Redis packages installed. This particular example shows a custom-compile of Redis 3.0.5.

Redis 3.0.5 Build

4 Dec 2015

Create the redis user and group.

Note we do this as root:

$ su -
# addgroup --gid 1002 redis
# adduser --home /home/redis --uid 1002 --gid 1002 --disabled-password redis

Get the Redis source code and build it. This is very straightforward. Note, however, that we are going to install in /usr/local/redis-3.0.5, to cordon this off from the rest of our system.

# cd /usr/local/src
# wget http://download.redis.io/releases/redis-3.0.5.tar.gz
# tar -xzvf redis-3.0.5.tar.gz
# cd redis-3.0.5/
# make
# make test
# make PREFIX=/usr/local/redis-3.0.5 install

This only installs the Redis binaries. It would be nice make's install target also gave us a configuration file, etc, etc, but there is a utility script in the source that we can use. Please note that I use all the options to put everything in a subdirectory of /usr/local/redis-3.0.5.

# cd utils
# ./install_server.sh

Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] /usr/local/redis-3.0.5/conf/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] /usr/local/redis-3.0.5/logs/6379.log
Please select the data directory for this instance [/var/lib/redis/6379] /usr/local/redis-3.0.5/data/6379
Please select the redis executable path [] /usr/local/redis-3.0.5/bin/redis-server
Selected config:
Port           : 6379
Config file    : /usr/local/redis-3.0.5/conf/6379.conf
Log file       : /usr/local/redis-3.0.5/logs/6379.log
Data dir       : /usr/local/redis-3.0.5/data/6379
Executable     : /usr/local/redis-3.0.5/bin/redis-server
Cli Executable : /usr/local/redis-3.0.5/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort. <<enter>>
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Success!
Starting Redis server...
Installation successful!

Unfortunately, ./install_server.sh has set us up with an old-school SysV init script, so we must go dismantle it.

# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
# update-rc.d -n redis_6379 remove
insserv: remove service /etc/init.d/../rc0.d/K01redis_6379
insserv: remove service /etc/init.d/../rc1.d/K01redis_6379
insserv: remove service /etc/init.d/../rc2.d/S02redis_6379
insserv: remove service /etc/init.d/../rc3.d/S02redis_6379
insserv: remove service /etc/init.d/../rc4.d/S02redis_6379
insserv: remove service /etc/init.d/../rc5.d/S02redis_6379
insserv: remove service /etc/init.d/../rc6.d/K01redis_6379
insserv: dryrun, not creating .depend.boot, .depend.start, and .depend.stop
# rm /etc/init.d/redis_6379

Also, sadly, the default conf file wants to put the pid file in /var/run, so in the interests of keeping everything under the /usr/local/redis-3.0.5 directory, let's fix that now.

# mkdir -p /usr/local/redis-3.0.5/var
# vi /usr/local/redis-3.0.5/conf/6379.conf
pidfile /var/run/redis_6379.pid ==> pidfile /usr/local/redis-3.0.5/var/redis_6379.pid

Let's chown the entire Redis directory so that we can run Redis as user redis.

# chown -R redis:redis /usr/local/redis-3.0.5/

Now let's create a systemd file for Redis.

# cd /etc/systemd/system
# vi redis-6379.service
[Unit]
Description=Redis 3.0.5
# This unit can only run after the network is up and running
# (that is, the network target has run)
After=network.target

[Service]
# Redis is a traditional UNIX daemon that forks a child,
# and the initial process exits
Type=forking
# Wait 120 seconds on startup and shutdown to consider the process
# correctly started up or shut down.
TimeoutSec=120
# The UNIX user and group to execute Redis as
User=redis
Group=redis

# Set the REDISROOT environmental variable for Redis
Environment=REDISROOT=/usr/local/redis-3.0.5

# If StandardOutput= or StandardError= are set to syslog, journal or kmsg,
# prefix log lines with "postgres"
SyslogIdentifier=postgres

# Let systemd know where Redis keeps its pid file
PIDFile=/usr/local/redis-3.0.5/var/redis_6379.pid

# Command used to start Redis
ExecStart= /usr/local/redis-3.0.5/bin/redis-server ${REDISROOT}/conf/6379.conf
# Command used to stop Redis
ExecStop= /bin/kill $(cat /usr/local/redis-3.0.5/var/redis_6379.pid)

# Use the lowest allowable setting for the OOM killer; this should
# actually disable the OOM killer for Redis
OOMScoreAdjust=-1000

[Install]
# This unit is part of target multi-user
WantedBy=multi-user.target

Now let's test:

# systemctl daemon-reload
# systemctl start redis-6379
# systemctl status redis-6379
# systemctl stop redis-6379
# systemctl status redis-6379

Everything worked as expected.