Redis is an open-source, in-memory data structure store, used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, and geospatial indexes with radius queries. Its speed and versatility make it a popular choice for high-performance applications. This Tutorial will guide you through the process of installing and configuring Redis on your Ubuntu 24.04 server.
Before you begin, ensure you have a fresh installation of Ubuntu 24.04 and a user with `sudo` privileges. It’s always a good practice to update your system’s package list and upgrade existing packages to their latest versions. Open your terminal and run the following commands:
“`bash
sudo apt update
sudo apt upgrade -y
“`
Now, you can install Redis directly from the Ubuntu repositories. This is the simplest method and ensures that Redis is well-integrated with your system’s package management.
“`bash
sudo apt install redis-server -y
“`
Once the installation is complete, the Redis server should start automatically. You can verify its status using the `systemctl` command:
“`bash
sudo systemctl status redis-server
“`
You should see an output indicating that Redis is `active (running)`.
By default, Redis is configured to listen on `127.0.0.1` (localhost), which means it’s only accessible from the server itself. For most caching scenarios, this is sufficient and secure. However, if you need to access Redis from other machines, you’ll need to modify its configuration.
To configure Redis, open its configuration file using a text editor like `nano`:
“`bash
sudo nano /etc/redis/redis.conf
“`
To allow remote connections, find the line that starts with `bind 127.0.0.1 ::1` and comment it out by adding a `#` at the beginning. Alternatively, you can change it to `bind 0.0.0.0` to listen on all available network interfaces, but this is generally not recommended without proper firewall rules. A more secure approach is to bind to a specific private IP address or use a firewall to restrict access.
For demonstration, let’s assume you want to bind to a specific IP address, say `192.168.1.100`. You would change the line to:
“`
bind 192.168.1.100
“`
If you comment out `bind 127.0.0.1 ::1` and do not specify another `bind` address, Redis will listen on all interfaces. This is less secure.
Next, it’s highly recommended to set a strong password for Redis access. Find the line that starts with `# requirepass foobared` (it might be commented out). Uncomment it and replace `foobared` with your desired strong password:
“`
requirepass YourSuperStrongPasswordHere
“`
Save the file (Ctrl+O, Enter) and exit nano (Ctrl+X).
After making changes to the configuration file, you must restart the Redis service for the changes to take effect:
“`bash
sudo systemctl restart redis-server
“`
If you configured Redis to accept remote connections, you’ll need to open the Redis port (default 6379) in your firewall. Ubuntu uses UFW (Uncomplicated Firewall) by default.
“`bash
sudo ufw allow 6379/tcp
sudo ufw enable
sudo ufw status
“`
You can test your Redis installation by connecting to it using the `redis-cli` utility.
If you configured a password:
“`bash
redis-cli
AUTH YourSuperStrongPasswordHere
PING
“`
If successful, it should return `PONG`. You can also try setting and getting a key:
“`bash
SET mykey “Hello Redis”
GET mykey
“`
This should return `”Hello Redis”`.
If you did not configure a password (not recommended for production):
“`bash
redis-cli
PING
“`
This will also return `PONG`.
This tutorial has guided you through installing Redis on Ubuntu 24.04, verifying its status, and configuring Basic security settings like binding to an IP and setting a password. Redis is now ready to serve your applications, providing a robust and high-performance data store. Remember to secure your Redis instance, especially if it’s exposed to the network, by using strong passwords and appropriate firewall rules.
