Introduction
Redis is generally used as a database, cache, and message broker. Also, it is known for its flexibility and performance, once it supports a wide variety of data structures.
In this tutorial you’ll learn how to install and configure Redis from source on an Ubuntu 18.04 server.
Prerequisites
To follow this guide, you’ll need:
- Access to an Ubuntu 18.04 server with a non-root user with sudo privileges and firewall;
Step 1 - Installing the Build and Test Dependencies
First step is to download and install Redis from source, in order to get the latest version of the software.
For this, install the build-essential meta-package from the Ubuntu repositories. Also, download the tcl package which you can use to test the binaries. Update your local apt package cache and install the dependencies by typing:
$$apt update
apt install build-essential tcl
With that, all the build and test dependencies are installed on your server and you can begin the process of installing Redis itself.
Step 2 - Downloading, Compiling, and Installing Redis
After all the dependencies are set, you can install Redis from the source code.
Start by navigating to this directory:
$cd /tmp
Next, use curl
to download the latest stable version of Redis. The latest version can always be found at a stable download URL:
$curl -O http://download.redis.io/redis-stable.tar.gz
Unpack the tarball by typing:
$tar xzvf redis-stable.tar.gz
Then move into the Redis source directory structure that was just extracted:
$cd redis-stable
Compile the Redis binaries by typing:
$make
Once the binaries have finished compiling, run the test suite to make sure everything was built correctly:
$make test
This typically takes a few minutes to finish. Once the test completes, install the binaries onto the system by typing:
$sudo make install
That’s it for installing Redis, and now you’re ready to start configuring it. To this end, you’ll have to create a configuration directory. The Redis configuration directory is conventionally located within the /etc/ directory, and you can create it there by typing:
$mkdir /etc/redis
Next, copy over the sample Redis configuration file that came included with the Redis source archive:
$cp /tmp/redis-stable/redis.conf /etc/redis
Open the file with your preferred text editor to make a few changes to the configuration:
$nano /etc/redis/redis.conf
Inside the file, find the supervised
directive. This directive allows you to declare an init system to manage Redis as a service, providing you with more control over its operation. The supervised
directive is set to no
by default. Since you are running Ubuntu, which uses the systemd init system, change this to systemd
:
/etc/redis/redis.conf
... 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 mode3 #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 directive. This option specifies the directory which Redis will use to dump persistent data. You need to change this to a location where Redis will have write permissions and which isn't viewable by normal users. Use the /var/lib/redis directory for this; you will create this directory and adjust its permissions later in Step 4:
/etc/redis/redis.conf
. . .
#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
. . .
Save and close the file when you are finished. Those are all the changes you need to make to the Redis configuration file, but there are still a few steps you need to go through before you can start using it.
Step 3 - Creating a Redis systemd Unit File
To have more control over Redis, create a systemd unit file that allows it to work as a systemd service. This also gives the benefit of making it easy to enable Redis to start up whenever your server boots.
Create and open the /etc/systemd/system/redis.service
file to get started:
$nano /etc/systemd/system/redis.service
Once inside, start the [Unit]
section by adding a description of the service and defining a requirement that networking must be available before it is started:
/etc/systemd/system/redis.service
[Unit] Description=Redis In-Memory Data Store After=network.target
The [Service]
section is where you specify the service's behavior. You should not run this service as root, for security reasons. You should instead use a dedicated user and group and you can call both of these redis.
To start the service, you just need to call the redis-server
binary and point it at your configuration. To stop it, use the Redis shutdown
command, can be executed with the redis-cli binary
. Also, since it’s desirable to have Redis recover from failures whenever possible, set the Restart
directive to always
:
/etc/systemd/system/redis.service
[Unit] Description=Redis In-Memory Data Store 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
Finally, add an [Install]
section. There, define the systemd target that the service should attach to if it’s enabled (meaning that it’s configured to start at boot):
/etc/systemd/system/redis.service
[Unit] Description=Redis In-Memory Data Store 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 and close the file when you are finished.
The Redis systemd unit file is all set. Before it can be put to use, though, you must create the dedicated user and group you referenced in the [Service]
section and grant them the permissions they need to function.
Step 4 - Creating the Redis User, Group, and Directories
Lastly, you need to create a user, group and directory that you referenced in the previous files.
Begin by creating the redis user and group. You can do this in a single command by typing:
$adduser --system --group --no-create-home redis
Then, create the /var/lib/redis
directory (which is referenced in the redis.conf file you created in Step 2) by typing:
$mkdir /var/lib/redis
Give the redis
user and group ownership over this directory:
$chown redis:redis /var/lib/redis
Finally, adjust the permissions so that regular users cannot access this location:
$chmod 770 /var/lib/redis
You’re now ready to start the Redis service and test its functionality.
Step 5 - Starting and Testing Redis
Start the systemd service by typing:
$systemctl start redis
Check that the service has no errors by running:
$systemctl status redis
This will produce output similar to the following:
● redis.service - Redis In-Memory Data Store
Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2018-05-29 17:49:11 UTC; 4s ago
. . .
To test that your service is functioning correctly, connect to the Redis server with the command-line client:
$redis-cli
In the prompt that follows, test connectivity by typing:
127.0.0.1:6379>ping
This will return:
PONG
Next, check that you can set keys by typing:
127.0.0.1:6379>set test "It's working!"
OK
Retrieve the test value by typing:
You should be able to retrieve the value you stored:
"It's working!"
After confirming that you can fetch the value, exit the Redis prompt to get back to the shell:
127.0.0.1:6379>exit
Finally, check whether Redis is able to persist data even after it’s been stopped or restarted. To do this, first restart the Redis instance:
$systemctl restart redis
Then connect with the client again and confirm that your test value is still available:
$$redis-cli
127.0.0.1:6379> get test
The value of your key should still be accessible:
"It's working!"
Exit out into the shell again when you are finished:
127.0.0.1:6379>exit
Assuming all of these tests worked and that you would like to start Redis automatically when your server boots, enable the systemd service:
$systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /etc/systemd/system/redis.service.
With that, your Redis installation is complete. If you have any doubts regarding this installation and configuration of Redis tutorial, leave a comment in the section below.
Conclusion
In this tutorial, you installed, compiled, and built Redis from its source code, configured it to run as a systemd service, and you validated that your Redis installation is functioning correctly
0 COMMENTS