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
Full Script
Here is the full Bash script used for this Redis install and local object-cache workflow:
#!/usr/bin/env bash
__show_script_usage() {
cat <<'__SCRIPT_USAGE__'
# Install-Redis.sh
Installs and configures Redis for local WordPress object caching.
## Example Usage
```bash
cd /opt/DevOps/Scripts
sudo bash ./Install-Redis.sh
```
## Notes
This changes Redis configuration, writes backups under `/opt/DevOps/Backups/Redis`, and restarts Redis.
__SCRIPT_USAGE__
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
case "${1:-}" in
-h|--help|--usage)
__show_script_usage
exit 0
;;
esac
fi
REDIS_BIND_ADDRESS="127.0.0.1 ::1"
REDIS_PORT="6379"
REDIS_MAXMEMORY="2gb"
REDIS_MAXMEMORY_POLICY="allkeys-lru"
REDIS_TCP_BACKLOG="511"
REDIS_TIMEOUT="0"
DEVOPS_BACKUP_DIR="/opt/DevOps/Backups/Redis"
set -euo pipefail
echo "Installing and configuring Redis..."
if [ "$EUID" -ne 0 ]; then
echo "ERROR: Run with sudo."
exit 1
fi
apt update
apt install -y redis-server
systemctl enable redis-server
mkdir -p "${DEVOPS_BACKUP_DIR}"
cp /etc/redis/redis.conf "${DEVOPS_BACKUP_DIR}/redis.conf.before.$(date +%Y%m%d%H%M%S).bak"
sed -i "s/^bind .*/bind ${REDIS_BIND_ADDRESS}/" /etc/redis/redis.conf
sed -i "s/^port .*/port ${REDIS_PORT}/" /etc/redis/redis.conf
sed -i "s/^# maxmemory .*/maxmemory ${REDIS_MAXMEMORY}/" /etc/redis/redis.conf
sed -i "s/^# maxmemory-policy .*/maxmemory-policy ${REDIS_MAXMEMORY_POLICY}/" /etc/redis/redis.conf
sed -i "s/^tcp-backlog .*/tcp-backlog ${REDIS_TCP_BACKLOG}/" /etc/redis/redis.conf
sed -i "s/^timeout .*/timeout ${REDIS_TIMEOUT}/" /etc/redis/redis.conf
grep -q "^protected-mode yes" /etc/redis/redis.conf || echo "protected-mode yes" >> /etc/redis/redis.conf
cp /etc/redis/redis.conf "${DEVOPS_BACKUP_DIR}/redis.conf.after.$(date +%Y%m%d%H%M%S).bak"
redis-server /etc/redis/redis.conf --test-memory 2 >/dev/null
systemctl restart redis-server
systemctl is-active --quiet redis-server
echo "Redis install complete."
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.