How to Install and Configure Redis on Linux Servers
What is Redis? Redis (Remote Dictionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. Known for its high performance, Redis supports data structures like strings, hashes, lists, sets, and more. Itโs often used for applications re
What is Redis?
Redis (Remote Dictionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. Known for its high performance, Redis supports data structures like strings, hashes, lists, sets, and more. Itโs often used for applications requiring fast data retrieval, caching, real-time analytics, and session management.
Prerequisites
Before installing Redis, ensure that:
- You have a Linux server โ Redis can be installed on distributions like Ubuntu, Debian, CentOS, and RHEL.
- Your server is updated โ Run system updates to ensure all packages are current.
- Basic command-line knowledge โ Familiarity with Linux commands will be helpful.
Step 1: Update the Package Repository
Update your systemโs package list to make sure you have access to the latest version of Redis:
For Ubuntu/Debian
sudo apt update
For CentOS/RHEL
sudo yum update
Step 2: Install Redis
Redis is available through the default repositories on most Linux distributions, so you can install it directly using your package manager.
On Ubuntu/Debian:
sudo apt install redis-server -y
On CentOS/RHEL:
- Install EPEL Repository (if not already installed):
sudo yum install epel-release -y
2. Install Redis:
sudo yum install redis -y
Step 3: Configure Redis
The main configuration file for Redis is usually located at /etc/redis/redis.conf on Debian-based systems or /etc/redis.conf on Red Hat-based systems. Here are some common configurations you may want to adjust:
- Enable Persistence: Redis stores data in memory, but you can enable data persistence to save it to disk.
- Open the configuration file:
sudo vi /etc/redis/redis.conf
- Locate the
appendonlysetting and set it toyes:
appendonly yes
2. Set up a Password: For security, set a password to protect your Redis instance.
- In the
redis.conffile, locaterequirepassand set a strong password:
requirepass YourStrongPassword
3. Configure Network Binding: By default, Redis binds to localhost. To allow access from other hosts, modify the bind setting in the configuration file:
bind 0.0.0.0
- Note: Enabling remote access requires strong security measures, such as firewalls and passwords.
4. Set a Memory Limit: Redis allows you to define a maximum memory usage, which can be helpful if youโre using it as a cache
- Set the memory limit by adding or updating the following lines in the configuration file:
maxmemory 256mbmaxmemory-policy allkeys-lru
5. Define Save Intervals: Redis can periodically save data to disk based on specific intervals.
- You can set save intervals in the configuration file like this:
save 900 1save 300 10save 60 10000
Step 4: Start and Enable Redis
Once configured, start the Redis service and enable it to start at boot:
sudo systemctl start redis
sudo systemctl enable redis
To verify that Redis is running:
sudo systemctl status redis
Step 5: Testing Redis
- Connect to Redis CLI: Use the Redis CLI to connect to the Redis server.
redis-cli
2. Authenticate (if you set a password):
AUTH YourStrongPassword
3. Ping the Server: Test the connection by running:
ping
Step 6: Enable Redis for External Access (Optional)
If you plan to access Redis from outside the server (e.g., for use with a web application hosted elsewhere), ensure the Redis port (6379) is open in your firewall:
# For UFW (on Ubuntu)
sudo ufw allow 6379
# For firewalld (on CentOS/RHEL)
sudo firewall-cmd --permanent --add-port=6379/tcp
sudo firewall-cmd --reload
Warning: Allowing external access to Redis without proper security settings is risky. Always use strong passwords, configure firewalls, or set up a VPN for secure access.
Step 7: Verify the Installation
You can access Redis from your local or remote setup by running:
redis-cli -h -p 6379
Final Word
Redis is now installed and configured on your Linux server. Youโve set up basic security, enabled persistence, and adjusted network and memory settings for optimal performance.