Redis is an in-memory data structure store that serves as a database, cache, and message broker. It supports various data structures like strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, and geospatial indexes. Redis is known for its performance, flexibility, and simplicity, making it a popular choice for developers and system administrators.
This guide shows you how to install and configure Redis on an Ubuntu 24.04 server.
Prerequisites
To get started, ensure you have the following:
- An Ubuntu 24.04 server. We recommend a Digital Ocean VPS server.
- A non-root user with sudo privileges. Read our guide on How to Create a Non-Root Sudo User on Ubuntu 24.04
Install Redis
This section shows you how to install Redis on Ubuntu 24.04. The Redis package is available in the default Ubuntu repository, making it straightforward to install using the apt
package manager.
-
Update the package index.
CONSOLE$ sudo apt update
-
Install the Redis package.
CONSOLE$ sudo apt install redis-server
-
Verify the installation.
CONSOLE$ redis-server --version
Manage the Redis Service
Redis runs under redis
. In this section, you will manage the service using systemctl
, the system and service manager for Ubuntu.
-
Start the Redis service.
CONSOLE$ sudo systemctl start redis
-
Enable Redis to start on boot.
CONSOLE$ sudo systemctl enable redis
-
Check the status of the Redis service.
CONSOLE$ sudo systemctl status redis
Secure the Redis Server
By default, Redis does not require a password, which can pose security risks. This section explains how to secure your Redis server by setting a password.
-
Open the Redis configuration file in a text editor.
CONSOLE$ sudo nano /etc/redis/redis.conf
-
Find the
requirepass
directive.INI# requirepass your_secure_password
-
Uncomment the
requirepass
directive by removing the#
at the beginning. Then, set a strong password.INIrequirepass your_secure_password
-
Save the changes and close the editor.
-
Restart the Redis service to apply the changes.
CONSOLE$ sudo systemctl restart redis
Understand Redis Directory Structure
The following details show the Redis directory structure, including configuration, error, and log files.
- Redis configuration file:
/etc/redis/redis.conf
. A file that contains all the settings for the Redis server. - Redis data directory:
/var/lib/redis
. A directory that stores the data files used by Redis. - Redis log file:
/var/log/redis/redis-server.log
. A file that stores stores logs generated by the Redis server.
Log In to Redis and Perform Basic Operations
Test the Redis server by seting a key and retrieve the key's value to ensure Redis works as expected.
-
Log in to the Redis server using the
redis-cli
command.CONSOLE$ redis-cli
-
Authenticate with the password you set earlier.
CONSOLEauth your_secure_password
-
Set a sample
test
key in Redis.CONSOLEset test "Hello, Redis!"
-
Retrieve the value of the
test
key you set.CONSOLEget test
Conclusion
You have successfully installed and configured Redis on your Ubuntu 24.04 server. You have learned how to manage the Redis service, secure it with a password, understand its directory structure, and perform basic operations like setting and retrieving keys. Redis is a powerful tool that can enhance your application's performance and scalability.