Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. It is designed to provide fast and flexible data access, making it an ideal solution for applications requiring quick data processing and retrieval. If you’re looking to enhance the performance and scalability of your Rocky Linux server, Redis can be an excellent choice. With its ability to handle large amounts of data, Redis can help you easily manage high-traffic applications. You can use Redis as a database, cache, or message broker to improve the speed and reliability of your application. Whether you’re developing a new application or upgrading an existing one, Redis is a great solution to help you get the job done.
Features of Redis:
- In-Memory Data Storage: Redis stores all data in memory, making it incredibly fast for read and write operations.
- Persistence: Redis can persist data to disk to ensure that data is not lost even in the event of a crash or shutdown.
- Pub/Sub Messaging System: Redis provides a publish/subscribe messaging system that allows real-time communication between applications.
- Data Structures: Redis supports a variety of data structures, including strings, hashes, lists, sets, and sorted sets.
- Lua Scripting: Redis provides a Lua scripting engine, allowing you to execute scripts on the server to perform complex operations.
- Clustering: Redis supports clustering, allowing you to scale your applications horizontally by distributing data across multiple Redis instances.
- Multi-Threading: Redis is designed to be multithreaded, making it highly efficient and scalable.
In this guide, you will discover how to effectively set up Redis on your Rocky Linux 9 server or workstation through two distinct methods – either via the appstream or Remi Redis pm – using the terminal command line. You’ll also receive step-by-step instructions on initiating the setup process and effectively using the terminal command line.
Table of Contents
Step 1: Update Rocky Linux
Before proceeding with the Redis installation, it is important to ensure your system’s packages are updated to prevent any issues during the installation process. You can accomplish this by executing the following command.
sudo dnf upgrade --refresh
Method 1: Install Redis with Appstream
The Appstream repository, as the default source, holds the stable version of Redis that the Rocky Linux team has thoroughly tested. This makes the installation process straightforward. To begin, just run the following command.
sudo dnf install redis
It’s important to note that by default, the installation of Redis does not activate the service or set it to start on system boot. To activate the service and ensure it launches every time the system starts, you can use the following command.
sudo systemctl enable redis --now
Method 2: Install Redis with Remi RPM
The Remi repository is the recommended option for those who prefer to have the latest version of Redis. Remi is well-known for maintaining the latest stable releases of PHP and provides the latest stable releases of Redis. By using Remi, you can ensure that your Redis installation is up-to-date and fully functional.
Before proceeding with the installation of Redis, it is necessary first to install the EPEL (Extra Packages for Enterprise Linux) repository. For those new to Rocky Linux and other distributions based on RHEL, the EPEL repository is valuable as it provides access to the most commonly used software packages in Enterprise Linux.
After importing the EPEL repository for your specific version of Rocky Linux, the next step is importing the Remi RPM. This will provide access to the latest stable releases of Redis and other software packages maintained by Remi. Importing the Remi RPM will allow you to manage and install these packages through the DNF package manager.
Import EPEL and Remi RPM for Rocky Linux 9
To begin, enabling the CRB (Code Ready Builder) with the following command is necessary.
sudo dnf config-manager --set-enabled crb
The next step in the process is to install EPEL 9 using the DNF terminal command.
sudo dnf install \ https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm \ https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm
With the EPEL repository now added, the next step is to enable the Remi repository using the following terminal command.
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
Import EPEL and Remi RPM for Rocky Linux 8
For those using the Rocky Linux 8 distribution, installing the EPEL repository is similar to that of Rocky Linux 9. Simply use the following DNF terminal command to install EPEL on your Rocky Linux 8 system.
sudo dnf install \ https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm \ https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-8.noarch.rpm
Having successfully added the EPEL 8 repository, the next step is to enable the Remi repository. This is achieved by executing the following terminal command.
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
Now, list the versions available with the following command.
sudo dnf module list redis
When installing Redis, it’s important to note that multiple versions of the software are available, including versions 5, 6, 6.2, and 7.
Enable Redis 7.0:
sudo dnf module enable redis:remi-7.0 -y
Enable Redis 6.2:
sudo dnf module enable redis:remi-6.2 -y
Enable Redis 5.0:
sudo dnf module enable redis:remi-5.0 -y
When installing Redis, it’s important to note that the process for installation remains the same, regardless of the version you choose. This means that you can easily switch to a different version if you encounter any issues with your installation. For example, if you have installed Redis 7 and find it too buggy, you can remove it and switch to a different version, such as 6.2.
sudo dnf install redis
To verify the successful installation of Redis, use the following terminal command. This will provide you with information about the version of Redis installed.
Redis server v=7.0.8 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=85aed731497b4b06
Redis is not automatically started or set to run on system boot by default. To activate the service and ensure it is enabled on the system boot, you will need to run the following command. This step is necessary to ensure that Redis is up and running every time your system is restarted.
sudo systemctl enable redis --now
After activation, it’s important to verify the status of Redis to ensure it is running properly and without any errors. You can check the current status and run any necessary diagnostics to confirm its proper operation. This step is crucial for ensuring that Redis is functioning correctly and providing reliable service.
systemctl status redis
It’s important to note that Redis actively listens on localhost on the default port 6379. To verify this, you can use the following terminal command: “ps -ef | grep redis.” This command will show you the current status of Redis and help you confirm that it is indeed listening on the correct port. This is important in ensuring that Redis is set up correctly and ready to use.
ps -ef | grep redis
redis 3845 1 0 12:15 ? 00:00:00 /usr/bin/redis-server 127.0.0.1:6379 joshua 3966 3929 0 12:16 pts/0 00:00:00 grep --color=auto redis
Working Redis CLI
Redis CLI (Command Line Interface) is a powerful tool that provides an interface for interacting with Redis databases. After installing Redis, you can use Redis CLI to interact with the database and perform various tasks such as setting and getting values, deleting keys, and much more.
Here are a few examples of how to use Redis CLI:
To connect to Redis, open a terminal window and enter the following command:
Set a Value Once you are connected to Redis, you can set a value for a key using the following syntax:
SET key value
For example, to set the key “message” value to “Hello World,” you would enter.
SET message "Hello World"
Get a Value You can retrieve the value of a key by using the following syntax.
For example, to retrieve the value of the key “message,” you would enter.
Delete a Key To delete a key, use the following syntax.
For example, to delete the key “message,” you would enter.
List All Keys To list all keys in the Redis database, use the following syntax.
These are just a few of the many commands available in Redis CLI. The Redis CLI is a powerful tool that provides various capabilities for interacting with Redis databases. To learn more about the different commands available, you can refer to the Redis documentation.
How to Configure Redis
Configuring Redis after installation involves setting some key parameters to ensure the proper operation and security of the service. This section will teach you how to configure Redis max memory, network access, password, and cache eviction policies.
First, you need to open the “/etc/redis/redis.conf” file using the nano editor.
sudo nano /etc/redis.conf
Configure Max Memory
The max memory setting determines the amount of memory Redis can use to store data. This setting is important for two reasons:
- To prevent Redis from using too much memory and slowing down the system.
- To ensure Redis does not store too much data in memory and start writing to disk, which slows down performance.
To configure max memory, in the Redis configuration file “/etc/redis.conf,” find and set the maxmemory parameter, as shown below:
# set maxmemory to 512 MB maxmemory 512mb
Configure Network Access
By default, Redis listens only on the loopback address (localhost) and is not accessible from other systems. To configure Redis to listen on a specific IP address, you can modify the bind parameter in the “/etc/redis.conf” configuration file.
# bind Redis to the specific IP address bind 192.168.1.100
Instead, you can allow connections from any IP address to Redis by configuring the bind parameter, but this is not recommended as it lacks security measures. It is best to implement specific firewalls before proceeding with this method.
To secure Redis, you can configure a password that must be provided by clients connecting to the server. To configure a password, you must set the “requirepass” parameter in the “/etc/redis.conf” configuration file.
# set Redis password requirepass password
If an unsuccessful login attempt occurs, Redis users will receive the following error message.
(error) NOAUTH Authentication required.
Upon successful login, the following message will be displayed to the user.
Configure Redis as a Cache
Redis can be used as a cache to store frequently accessed data in memory for faster retrieval. To configure Redis as a cache, you must choose an eviction policy that determines how Redis removes old data to make room for new data. There are two main policies:
- LRU (Least Recently Used): removes the oldest data that has not been accessed recently.
- LFU (Least Frequently Used): removes the data that has been accessed the least number of times.
To configure an eviction policy, you must set the “maxmemory-policy” parameter in the “/etc/redis.conf” configuration file.
# set Redis eviction policy to LRU maxmemory-policy LRU
Save the file and restart the Redis server using the following command.
sudo systemctl restart redis-server
These are some basic configurations you can perform on Redis to ensure its proper operation and security. For more advanced configurations and features, refer to the official Redis documentation.
Configure Firewalld for Redis
FirewallD is a firewall management tool used to manage incoming and outgoing network connections on a Linux system. If you have installed Redis on a Linux system, it is important to configure the firewall to ensure it is properly protected. In this section, we will review some popular methods for configuring FirewallD for Redis.
Allow Redis Zone
The simplest way to allow Redis connections through the firewall is to assign the Redis service to a firewall zone. This is done by adding the Redis service to the desired zone in the firewall-cmd configuration file.
firewall-cmd --permanent --zone=public --add-service=redis
Allow Dedicated IP Address
If you want to allow connections only from a specific IP address, you can add a rule to the firewall that allows incoming connections only from that address.
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="6379" accept'
Allow IP Range
You can also allow incoming connections from a range of IP addresses. To do this, you need to specify the IP range using the source address option in the firewall-cmd command.
firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="6379" accept'
Another way to allow incoming connections to Redis is to allow the port that Redis uses for incoming connections. The default port for Redis is 6379.
firewall-cmd --permanent --zone=public --add-port=6379/tcp
In conclusion, installing Redis on Rocky Linux is relatively straightforward but requires careful attention to detail to ensure a smooth and secure installation. Using the recommended steps and procedures discussed in this article, you can have Redis up and running in no time. It is important to remember that while Redis is a powerful tool, it should always be used with care and in accordance with best security practices. With this in mind, you can use Redis to unleash its full potential, enhance the performance of your applications, and enjoy its many benefits.
To find more information about managing your Redis installation, visit the Redis documentation page.
Frequently Asked Questions
What is Redis, and why is it important?
Redis is an in-memory data structure store used as a database, cache, and message broker. It is important because it provides high performance and flexibility, making it an ideal choice for modern web applications and real-time systems.
What is the benefit of installing Redis from Remi’s RPM on Rocky Linux?
Installing Redis from Remi’s RPM on Rocky Linux provides several benefits, including access to the latest stable version of Redis, compatibility with PHP and other Remi-maintained software packages, and ease of installation and maintenance.
What benefit is there to swapping from Memcached to Redis on Rocky Linux?
Swapping from Memcached to Redis on Rocky Linux can offer several benefits, including increased speed, improved data structures, and greater flexibility in terms of data storage. Additionally, Redis supports persistence, allowing data to persist even after a reboot or crash, while Memcached does not. This makes Redis a better choice for use cases that require high performance, reliability, and data persistence.
Is Redis secure?
Redis has security features such as password authentication, but it should not be considered entirely secure without proper configuration and maintenance. It’s important to secure your Redis installation, such as configuring firewalls, monitoring the logs, and following best practices for secure data storage.
How do I configure Redis on Rocky Linux?
Configuring Redis on Rocky Linux involves several steps, including enabling the Remi repository, installing Redis, configuring the Redis max memory, network access, and password, and choosing between LRU and LFU eviction policies. It is important to carefully follow the instructions and best practices to ensure that your Redis installation is configured correctly and securely.