How to Install Redis Server from Source
Redis is an in-memory key-value store database. There are a few different ways to install redis-server in a Linux based system. To fit our purpose we are going to use the current (latest) stable release from REDIS.IO This process of installation is a bit complex because it requires you to build, compile and install the service manually. So hold on tight…
Install Build Dependencies
Before we download the source code we need to install the build dependencies meta package from ubuntu repositories.
sudo apt-get update
sudo apt-get install build-essential tcl
We are installing tcl
package also to test the binaries after compilation
Download, Compile, Install Redis
After we are done with the compilation of the source code we can purge the files, so we can use /tmp
folder to download and compile the source code.
Let’s change the directory to /tmp
using
cd /tmp
Now we can download the tarball
directly in this folder.
curl -O http://download.redis.io/redis-stable.tar.gz
Extract the tarball using
tar -xzvf redis-stable.tar.gz
Change the directory to redis-stable
cd redis-stable
Now we can compile the source code
make
After the compilation is complete, run the test suit to make sure that everything was built correctly
make test
Once the test is finished and everything is okay, it’s time to install the binaries into the system
sudo make install
Once the installation is successful, we can move on to configuring the redis-server
To check the installation process was successful or not, use the following command
redis-server --version
It should show something like below
Redis server v=5.0.5 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=2f03bf541f259179
Configure Redis Server
To start the configuration process we need to create a configuration directory in /etc
using
sudo mkdir /etc/redis
The next step is to copy the sample configuration file that comes with the source code to the recently created /etc/redis
directory, using
sudo cp ./redis.conf /etc/redis
Now it’s time to tweak a few configurations, open the file and edit like below
sudo nano /etc/redis/redis.conf
In the file search for supervised
directive and change it to systemd
, default is set to no
.
The supervised
directive will look like following
supervised systemd
Now, we can configure the persistent data storage location by finding the dir
directive. Change it to /var/lib/redis
dir /var/lib/redis
Now, find the logfile
directive and change it to logfile /var/log/redis/redis-server.log
logfile "/var/log/redis/redis-server.log"
Now, it’s time to find the pidfile
directive, change it to pidfile /var/run/redis/redis-server.pid
pidfile /var/run/redis/redis-server.pid
redis must have access and write permission to these
/var/lib/redis
/var/log/redis/
/var/run/redis/
directories in order to be able to dump data
Save the file and exit.
Create Redis User, Group and Data Directory
Now we can create redis
user and group and also the directory we mentioned in the configuration file. We start by adding a user and group in the system
sudo adduser --system --group --no-create-home redis
Now once the user and group is created we can move on to creating the directory, we can create the directory by using
sudo mkdir /var/lib/redis /var/log/redis /var/run/redis
Provide redis
user and group permission to the newly-created directory. To do that use the following
sudo chown redis:redis /var/lib/redis /var/log/redis /var/run/redis
Now, we can tweak the permission of this folder so that other regular users can not access this directory. To achieve that, use the following command
sudo chmod 770 /var/lib/redis /var/log/redis /var/run/redis
Once user, group, and directory permissions are in place, let’s create the systemd
unit file to run redis as a service.
Create Systemd Unit File
Create a file named redis-server.service
in /lib/systemd/system
using the following command
sudo nano /lib/systemd/system/redis.service
Add the following content to the file, save it, and exit.
[Unit]
Description=Advanced key-value store
After=network.target
[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/bin/kill -s TERM $MAINPID
PIDFile=/var/run/redis/redis-server.pid
TimeoutStopSec=0
Restart=always
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=2755
UMask=007
PrivateTmp=yes
LimitNOFILE=65535
PrivateDevices=yes
ProtectHome=yes
ReadOnlyDirectories=/
ReadWriteDirectories=-/var/lib/redis
ReadWriteDirectories=-/var/log/redis
ReadWriteDirectories=-/var/run/redis
NoNewPrivileges=true
CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE
MemoryDenyWriteExecute=true
ProtectKernelModules=true
ProtectKernelTunables=true
ProtectControlGroups=true
RestrictRealtime=true
RestrictNamespaces=true
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
# redis-server can write to its own config file when in cluster mode so we
# permit writing there by default. If you are not using this feature, it is
# recommended that you replace the following lines with "ProtectSystem=full".
ProtectSystem=true
ReadWriteDirectories=-/etc/redis
[Install]
WantedBy=multi-user.target
Alias=redis.service
Once this file is created in /lib/systemd/system/
create a symlink to it from /etc/sysetmd/system
with
sudo ln -s /lib/systemd/system/redis-server.service /etc/systemd/system/redis.service
Please note that two directories are different, one is /lib
another is /etc
, although the child directory names are the same
Start Redis Server
Star the redis systemd process by using the following command
sudo systemctl start redis.service
The current status of redis-server
can be checked with
sudo systemctl status redis
If everything went Okay, this should produce an output similar to following
● redis-server.service - Advanced key-value store
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2019-08-16 19:15:34 CEST; 11s ago
Main PID: 30540 (redis-server)
Tasks: 4 (limit: 4915)
CGroup: /system.slice/redis-server.service
└─30540 /usr/local/bin/redis-server 127.0.0.1:6379
Aug 16 19:15:34 isemg-p-ntr001 systemd[1]: Started Advanced key-value store.
Test Redis
To test that the newly installed redis-server
instance is functioning properly, connect to redis server using the command line client for redis.
redis-cli
In the prompt that follows the above command, try ping
command to test the connectivity with the server
127.0.0.1:6379> ping
It should return something like below
PONG
This action confirms the connection to redis server
, now let’s try setting a key-value pair and retrieving it.
127.0.0.1:6379> set test "this is a test string from the blog"
The output should be similar as below
OK
Now let’s retrive the saved value
127.0.0.1:6379> get test
In the output it should return the text we set in the previous step
"this is a test string from the blog"
Once it’s confirmed, let’s restart the redis server and confirm that it’s persistent with data even after restart
sudo systemcrl restart redis
Now let’s try to retrive the stored value for key test
redis-cli
127.0.0.1:6379> get test
"this is a test string from lego python blog"
This confirms that redis installation is fully operational and ready for use.
Enjoy!