In this tutorial, we are going to explain in step-by-step detail how to install and secure Redis on Ubuntu 22.04.
Redis is a shortcut for a remote dictionary server and an in-memory data structure store. It is used as an option for a distributed in-memory key-value database with durability. Redis server is written in C language and supports a huge range of data types such as strings, hashes, lists, and many more.
Installing and securing Redis on Ubuntu 22.04 is a straightforward process that may take up to 15 minutes. Let’s get things working!
Table of Contents
Prerequisites
- A server with Ubuntu 22.04 as OS
- User privileges: root or non-root user with sudo privileges
Step 1. Update the System
We need to update the packages to their latest versions available after a fresh install of Ubuntu 22.04
sudo apt-get update -y && sudo apt-get upgrade -y
Step 2. Install Redis Server
To install the Redis server, execute the command below:
sudo apt-get install redis-server -y
Once installed, start and enable the redis service.
sudo systemctl enable redis-server.service && sudo systemctl start redis-server.service
Check if the service is up and running:
sudo systemctl status redis-server.service
You should receive the following output:
root@host:~# sudo systemctl status redis-server.service ● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2022-07-11 11:00:55 UTC; 4min 52s ago Docs: http://redis.io/documentation, man:redis-server(1) Main PID: 50163 (redis-server) Status: "Ready to accept connections" Tasks: 5 (limit: 4548) Memory: 2.6M CPU: 728ms CGroup: /system.slice/redis-server.service └─50163 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""> Jul 11 11:00:55 host.test.vps systemd[1]: Starting Advanced key-value store... Jul 11 11:00:55 host.test.vps systemd[1]: Started Advanced key-value store.
Step 3. Install Redis PHP extension
The Redis PHP extension provides client access to the Redis server. Installing the Redis PHP extension will allow PHP to communicate with the Redis server. We assume that you already have PHP installed on your server.
To install the Redis PHP extension execute the following command:
sudo apt-get install php-redis -y
To verify if the installation is successful, execute the command below:
php -m | grep redis
You should receive the following output:
root@host:~# php -m | grep redis redis
Step 4. Connect to Redis Server
To connect to the Redis server and check if the connection is ok use the command below:
redis-cli
You will notice that the cli will be different as described below:
root@host:~# redis-cli 127.0.0.1:6379
If you get the screen above about the localhost and port 6379, then you will be sure that Redis is working properly
Now to test the connectivity execute the ping command, and you should receive the PONG output as a result of a successful connection.
127.0.0.1:6379> ping PONG
Step 5. Secure the Redis Server
Redis Server security feature is not enabled by default, and we need to enable it manually. Redus authentication on the server works with a given password that can be set in two different ways. We will explain both scenarios, and you can use any of them in the future.
Option 1. To set a password in the redis configuration file, open the file /etc/redis/redis.conf and find the line that contains requirepass phrase. Uncomment it and set your password.
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility # layer on top of the new ACL system. The option effect will be just setting # the password for the default user. Clients will still authenticate using # AUTH as usually, or more explicitly with AUTH default # if they follow the new protocol: both will work. # requirepass YOURSTRONGPASSWORDHERE
Save the file, close it and restart the Redis service.
sudo systemctl restart redis-server.service
Option 2. To set the password via the Redis cli you will need to login to with redis-cli command as described in the step 4 and execute the following command:
CONFIG SET requirepass YOURSTRONGPASSWORDHERE
You should receive the OK message after a successful password set.
127.0.0.1:6379> CONFIG SET requirepass YOURSTRONGPASSWORDHERE OK 127.0.0.1:6379>
No matter which way you choose to set the password, after successful setting, you can test the authentication with the command below:
127.0.0.1:6379> ping (error) NOAUTH Authentication required. 127.0.0.1:6379> auth YOURSTRONGPASSWORDHERE OK 127.0.0.1:6379> ping PONG
In the example above, we tried to test the connection without authentication, which resulted in the (error) NOAUTH Authentication required.
After we authenticated with the auth YOURSTRONGPASSWORDHERE command pinging, the connection was successful.
Congratulations! You successfully installed, configured, and secured Redis on Ubuntu 22.04. Also, you learned how to manage the Redis service with a couple of commands.
If you think you will have difficulties with this setup, you can always contact our technical support, and they will do the rest for you. Do not hesitate to contact us anytime you want. We are available 24/7.
If you liked this post on how to install and secure Redis on Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below.