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
Step 2. Install Redis Server
To install the Redis server, execute the command below:
Once installed, start and enable the redis service.
Check if the service is up and running:
You should receive the following output:
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:
To verify if the installation is successful, execute the command below:
You should receive the following output:
Step 4. Connect to Redis Server
To connect to the Redis server and check if the connection is ok use the command below:
You will notice that the cli will be different as described below:
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.
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.
Save the file, close it and restart the Redis 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:
You should receive the OK message after a successful password set.
No matter which way you choose to set the password, after successful setting, you can test the authentication with the command below:
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.