Install and Configure Redis on Ubuntu for Local Object Cache
Redis is often used as a local object cache for WordPress and other web applications. It can reduce repeated database work by keeping frequently used values in memory.
This guide shows how to install Redis on Ubuntu, bind it to local addresses, set a memory limit, choose an eviction policy, validate memory, and restart the service.
What the Script Does
The deployment script this article is based on:
- Installs
redis-server. - Enables the Redis service.
- Backs up
/etc/redis/redis.conf. - Sets bind address, port, memory limit, eviction policy, backlog, and timeout.
- Keeps protected mode enabled.
- Runs a Redis memory test.
- Restarts and verifies the Redis service.
Install Redis
Run:
sudo apt update sudo apt install -y redis-server sudo systemctl enable redis-server
Check the service:
sudo systemctl status redis-server
Back Up the Redis Config
Before editing Redis, create a backup:
sudo mkdir -p /opt/server-backups/redis
timestamp="$(date +%Y%m%d%H%M%S)"
sudo cp /etc/redis/redis.conf "/opt/server-backups/redis/redis.conf.before.${timestamp}.bak"
Apply Local Cache Settings
This example uses Redis as a local service:
- Bind address:
127.0.0.1 ::1 - Port:
6379 - Max memory:
2gb - Eviction policy:
allkeys-lru - Protected mode:
yes
Apply the settings:
sudo sed -i "s/^bind .*/bind 127.0.0.1 ::1/" /etc/redis/redis.conf sudo sed -i "s/^port .*/port 6379/" /etc/redis/redis.conf sudo sed -i "s/^# maxmemory .*/maxmemory 2gb/" /etc/redis/redis.conf sudo sed -i "s/^# maxmemory-policy .*/maxmemory-policy allkeys-lru/" /etc/redis/redis.conf sudo sed -i "s/^tcp-backlog .*/tcp-backlog 511/" /etc/redis/redis.conf sudo sed -i "s/^timeout .*/timeout 0/" /etc/redis/redis.conf
Make sure protected mode is enabled:
sudo grep -q "^protected-mode yes" /etc/redis/redis.conf || echo "protected-mode yes" | sudo tee -a /etc/redis/redis.conf >/dev/null
For a single-server WordPress setup, binding Redis to localhost is usually the right default. Do not expose Redis directly to the public internet.
Test and Restart Redis
Run a basic memory test:
sudo redis-server /etc/redis/redis.conf --test-memory 2 >/dev/null
Back up the after-change config:
sudo cp /etc/redis/redis.conf "/opt/server-backups/redis/redis.conf.after.${timestamp}.bak"
Restart Redis:
sudo systemctl restart redis-server sudo systemctl is-active --quiet redis-server
Test with redis-cli:
redis-cli ping
Expected output:
PONG
Troubleshooting
If Redis will not restart, check:
sudo systemctl status redis-server sudo journalctl -u redis-server -xe
If redis-cli ping fails, confirm Redis is listening on the expected address and port:
ss -ltnp | grep 6379
Quick Reference
sudo apt install -y redis-server sudo systemctl enable redis-server sudo systemctl restart redis-server redis-cli ping
Redis is powerful, but keep it local unless you have a specific secured network design. For WordPress object caching on one server, localhost Redis is usually enough.