Redis Server Installation on Linux

Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. This tutorial covers installing and configuring Redis on Ubuntu and RHEL-based systems.

Prerequisites

  • Ubuntu 20.04/22.04 LTS or RHEL/Rocky Linux 8/9
  • Root or sudo privileges
  • Minimum 1GB RAM (2GB+ recommended for production)
  • At least 2GB of free disk space
  • Basic knowledge of Linux command line
Ubuntu/Debian
RHEL/CentOS

1 Install Redis

Update your package list and install Redis:

sudo apt update
sudo apt install redis-server -y

2 Configure Redis

Edit the Redis configuration file:

sudo nano /etc/redis/redis.conf

Make the following changes for basic configuration:

# Bind to all interfaces (or specific IPs)
bind 0.0.0.0

# Set a strong password (optional but recommended)
requirepass your_strong_password_here

# Enable persistence
appendonly yes
appendfilename "appendonly.aof"

# Set max memory policy
maxmemory 256mb
maxmemory-policy allkeys-lru

3 Start and Enable Redis Service

Start the Redis service and enable it to start on boot:

sudo systemctl start redis-server
sudo systemctl enable redis-server

Check the service status:

sudo systemctl status redis-server

4 Test Redis Installation

Connect to Redis using the command-line interface:

redis-cli

Test basic operations:

# If you set a password, authenticate first
AUTH your_strong_password_here

# Set a key-value pair
SET test_key "Hello Redis"

# Get the value
GET test_key

# Check server information
INFO server

# Exit Redis CLI
EXIT

5 Configure Firewall

Allow Redis through the firewall (default port 6379):

sudo ufw allow 6379/tcp
sudo ufw enable
sudo ufw status
Security Note: Only open port 6379 to trusted IP addresses in production environments. Consider using VPN or SSH tunneling for remote access.

6 Configure Redis as a Service

Edit the Redis systemd service file for better control:

sudo nano /etc/systemd/system/redis.service

Add the following content if the file doesn't exist:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target

Reload systemd and restart Redis:

sudo systemctl daemon-reload
sudo systemctl restart redis-server

7 Enable Redis Persistence

Redis supports two persistence methods. Edit the config file:

sudo nano /etc/redis/redis.conf

Configure RDB snapshots:

# Save the dataset to disk every 60 seconds if at least 1000 keys changed
save 60 1000

# Compression
rdbcompression yes

# RDB filename
dbfilename dump.rdb

# Directory for RDB and AOF files
dir /var/lib/redis

Configure AOF (Append Only File) for better durability:

# Enable AOF
appendonly yes

# AOF filename
appendfilename "appendonly.aof"

# fsync policy
appendfsync everysec

# Auto-rewrite AOF when it grows too large
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

8 Redis Security Configuration

Enhance Redis security with these measures:

sudo nano /etc/redis/redis.conf

Add these security settings:

# Rename dangerous commands
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command CONFIG ""

# Enable protected mode
protected-mode yes

# Use a non-default port
port 6380

# Limit client connections
maxclients 10000

# Set timeout for idle clients
timeout 300

Restart Redis to apply changes:

sudo systemctl restart redis-server

9 Test Redis Persistence

Test that Redis data persistence is working correctly:

redis-cli

# Authenticate if password is set
AUTH your_strong_password_here

# Set some test data
SET persistent:test "This should survive restart"

# Force save to disk
SAVE

# Check if data was saved
LASTSAVE

# Exit and restart Redis
EXIT
sudo systemctl restart redis-server

# Reconnect and check if data persists
redis-cli
GET persistent:test

10 Monitor Redis Performance

Use Redis CLI to monitor performance and statistics:

redis-cli

# Get overall statistics
INFO

# Get memory statistics
INFO memory

# Get persistence statistics
INFO persistence

# Get client statistics
INFO clients

# Monitor commands in real-time
MONITOR

# Show slow logs (commands taking longer than 10ms)
SLOWLOG GET 10

Install redis-tools for additional utilities:

sudo apt install redis-tools -y

1 Install Redis on RHEL-based Systems

Enable EPEL repository and install Redis:

# For RHEL 8/9, Rocky Linux, AlmaLinux
sudo dnf install epel-release -y
sudo dnf install redis -y

# For CentOS 7
sudo yum install epel-release -y
sudo yum install redis -y

2 Configure Redis

Edit the Redis configuration file:

sudo nano /etc/redis.conf

Make the following changes for basic configuration:

# Bind to all interfaces (or specific IPs)
bind 0.0.0.0

# Set a strong password (optional but recommended)
requirepass your_strong_password_here

# Enable persistence
appendonly yes
appendfilename "appendonly.aof"

# Set max memory policy
maxmemory 256mb
maxmemory-policy allkeys-lru

# Change daemonize to no (for systemd)
daemonize no

3 Start and Enable Redis Service

Start the Redis service and enable it to start on boot:

sudo systemctl start redis
sudo systemctl enable redis

Check the service status:

sudo systemctl status redis

4 Configure SELinux for Redis

If SELinux is enabled, configure it for Redis:

# Check SELinux status
sestatus

# If enforcing, adjust SELinux policies
sudo semanage port -a -t redis_port_t -p tcp 6379

# Or set permissive mode for Redis (not recommended for production)
sudo setsebool -P redis_can_network_connect 1

5 Configure Firewall

Allow Redis through the firewall:

sudo firewall-cmd --permanent --add-port=6379/tcp
sudo firewall-cmd --reload
Security Note: For production environments, consider restricting access to specific IP addresses only.

6 Test Redis Installation

Connect to Redis using the command-line interface:

redis-cli

Test basic operations:

# If you set a password, authenticate first
AUTH your_strong_password_here

# Set a key-value pair
SET test_key "Hello Redis"

# Get the value
GET test_key

# Check server information
INFO server

# Exit Redis CLI
EXIT

7 Configure Redis Persistence

Edit the Redis configuration file for persistence:

sudo nano /etc/redis.conf

Configure RDB snapshots:

# Save the dataset to disk every 60 seconds if at least 1000 keys changed
save 60 1000

# Compression
rdbcompression yes

# RDB filename
dbfilename dump.rdb

# Directory for RDB and AOF files
dir /var/lib/redis

Configure AOF (Append Only File):

# Enable AOF
appendonly yes

# AOF filename
appendfilename "appendonly.aof"

# fsync policy
appendfsync everysec

# Auto-rewrite AOF when it grows too large
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

8 Redis Security Configuration

Enhance Redis security with these measures:

sudo nano /etc/redis.conf

Add these security settings:

# Rename dangerous commands
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command CONFIG ""

# Enable protected mode
protected-mode yes

# Use a non-default port
port 6380

# Limit client connections
maxclients 10000

# Set timeout for idle clients
timeout 300

Restart Redis to apply changes:

sudo systemctl restart redis

9 Configure Redis Logging

Configure Redis logging for better monitoring:

sudo nano /etc/redis.conf

Update logging configuration:

# Set log level (debug, verbose, notice, warning)
loglevel notice

# Log file
logfile /var/log/redis/redis.log

# Syslog enabled
syslog-enabled no

# Syslog ident
syslog-ident redis

# Syslog facility
syslog-facility local0

# Set the number of databases
databases 16

Create log directory and set permissions:

sudo mkdir -p /var/log/redis
sudo chown redis:redis /var/log/redis
sudo systemctl restart redis

10 Redis Cluster Setup (Optional)

For production environments, consider setting up Redis Cluster:

# Install Ruby and redis gem for redis-trib
sudo dnf install ruby -y
sudo gem install redis

# Create cluster configuration directories
sudo mkdir -p /etc/redis/cluster
sudo mkdir -p /var/lib/redis/cluster

# Create multiple Redis instances for cluster
for port in {7000..7005}
do
  sudo mkdir -p /var/lib/redis/cluster/${port}
  sudo cp /etc/redis.conf /etc/redis/cluster/${port}.conf
done

Configure each cluster instance:

# Edit each cluster node configuration
sudo nano /etc/redis/cluster/7000.conf

Add cluster-specific settings:

port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
pidfile /var/run/redis_7000.pid
logfile /var/log/redis/redis_7000.log
dir /var/lib/redis/cluster/7000
Note: Redis Cluster requires at least 3 master nodes for a production environment. For development, you can set up a cluster on a single machine with different ports.
Command copied to clipboard!