Redis是一种高性能的内存数据库,可以有效加速WordPress网站。以下是详细的Redis缓存设置步骤:
服务器要求:
检查Redis是否安装:
redis-cli ping
如果返回"PONG"表示Redis服务正常运行
sudo apt update
sudo apt install redis-server
sudo systemctl enable redis-server
sudo systemctl start redis-server
Windows官方不提供Redis支持,但可以使用Microsoft提供的Windows端口版本
sudo apt install php-redis
sudo systemctl restart apache2 # 或nginx/php-fpm
推荐使用以下插件之一:
以Redis Object Cache为例:
wp-config.php
文件,在/* 好了!请不要再继续编辑。请保存本文件。使用愉快! */
之前添加:define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', '6379');
define('WP_REDIS_TIMEOUT', 1);
define('WP_REDIS_READ_TIMEOUT', 1);
define('WP_REDIS_DATABASE', 0); // 对于多站点,可以设置为不同的数字
// 使用可靠的Redis连接
define('WP_REDIS_CLIENT', 'pecl');
// 启用Redis缓存内容
define('WP_CACHE', true);
// 设置键前缀(多站点有用)
define('WP_REDIS_PREFIX', 'wp_'.DB_NAME);
调整Redis内存限制:
编辑/etc/redis/redis.conf
:
maxmemory 256mb
maxmemory-policy allkeys-lru
持久化设置(根据需求选择):
save 900 1
save 300 10
save 60 10000
监控Redis性能:
redis-cli info
redis-cli monitor
连接失败:
wp-config.php
中的主机和端口正确内存不足:
maxmemory
设置redis-cli FLUSHALL
插件冲突:
多站点配置:
define('WP_REDIS_DATABASE', get_current_blog_id());
使用Redis作为会话存储:
在wp-config.php
中添加:
define('WP_REDIS_SESSION_HOST', '127.0.0.1');
define('WP_REDIS_SESSION_PORT', 6379);
使用Redis进行页面缓存(需配合其他插件如WP Rocket)
通过以上设置,您的WordPress网站应该能获得显著的性能提升。记得定期监控服务器资源使用情况,确保Redis正常运行。