在 Ubuntu VPS 上安装 Redis 实现 WordPress 对象缓存

Redis 是一款内存数据存储工具,非常适合用作 WordPress 的对象缓存。启用 Redis 对象缓存后,WordPress 会将数据库查询结果存储在 Redis 中,而不必在每次页面加载时重复执行相同的数据库查询。其结果是响应速度明显加快,数据库 CPU 占用率大幅降低。

WordPress 对象缓存与 Redis 的工作原理

在没有持久化对象缓存的情况下,WordPress 会在每次请求时查询数据库——即使是对于未曾改变的数据也不例外。Redis 充当快速的基于 RAM 的缓存:第一次请求从数据库获取数据并存入 Redis,后续所有请求直接从 Redis 读取,完全跳过数据库,直到缓存过期。

Redis 与 Memcached 的对比:两者都是内存缓存。Redis 支持更丰富的数据结构(列表、哈希、集合)、可选的持久化功能以及发布/订阅(Pub/Sub)。Memcached 更为简单,但适用场景较少。对于 WordPress 而言,两者均可胜任,但 2026 年 Redis 是更主流的选择。

在 Ubuntu 22.04 上安装 Redis

第一步:安装 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

第二步:配置 redis.conf

sudo nano /etc/redis/redis.conf

更新以下设置:

# 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

第三步:重启 Redis

sudo systemctl restart redis-server

第四步:测试 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

加固 VPS 上的 Redis 安全性

未经保护而暴露在公网上的 Redis 实例存在严重的安全隐患,攻击者会定期扫描开放的 Redis 端口。在任何生产环境部署之前,请务必完成以下安全加固步骤。

使用 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

确认 Redis 已绑定到本地回环地址

# 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

启用保护模式并重命名危险命令

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

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

安全检查清单:上线前请确认:(1) 已绑定 127.0.0.1,(2) 已设置 requirepass,(3) UFW 已阻止外部访问 6379 端口,(4) protected-mode 为 yes。在外部机器运行 redis-cli -h YOUR_SERVER_IP ping 验证,结果必须超时。

VPS 上的 Redis 持久化选项

Redis 默认使用 RDB 快照定期将数据写入磁盘。对于 WordPress 对象缓存而言,服务器重启会导致短暂的缓存穿透——WordPress 将重新查询数据库并在几分钟内重新填充 Redis,对大多数网站来说这是可以接受的。但如果您将 Redis 用作 Session 存储,持久化则更为重要。

默认 RDB 快照设置

# /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(追加写入文件)提供更高持久性

# 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

WordPress 推荐配置:对象缓存无需持久化。在 redis.conf 中设置 save "" 以禁用 RDB 快照,可获得轻微的性能提升。WordPress 在每次重启后会自动重建缓存。

安装 PHP Redis 扩展

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

sudo systemctl restart php8.3-fpm

使用 WP Redis 插件将 WordPress 连接到 Redis

安装插件

进入 WordPress 后台 → 插件 → 添加新插件 → 搜索 Automattic 的"WP Redis" → 安装并启用。或使用 WP-CLI:

wp plugin install wp-redis --activate

编辑 wp-config.php

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

在 /* 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_');

放入对象缓存文件

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

验证缓存是否正常运行

进入 WordPress 后台 → 设置 → WP Redis,应显示"Connected"以及缓存命中/未命中计数器。

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

预期效果:启用 Redis 对象缓存后,分类归档、搜索结果及 WooCommerce 商店页面等数据库密集型页面的首字节时间(TTFB)预计将降低 30%–50%。

监控与调优 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.

在 VPS 上构建完整的 WordPress 缓存体系

Redis 对象缓存只是 WordPress 缓存策略中的一个层次。将其与其他缓存层结合使用,可以带来最佳的用户体验和最低的服务器负载。

缓存层 工具 优势
对象缓存 Redis + WP Redis 消除重复数据库查询,优化动态内容
页面缓存 WP Super Cache / W3 Total Cache 缓存渲染后的 HTML,减少 PHP 处理开销
字节码缓存 PHP OPcache(内置) 缓存 PHP 字节码,减少编译时间
Nginx FastCGI 缓存 Nginx fastcgi_cache 在 Web 服务器层缓存,响应速度最快

检查 PHP OPcache 是否已启用

# 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

检查 WordPress Transient 缓存

# 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

在 VPS 上运行 WordPress,性能全面释放

Linux VPS 月租低至 500 泰铢,提供完整 Root 权限——自由安装 Redis、Nginx、PHP-FPM 及任何 WordPress 优化组件。

查看 VPS 套餐

查看泰国VPS主机全部套餐 →