Software & Configuration
Redis: Installation and Basic Usage
How to install and configure Redis on your VPS for caching, sessions and message queues
Redis is an extremely fast in-memory database used for caching, session management, message queues and much more. It's used by Laravel, WordPress (with plugins), Next.js, and many other applications.
Installation
# Debian/Ubuntu
apt update && apt install redis-server -y
# CentOS/AlmaLinux
dnf install redis -y
# Start and enable
systemctl enable --now redisVerify installation
systemctl status redis
# Test from CLI
redis-cli ping
# Response: PONGBasic configuration
nano /etc/redis/redis.confImportant parameters
# Listen only on localhost (more secure, default)
bind 127.0.0.1 -::1
# Port (default 6379)
port 6379
# Access password (recommended)
requirepass long_secure_password
# Maximum memory size
maxmemory 256mb
# Policy when memory is full
# allkeys-lru: removes least recently used keys (recommended for cache)
maxmemory-policy allkeys-lru
# Persistence to disk (comment out if using Redis only as cache)
# save 900 1
# save 300 10
# save 60 10000systemctl restart redisBasic Redis commands
# Access CLI
redis-cli
# With password
redis-cli -a long_secure_password
# Test
PING
# Set a value
SET key "value"
# Read a value
GET key
# Set with expiration (60 seconds)
SET session:123 "data" EX 60
# Check TTL (remaining time)
TTL session:123
# Delete a key
DEL key
# List all keys (avoid in production with many keys!)
KEYS *
# Total number of keys
DBSIZE
# Server info
INFO server
INFO memoryRedis with Laravel
In .env:
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=long_secure_password
REDIS_PORT=6379Install the PHP client:
composer require predis/predis
# or install PHP extension:
apt install php-redis -y
systemctl restart php8.2-fpmRedis with WordPress
Install the Redis Object Cache plugin:
- WordPress Dashboard → Plugins → Add New → search "Redis Object Cache"
- Install and activate
- Add to
wp-config.php:
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PASSWORD', 'long_secure_password');
define('WP_REDIS_DATABASE', 0);- Dashboard → Settings → Redis → Enable Object Cache
Monitor Redis
# Real-time statistics
redis-cli -a PASSWORD info stats
# Commands executed per second (updates every second)
redis-cli -a PASSWORD --stat
# Monitor all commands in real time (dev only, impacts performance)
redis-cli -a PASSWORD monitor
# Memory used
redis-cli -a PASSWORD info memory | grep used_memory_humanRedis must not be exposed on the internet
Redis has no advanced protections: never expose it on a public port. It must always listen on 127.0.0.1 (localhost) or on a private network.
Verify the port is not exposed:
ss -tlnp | grep 6379
# Must show 127.0.0.1:6379, NOT 0.0.0.0:6379If for some reason it is exposed publicly, block it immediately:
ufw deny 6379Flush the cache
# Empty the current database (DB 0)
redis-cli -a PASSWORD FLUSHDB
# Empty all databases
redis-cli -a PASSWORD FLUSHALL