GICT knowledge base
How to Install and Configure Redis on Linux
Published
Redis is an open-source, in-memory key-value store renowned for its speed and versatility. Commonly used as a cache, message broker, and database, Redis supports a variety of data structures and is a core component of many modern web applications. Installing and configuring Redis on a Linux system enables you to accelerate application performance, manage ephemeral data efficiently, and implement advanced data processing scenarios.
This comprehensive guide walks you through the process of installing Redis on Linux, configuring it for optimal performance and security, and performing basic management tasks. You will learn how to tailor Redis to your needs, secure your deployment, and integrate it into your application stack.
Prerequisites
- Access to a Linux system (Ubuntu, Debian, CentOS, or similar)
- User account with sudo or root privileges
- Familiarity with the terminal and basic command-line tools
- Stable internet connection for downloading packages
Introduction to Redis
Redis (Remote Dictionary Server) is a lightweight, high-performance data store that excels in speed, simplicity, and flexibility. It serves as a cache, database, and message broker, supporting data structures such as strings, hashes, lists, sets, and sorted sets. Redis operates in-memory, making it ideal for low-latency operations, session management, and real-time analytics.
Step 1: Update Your System
Before installing Redis, update the package index and upgrade existing packages:
Ubuntu/Debian:
$ sudo apt update $ sudo apt upgrade
CentOS/RHEL:
$ sudo yum update
Step 2: Install Redis
On Ubuntu/Debian
Install Redis from Repositories:
$ sudo apt install redis-server
Verify Installation:
$ redis-server --version
On CentOS/RHEL
Enable EPEL Repository:
$ sudo yum install epel-release
Install Redis:
$ sudo yum install redis
Verify Installation:
$ redis-server --version
Build from Source (Optional, for Latest Version)
Install build dependencies:
$ sudo apt install build-essential tcl
Download and compile Redis:
$ wget http://download.redis.io/redis-stable.tar.gz $ tar xzf redis-stable.tar.gz $ cd redis-stable $ make $ sudo make install
(Optional) Run tests:
$ make test
Step 3: Start and Enable Redis
After installation, start the Redis service and ensure it launches on boot.
Ubuntu/Debian:
$ sudo systemctl start redis-server $ sudo systemctl enable redis-server
CentOS/RHEL:
$ sudo systemctl start redis $ sudo systemctl enable redis
Check Redis status:
$ sudo systemctl status redis
Step 4: Basic Redis Usage and Testing
Confirm that Redis is running and responsive.
Connect to the Redis CLI:
$ redis-cli
Run test commands:
127.0.0.1:6379> ping PONG 127.0.0.1:6379> set testkey "Hello, Redis!" OK 127.0.0.1:6379> get testkey "Hello, Redis!" 127.0.0.1:6379> exit
Step 5: Configure Redis
Redis’ main configuration file is located at /etc/redis/redis.conf (Ubuntu/Debian) or /etc/redis.conf (CentOS/RHEL).
To change configuration, edit this file using a text editor:
$ sudo nano /etc/redis/redis.conf
Common Configuration Options
- Change Bind Address
By default, Redis only listens on 127.0.0.1 (localhost) for security. To allow external access, change the bind directive:bind 127.0.0.1 192.168.1.100
Warning: Exposing Redis to the network can be a security risk. Always enable authentication or use a firewall.
- Set a Password
Enable password protection by editing the requirepass directive:requirepass your_strong_password
After restarting Redis, you must authenticate before issuing commands:
$ redis-cli 127.0.0.1:6379> auth your_strong_password OK - Set a Persistent Storage Policy
Redis supports two persistence mechanisms: RDB snapshots and AOF logs.
RDB (default): Periodic point-in-time snapshots.
AOF: Logs every write operation for complete durability.
To enable AOF, set:appendonly yes
You can fine-tune snapshot intervals (save), AOF rewrite policies, and more.
- Adjust Memory Usage
Control memory limits to prevent Redis from consuming too much RAM:maxmemory 256mb maxmemory-policy allkeys-lruThis configuration limits Redis to 256MB and evicts least-recently-used keys when full.
- Change the Default Port
By default, Redis listens on port 6379. To change:port 6380
Step 6: Restart Redis to Apply Changes
After editing the configuration file, restart the Redis service:
Ubuntu/Debian:
$ sudo systemctl restart redis-server
CentOS/RHEL:
$ sudo systemctl restart redis
Step 7: Secure Redis
Redis does not enable authentication or encryption by default. To avoid accidental data leaks or unauthorized access:
- Restrict Access with a Firewall
Open only necessary ports and block public access: - Using UFW (Ubuntu):
$ sudo ufw allow from 192.168.1.0/24 to any port 6379 $ sudo ufw deny 6379 - Using firewalld (CentOS):
$ sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="6379" accept' $ sudo firewall-cmd --permanent --remove-port=6379/tcp $ sudo firewall-cmd --reload - Enable Authentication
As described above, set the requirepass directive in your configuration file. - Run Redis as a Non-Root User
By default, Redis runs as the redis user for security. Do not run Redis as root. - Disable Dangerous Commands
You can rename or disable commands such as FLUSHALL, DEBUG, and CONFIG by editing redis.conf:rename-command FLUSHALL ""
Step 8: Enable Redis Persistence and Backups
By default, Redis saves snapshots to /var/lib/redis/dump.rdb. You can adjust snapshot intervals with the save directive:
save 900 1 save 300 10 save 60 10000
This means:
- Save if at least 1 change in 900 seconds
- Save if at least 10 changes in 300 seconds
- Save if at least 10,000 changes in 60 seconds
To back up, simply copy the dump file:
$ sudo cp /var/lib/redis/dump.rdb /backup/location/
For AOF logs, back up appendonly.aof.
Step 9: Monitor Redis
Monitor resource usage, connections, and performance metrics with:
$ redis-cli info
For real-time monitoring, use:
$ redis-cli monitor
You can also integrate Redis with external monitoring tools like Prometheus, Nagios, or Zabbix.
Step 10: Integrate Redis With Applications
Most programming languages have Redis client libraries (e.g., redis-py for Python, redis npm package for Node.js, phpredis for PHP).
Example (Python):
import redis
r = redis.Redis(host='localhost', port=6379, password='your_strong_password')
r.set('mykey', 'value')
print(r.get('mykey'))
Example (Node.js):
const redis = require('redis');
const client = redis.createClient({ password: 'your_strong_password' });
client.set('mykey', 'value');
client.get('mykey', (err, reply) => {
console.log(reply);
});
Step 11: Advanced Configuration (Optional)
- Set Up Redis as a Systemd Service
Redis is usually installed as a service by default, but if you build from source, you may want to create a systemd unit file for better process management. - Configure Redis Sentinel for High Availability
Redis Sentinel provides automatic failover and monitoring for distributed Redis deployments. - Set Up Redis Cluster for Horizontal Scaling
For advanced use cases, Redis Cluster enables sharding and large-scale distributed setups.
Best Practices
- Always bind Redis to a private IP and enable firewall rules.
- Enable authentication and avoid running Redis with default settings in production.
- Regularly back up your data files.
- Monitor memory usage and configure maxmemory appropriately.
- Use the latest stable version of Redis for security and performance benefits.
- Document your configuration and any changes for future reference.
Conclusion
Installing and configuring Redis on Linux provides a powerful, flexible, and fast data store for a wide range of applications. By following this guide, you can deploy Redis securely, optimize its performance, and ensure reliability in your environment. Regularly review your configuration, update as needed, and integrate Redis with monitoring and backup solutions to maximize its value.
[!NOTE] For more advanced setups, refer to the official Redis documentation and your Linux distribution.
Related guides
How to Set Up zram on a Small VPS
Configure zram on Ubuntu with kernel and swap checks. Understand compressed memory limits, monitor real use and avoid unsafe swap-file changes.
LinuxHow to Find Services That Need a Restart After Updates with needrestart
Find services using outdated libraries with needrestart on Ubuntu. Read restart reports, review automatic restart policy and plan maintenance safely.
LinuxHow to Limit a Linux Service’s Memory with systemd
Limit a Linux service with systemd MemoryHigh, MemoryMax and swap controls. Check cgroup support, diagnose pressure and remove only your own policy.
LinuxHow to Protect SSH with CrowdSec on Ubuntu
Protect SSH with CrowdSec on Ubuntu. Connect log detection to firewall enforcement and test temporary blocking without losing administrator access.
