Install Redis on Ubuntu VPS for WordPress Object Cache

Redis is an in-memory data store that makes an excellent object cache for WordPress. With Redis Object Cache enabled, WordPress stores database query results in Redis instead of repeating the same database hits on every page load. The result is noticeably faster response times and significantly lower database CPU usage.

How WordPress Object Cache Works with Redis

Without a persistent object cache, WordPress queries the database on every request — even for data that hasn't changed. Redis acts as a fast RAM-based cache: the first request fetches data from the database and stores it in Redis; all subsequent requests read from Redis, skipping the database entirely until the cache expires.

Redis vs Memcached: Both are in-memory caches. Redis supports richer data structures (lists, hashes, sets), optional persistence, and Pub/Sub. Memcached is simpler but covers fewer use cases. For WordPress, both work well, but Redis is the more popular choice in 2026.

Install Redis on Ubuntu 22.04

Step 1: Install Redis

sudo apt update
sudo apt install -y redis-server

redis-server --version

sudo systemctl enable redis-server
sudo systemctl start redis-server
sudo systemctl status redis-server

Step 2: Configure redis.conf

sudo nano /etc/redis/redis.conf

Update these settings:

# Bind to localhost only — never expose to the internet
bind 127.0.0.1

# Max memory — set to about 20-25% of total RAM
maxmemory 256mb

# Eviction policy: remove least-recently-used keys when full
maxmemory-policy allkeys-lru

# Optional: set a strong password
requirepass YourStrongRedisPassword

Step 3: Restart Redis

sudo systemctl restart redis-server

Step 4: Test Redis

# Connect with redis-cli
redis-cli -a YourStrongRedisPassword

# Basic SET / GET test
SET testkey "hello redis"
GET testkey
# Returns: "hello redis"

# Memory stats
INFO memory

Securing Redis on Your VPS

An unsecured Redis instance exposed to the internet is a serious security risk. Attackers regularly scan for open Redis ports. Complete the following hardening steps before any production deployment.

Verify the Firewall with UFW

# Check that port 6379 is not open to the internet
sudo ufw status

# Set up basic UFW rules if not already done
sudo ufw allow ssh
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Do NOT allow 6379 — Redis must remain on localhost only
sudo ufw enable

Confirm Redis Is Bound to Localhost

# Verify Redis is listening on 127.0.0.1, not 0.0.0.0
sudo ss -tlnp | grep 6379
# Expected output: 127.0.0.1:6379

# If you see 0.0.0.0:6379, immediately edit /etc/redis/redis.conf:
# bind 127.0.0.1

Enable Protected Mode and Rename Dangerous Commands

# In /etc/redis/redis.conf
protected-mode yes

# Optionally rename dangerous commands to prevent misuse
rename-command FLUSHALL ""
rename-command CONFIG "CONFIG_INTERNAL_ONLY"

Security Checklist: Before going live, verify: (1) bind 127.0.0.1 only, (2) requirepass is set, (3) UFW blocks port 6379 externally, (4) protected-mode yes. Test by running redis-cli -h YOUR_SERVER_IP ping from outside — it must time out.

Redis Persistence Options for VPS

By default Redis uses RDB snapshots to periodically save data to disk. For WordPress Object Cache, a server restart causes a temporary cache miss storm — WordPress queries the database and repopulates Redis within minutes. This is acceptable for most sites. However, if you use Redis as a session store, persistence matters more.

Default RDB Snapshot Settings

# /etc/redis/redis.conf — default RDB settings
save 3600 1    # Snapshot if at least 1 key changed in 1 hour
save 300 100   # Snapshot if 100 keys changed in 5 minutes
save 60 10000  # Snapshot if 10000 keys changed in 1 minute

# For pure Object Cache, you can disable persistence for maximum speed:
# save ""

AOF (Append Only File) for Higher Durability

# Enable AOF in redis.conf (recommended for session stores, not object cache)
appendonly yes
appendfsync everysec   # Flush to disk every second — balanced durability/speed

# Check AOF file size
ls -lh /var/lib/redis/appendonly.aof

Recommendation for WordPress: Object Cache does not need persistence. Set save "" in redis.conf to disable RDB snapshots and gain a small performance boost. WordPress rebuilds the cache automatically after any restart.

Install the PHP Redis Extension

# For PHP 8.3 — adjust version as needed
sudo apt install -y php8.3-redis

sudo systemctl restart php8.3-fpm

Connect WordPress to Redis with WP Redis Plugin

Install the Plugin

Go to WordPress Admin → Plugins → Add New → search for "WP Redis" by Automattic → Install and Activate. Or via WP-CLI:

wp plugin install wp-redis --activate

Edit wp-config.php

sudo nano /var/www/html/wp-config.php

Add before /* That's all, stop editing! */:

// Redis Object Cache
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PASSWORD', 'YourStrongRedisPassword');
define('WP_REDIS_DATABASE', 0);
define('WP_REDIS_PREFIX', 'wp_');

Drop in the Object Cache File

cp /var/www/html/wp-content/plugins/wp-redis/object-cache.php \
   /var/www/html/wp-content/object-cache.php

Verify the Cache Is Active

Go to WordPress Admin → Settings → WP Redis. You should see "Connected" along with a Cache Hit / Miss counter.

redis-cli -a YourStrongRedisPassword INFO keyspace
# Expect: db0: keys=XXX — this means data is being cached

Expected Result: After enabling Redis Object Cache, Time to First Byte (TTFB) should drop by 30-50% on database-heavy pages like category archives, search results, and WooCommerce shop pages.

Monitor and Tune Redis

# Watch commands in real time
redis-cli -a pass MONITOR

# Check hit rate
redis-cli -a pass INFO stats | grep -E "keyspace_hits|keyspace_misses"

# hit_rate = keyspace_hits / (keyspace_hits + keyspace_misses) * 100
# Aim for > 90%. If lower, consider increasing maxmemory.

Building a Complete WordPress Cache Stack on VPS

Redis Object Cache is one layer in a well-rounded WordPress caching strategy. Combining it with other caching layers delivers the best user experience and the lowest server load.

Cache Layer Tool Benefit
Object Cache Redis + WP Redis Eliminates repeated DB queries; helps dynamic content
Page Cache WP Super Cache / W3 Total Cache Stores rendered HTML; reduces PHP processing
Opcode Cache PHP OPcache (built-in) Caches PHP bytecode; reduces compile time
Nginx FastCGI Cache Nginx fastcgi_cache Caches at web server layer; fastest possible response

Check PHP OPcache Is Active

# Verify OPcache is enabled
php -r "var_dump(function_exists('opcache_get_status'));"

# View OPcache stats
php -r "print_r(opcache_get_status());"

# Key php.ini settings for OPcache
# opcache.enable=1
# opcache.memory_consumption=128
# opcache.max_accelerated_files=10000

Check WordPress Transient Cache

# Count transients currently stored in Redis (after enabling Redis Object Cache)
redis-cli -a YourStrongRedisPassword KEYS "wp_transient_*" | wc -l
# A high number means plugins rely heavily on transients — Redis helps significantly

Run WordPress on VPS for Maximum Performance

Linux VPS starting at 500 THB/month with Full Root Access — install Redis, Nginx, PHP-FPM, and any WordPress optimization stack you need.

View VPS Plans

View all affordable VPS Thailand plans →