How To Install and Secure Redis on Ubuntu 20.04?

Linux VPS

Introduction

Redis is an in-memory key-value store well-known for its adaptability, performance, and extensive language support. This guide will show you how to install, setup, and secure Redis on an Ubuntu 20.04 server.

Prerequisites

You will need access to an Ubuntu 20.04 server with a non-root user with sudo rights and a firewall configured with ufw. Follow our Initial Server Setup instructions for Ubuntu 20.04 to get started.

Step 1 - Installing and Configuring Redis

To begin, refresh your local apt package cache:

$ sudo apt update

Install the LAMP Server with the below command:

$ sudo apt-get install apache2 php libapache2-mod-php php-pdo php-jspn php-cli mysql-server -y

Then run the following command to install Redis:

$ sudo apt install redis-server -y

Install the Redis extensions of PHP and MySQL with the following command:

$ sudo apt-get install php-redis php-mysql -y

After installing both packages, start the Redis service and enable it to start at system reboot:

$ systemctl start redis-server
$ systemctl enable redis-server


Restart the Apache service to apply the configuration changes:

$ systemctl restart apache2

Then, using your choice text editor, open the Redis configuration file:

$ sudo nano /etc/redis/redis.conf

Find the supervised directive within the file, which lets you specify an init system to administer Redis as a service. Because Ubuntu uses the systemd init system, modify its value from no to systemd:

. . .

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 mode
#   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

. . .


 

When finished, save and close the file. If you used Nano to edit the file, use CTRL + X, Y, then ENTER.

Then, restart the Redis service to reflect the configuration file changes:

$ sudo systemctl restart redis.service

Connect to the server using redis-cli, Redis' command-line client, to ensure that Redis is working properly -

$ redis-cli

Test connectivity with the ping command in the prompt that follows:

127.0.0.1:6379> ping

Output:
PONG

This indicates that the server connection is operational. Next, verify that you can set keys by running:

127.0.0.1:6379> set test "It's working!"

Output:
OK

Exit the Redis prompt to return to the shell after checking that you can retrieve the value:

127.0.0.1:6379> exit

Step 2 - Configuring a Redis Password
You can set the Redis password directly in Redis' configuration file, /etc/redis/redis.conf.
Reopen the file in your chosen editor:

$ sudo nano /etc/redis/redis.conf

Scroll down to the SECURITY section and look for the following commented directive:

. . .
# requirepass foobared
. . .

Remove the # to uncomment it, and change foobared to a secure password:

. . .
requirepass your_redis_password
. . .

After you've set the password, save and close the file before restarting Redis:

$ sudo systemctl restart redis.service

To verify that the password is correct, launch the Redis client:

$ redis-cli

The following commands test whether the Redis password worked. Before authentication, the first command attempts to set a key to a value:

127.0.0.1:6379> set key1 10

Because you did not authenticate, this will not function, and Redis will return an error:

Output:
(error) NOAUTH Authentication required.

The following command uses the password supplied in the Redis configuration file to authenticate:

127.0.0.1:6379> auth your_redis_password

Output:
OK

Running the previous command again will succeed after that:

127.0.0.1:6379> set key1 10

Output:
OK

You can leave redis-cli after ensuring that you can perform commands in the Redis client after authenticating:

127.0.0.1:6379> quit;

Note: 

Conclusion

In this article, you installed and configured Redis and verified that your Redis installation is operational. If you want to Set Up Redis As A Cache For MySQL With PHP On Ubuntu 20.04, then after completing all above steps, please follow this article.