Using Redis

Redis Installation Guide for Linux Server

Prerequisites

Before installing Redis, ensure your Linux server is up to date:

sudo apt update
sudo apt upgrade -y

Ubuntu/Debian

sudo apt install redis-server -y

CentOS/RHEL/Fedora

sudo yum install redis -y
# or for newer versions
sudo dnf install redis -y

Configuration

Basic Configuration

The main Redis configuration file is typically located at /etc/redis/redis.conf.

# Open configuration file
sudo nano /etc/redis/redis.conf

Key configuration settings to consider:

# Bind to specific IP (use 127.0.0.1 for local only)
bind 127.0.0.1

# Set a password for Redis
requirepass your_strong_password_here

# Set maximum memory limit
maxmemory 256mb
maxmemory-policy allkeys-lru

# Enable persistence (RDB snapshots)
save 900 1
save 300 10
save 60 10000

# Append-only file persistence
appendonly yes
appendfilename "appendonly.aof"

Configure Redis as a Service

If you installed from source, create a systemd service file:

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

Add the following content:

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

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

[Install]
WantedBy=multi-user.target

Create Redis user and directories:

sudo adduser --system --group --no-create-home redis
sudo mkdir -p /var/lib/redis
sudo chown redis:redis /var/lib/redis
sudo chmod 770 /var/lib/redis

Managing Redis Service

Start Redis

sudo systemctl start redis
# or for older systems
sudo service redis start

Enable Redis to start on boot

sudo systemctl enable redis

Check Redis status

sudo systemctl status redis

Stop Redis

sudo systemctl stop redis

Restart Redis

sudo systemctl restart redis

Verify Installation

Test if Redis is working properly:

# Connect to Redis CLI
redis-cli

# If you set a password
redis-cli -a your_password

# Test with ping command
127.0.0.1:6379> ping
# Should return: PONG

# Set and get a test value
127.0.0.1:6379> set test "Hello Redis"
127.0.0.1:6379> get test
# Should return: "Hello Redis"

# Exit CLI
127.0.0.1:6379> exit

Security Recommendations

  1. Set a strong password in the configuration file using requirepass

  2. Bind to localhost if Redis is only used locally:

    bind 127.0.0.1
  3. Configure firewall to restrict access:

    # Allow only specific IP
    sudo ufw allow from trusted_ip to any port 6379
  4. Disable dangerous commands in production:

    rename-command FLUSHDB ""
    rename-command FLUSHALL ""
    rename-command CONFIG ""
  5. Run Redis as non-root user (handled automatically when installed via package manager)

Laravel Integration

If you're using Redis with Laravel, install the required PHP extension:

# Install PHP Redis extension
sudo apt install php-redis -y

# Or via PECL
sudo pecl install redis

Update your Laravel .env file:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=your_password
REDIS_PORT=6379
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis

Troubleshooting

Check Redis logs

sudo tail -f /var/log/redis/redis-server.log

Test Redis connection

redis-cli ping

Check Redis memory usage

redis-cli info memory

Monitor Redis in real-time

redis-cli monitor

Additional Resources

  • Official Redis Documentation: https://redis.io/documentation

  • Redis Commands Reference: https://redis.io/commands

  • Redis Security Guide: https://redis.io/topics/security

Last updated

Was this helpful?