How to install and configure Redis on AlmaLinux 9
This guide walks you through installing Redis 7.2.4 on AlmaLinux 9 Stream, configuring basic security settings, and verifying the cache is running correctly for your applications.
Install Redis to build a high-speed in-memory data store for your web applications. This tutorial covers installing Redis 7.2.4 on AlmaLinux 9 Stream, configuring the service, and securing the default port.
Prerequisites
- AlmaLinux 9 Stream access via SSH.
- Root privileges or sudo access.
- Internet connection to download the official Redis repository.
- At least 512MB of free RAM for a basic setup.
Step 1: Install the Redis Repository
AlmaLinux 9 does not include Redis in the default repositories, so you must first add the official Redis RPM repository. This ensures you get the latest stable version, specifically Redis 7.2.4, with security updates.
curl -fsSL https://get.redis.io/install-redis.sh | bash
After running the script, the repository is added to your system. You will see output confirming the installation of the repository package and the GPG key.
Step 2: Install Redis Server
Update your package manager cache to ensure you have the latest list of available packages, then install the Redis server. This command pulls the specific version required for your deployment.
dnf update -y
dnf install redis -y
Once the installation finishes, the redis-server and redis-trib packages are installed on your system. The default configuration file is located at /etc/redis.conf.
Step 3: Configure Redis Security
By default, Redis binds to localhost and listens on port 6379 without a password. For production environments, you must enable authentication. Open the configuration file using a text editor like vi or nano.
vi /etc/redis.conf
Find the line # requirepass foobared and remove the hash symbol to enable it. Change the password to a strong, random string of your choice. Also, ensure the bind directive restricts access to localhost unless you need remote access.
requirepass YourStrongPasswordHere
bind 127.0.0.1
Save the file and exit the editor. If you are using vi, type :wq to save and quit.
Step 4: Configure Memory Limits
Prevent Redis from consuming all available RAM by setting a maximum memory limit. Locate the # maxmemory line in the configuration file and uncomment it. Set the value to 50% of your total system memory.
maxmemory 512mb
maxmemory-policy allkeys-lru
The allkeys-lru policy evicts the least recently used keys when the memory limit is reached. This prevents the Redis server from crashing your host system due to memory exhaustion.
Step 5: Start and Enable the Redis Service
Start the Redis service immediately to begin caching data. Then, enable the service to start automatically on system boot. This ensures Redis is always available after a server restart.
systemctl start redis
systemctl enable redis
You will see a success message indicating the service is now active and enabled.
Verify the installation
Check the status of the Redis service to confirm it is running. You should see active (running) in the output. Then, connect to the Redis CLI using the redis-cli command and run a simple ping test.
systemctl status redis
redis-cli ping
The redis-cli ping command returns PONG. This confirms the server is accepting connections and processing commands correctly.
Troubleshooting
If the Redis service fails to start, check the logs for specific errors. A common issue is a syntax error in the configuration file. Run redis-server /etc/redis.conf --test-memory to validate the config without starting the daemon. If the file has errors, fix them before restarting the service.
Another frequent error is the authentication failure when connecting with redis-cli. If you see ERR invalid password, ensure you updated the requirepass line in /etc/redis.conf and restarted the service with systemctl restart redis.
Finally, if Redis is not listening on port 6379, verify the firewall rules. On AlmaLinux 9, use firewall-cmd to open the port if remote access is required. Add the port and reload the firewall configuration.
firewall-cmd --permanent --add-port=6379/tcp
firewall-cmd --reload
Ensure you always restart the service after changing configuration files to apply the new settings. If the service fails to start again, check the journal logs using journalctl -u redis -f to see real-time error messages.