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 -yInstall from Official Repositories (Recommended)
Ubuntu/Debian
sudo apt install redis-server -yCentOS/RHEL/Fedora
sudo yum install redis -y
# or for newer versions
sudo dnf install redis -yConfiguration
Basic Configuration
The main Redis configuration file is typically located at /etc/redis/redis.conf.
# Open configuration file
sudo nano /etc/redis/redis.confKey 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.serviceAdd 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.targetCreate 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/redisManaging Redis Service
Start Redis
sudo systemctl start redis
# or for older systems
sudo service redis startEnable Redis to start on boot
sudo systemctl enable redisCheck Redis status
sudo systemctl status redisStop Redis
sudo systemctl stop redisRestart Redis
sudo systemctl restart redisVerify 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> exitSecurity Recommendations
Set a strong password in the configuration file using
requirepassBind to localhost if Redis is only used locally:
bind 127.0.0.1Configure firewall to restrict access:
# Allow only specific IP sudo ufw allow from trusted_ip to any port 6379Disable dangerous commands in production:
rename-command FLUSHDB "" rename-command FLUSHALL "" rename-command CONFIG ""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 redisUpdate 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=redisTroubleshooting
Check Redis logs
sudo tail -f /var/log/redis/redis-server.logTest Redis connection
redis-cli pingCheck Redis memory usage
redis-cli info memoryMonitor Redis in real-time
redis-cli monitorAdditional 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?