Redis: Installation and Basic Usage
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.
02
Installation
bash
# Debian/Ubuntu
apt update && apt install redis-server -y
# CentOS/AlmaLinux
dnf install redis -y
# Start and enable
systemctl enable --now redis
Verify installation
bash
systemctl status redis
# Test from CLI
redis-cli ping
# Response: PONG
03
Basic configuration
bash
nano /etc/redis/redis.conf
Important parameters
ini
# 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 10000
bash
systemctl restart redis
04
Basic Redis commands
bash
# 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 memory
05
Redis with Laravel
In .env:
ini
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=long_secure_password
REDIS_PORT=6379
Install the PHP client:
bash
composer require predis/predis
# or install PHP extension:
apt install php-redis -y
systemctl restart php8.2-fpm
06
Redis with WordPress
Install the Redis Object Cache plugin:
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);
- WordPress Dashboard → Plugins → Add New → search "Redis Object Cache"
- Install and activate
- Add to wp-config.php:
- Dashboard → Settings → Redis → Enable Object Cache
07
Monitor Redis
bash
# 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_human
08
Redis 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:
bash
ss -tlnp | grep 6379
# Must show 127.0.0.1:6379, NOT 0.0.0.0:6379
If for some reason it is exposed publicly, block it immediately:
bash
ufw deny 6379
09
Flush the cache
bash
# Empty the current database (DB 0)
redis-cli -a PASSWORD FLUSHDB
# Empty all databases
redis-cli -a PASSWORD FLUSHALL
Gerelateerde artikelen
