Install Redis on Raspberry Pi from Compiled Source

We need a method to store values on the pi temporarily. Additionally we are going to need some sort of broadcasting or event system for notifying certain components and triggering devices on our setup in the future.

Redis (~50min)

It is now time to install our first package on the raspberry pi. One of the requirements we will need is Redis which will allow us to store our sensor readings and eventually listen for updates to react to them such as storing data to a file for logging purposes.

Redis is a great solution for our needs and works quite fast as a temporary key/value store. It also has some publish/subscribe features that we will take advantage of.

Installing Redis

First, go to your user's home directory

cd ~

Download the latest release of redis using wget with the following command.

wget <http://download.redis.io/redis-stable.tar.gz>

We need to uncompress the file we just downloaded

tar xzf redis*

Once it finishes extracting there will be a folder named redis available. We need to enter that directory. For me the folder was named redis-stable

cd redis-stable

Now we should build Redis from its source by using the provided makefile. You can do so with the following commands.

sudo make
make test
sudo make install PREFIX=/usr

You may run into errors when you run your make test. (Thats why its good to run it) In my case the test failed with “You need tcl version 8.5 in order to run make test”

After a quick google I found I just need to apt-get the newest version.

sudo apt-get install -y tcl

Easy. Now the make test and make install ran without error.

Redis is installed but we now need to add it into a directory for users to access.

sudo mkdir /etc/redis

Install the compiled redis to our newly created folder

sudo cp redis.conf /etc/redis/

Now we will remove our install files and wrap up the installation

cd ..

Remove the archive and folder we no longer need.

sudo rm -Rf redis*

Create a new user that we are going to use use to run Redis. This user will not need a home directory or ability to login

sudo adduser --system --group --disabled-login redis --no-create-home --shell /bin/nologin --quiet

You can check if the user was successfully created with the following command that pulls info from the shadow file.

cat /etc/passwd | grep redis

We are ready to edit the redis configuration now. (Use any editor you like)

sudo nano /etc/redis/redis.conf

Here are the settings you will probably want to change. Some of them may already be set for you. These will keep redis running in the case it encounters an error and limit our memory since the pi only has limited amount.

bind 127.0.0.1
port 6379
daemonize yes
stop-writes-on-bgsave-error no
rdbcompression yes
maxmemory 70M
maxmemory-policy allkeys-lru

Its now time to create the script that will execute and run the Redis server. Create the directory for our script to run from and set our redis user we created as the owner.

sudo mkdir -p /var/run/redis
sudo chown -R redis /var/run/redis

Using the editor of your choice create the script file.

sudo nano /etc/init.d/redis-server

Paste the following into the file. (Google “redis-server init.d script” for more)

#! /bin/sh
### BEGIN INIT INFO
# Provides: redis-server
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: redis-server - Persistent key-value db
# Description: redis-server - Persistent key-value db
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/redis-server
DAEMON_ARGS=/etc/redis/redis.conf
NAME=redis-server
DESC=redis-server

RUNDIR=/var/run/redis
PIDFILE=$RUNDIR/redis-server.pid

test -x $DAEMON || exit 0

if [ -r /etc/default/$NAME ]
then
. /etc/default/$NAME
fi

. /lib/lsb/init-functions

set -e

case "$1" in
  start)
echo -n "Starting $DESC: "
mkdir -p $RUNDIR
touch $PIDFILE
chown redis:redis $RUNDIR $PIDFILE
chmod 755 $RUNDIR

if [ -n "$ULIMIT" ]
then
ulimit -n $ULIMIT
fi

if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON -- $DAEMON_ARGS
then
echo "$NAME."
else
echo "failed"
fi
;;
  stop)
echo -n "Stopping $DESC: "
if start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
then
echo "$NAME."
else
echo "failed"
fi
rm -f $PIDFILE
sleep 1
;;

  restart|force-reload)
${0} stop
${0} start
;;

  status)
status_of_proc -p ${PIDFILE} ${DAEMON} ${NAME}
;;

  *)
echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac

exit 0

Give the script execute privileges

sudo chmod +x /etc/init.d/redis-server

Make the script runnable as a command

sudo update-rc.d redis-server defaults

This will allow you to run the following command to manage redis is everything is set correctly.

sudo service redis-server restart

Redis is installed.

You can check if its running and get the version to be sure.

redis-server -v

You can interface with the redis-server using the redis-cli, the Redis command line interface – Redis from your terminal. Type exit to close the cli.

redis-cli

If you plan on using Redis with PHP then the last step you may want to do is installing phpredis.

Installing PHPRredis (Bonus)

You could also install predis which is simple but if you plan to heavily use redis, phpredis can yield better performance.

First there are some packages we will need to install.

sudo apt-get install libtool php7.3-dev

Now lets clone the phpredis repository since we are compiling from source. If this command fails make sure you have git installed.

git clone <https://github.com/phpredis/phpredis.git>

Enter the folder we just downloaded for phpredis

cd phpredis

Switch the the master branch. Now pull the latest changes if there are any.

git checkout master

git pull

Prepare the source code to be compiled and then compile it with the make commands. Finally install the compile source.

phpize
./configure
make clean
make
sudo make install

Now you can exit the folder and cleanup the install files.

cd ..
rm -rf phpredis

The next part is easier if you are super user. We can open the terminal using sudo to become super user. Or you can use the su command.

sudo /bin/bash

Enable the redis extension now in our PHP.

echo "extension=redis.so" > /etc/php/7.3/mods-available/redis.ini
ln -sf /etc/php/7.3/mods-available/redis.ini /etc/php/7.3/fpm/conf.d/20-redis.ini
ln -sf /etc/php/7.3/mods-available/redis.ini /etc/php/7.3/cli/conf.d/20-redis.ini

Exit from super user.

exit

Restart some services and PHPRedis should now be fully installed!

service php7.3-fpm restart
service nginx restart

Next Step >
Basic Automated Garden from Scratch
Preparing the SD Card 4:02
Booting Up & Configuring Raspbian 4:48
SSH and SSH Keys (Video Coming Soon)
4 Install Redis on Raspberry Pi from Compiled Source (Video Coming Soon)
5 Updating Python on Raspbian & Installing Useful Packages (Video Coming Soon)
6 How to Install MudPi from GitHub Repository (Video Coming Soon)
7 Attaching Our First Sensor (Video Coming Soon)
8 Using a Relay for the Pump (Video Coming Soon)
9 Logging Values to a File (Video Coming Soon)

Stay Up To Date With MudPi

MudPi is still being developed and updated with new features. Drop your email and I will contact you once or twice a month with the latest updates.