如何在CentOS 9上安装和配置Remi源的Redis?
美国、香港服务器
如何在CentOS 9上安装和配置Remi源的Redis?
05-28 来源:
一、准备:启用 CRB + EPEL + Remi(已做可跳过)
bash
运行
# 1. 启用 CRB(必须,依赖用)
sudo dnf config-manager --set-enabled crb
# 2. 安装 EPEL
sudo dnf install -y epel-release epel-next-release
# 3. 安装 Remi 源(CentOS 9)
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
# 4. 替换为阿里镜像(加速)
sudo sed -i "s/http:\/\/rpms.remirepo.net/https:\/\/mirrors.aliyun.com\/remi/g" /etc/yum.repos.d/remi*
sudo sed -i "s/#baseurl/baseurl/g" /etc/yum.repos.d/remi*
sudo sed -i "s|^mirrorlist|#mirrorlist|" /etc/yum.repos.d/remi*
# 5. 刷新缓存
sudo dnf clean all
sudo dnf makecache
验证 Remi:
bash
运行
dnf repolist | grep remi
二、安装 Remi 源的 Redis(选一个版本)
1. 查看 Remi 提供的 Redis 模块
bash
运行
dnf module list redis
# 会看到 redis:remi-7.2、redis:remi-8.2、redis:remi-8.6
2. 启用并安装(推荐 8.2 稳定)
bash
运行
# 重置默认模块
sudo dnf module reset redis -y
# 启用 Remi 的 Redis 版本(三选一)
sudo dnf module enable redis:remi-8.2 -y # 8.2 稳定
# sudo dnf module enable redis:remi-7.2 -y # 7.2 旧稳定
# sudo dnf module enable redis:remi-8.6 -y # 8.6 最新
# 安装 Redis
sudo dnf install -y redis
# 锁定版本(防止自动升级)
sudo dnf versionlock add redis*
3. 验证版本
bash
运行
redis-server --version
# 输出示例:Redis server v=8.2.6 (Remi) ...
三、启动 & 开机自启
bash
运行
# 启动
sudo systemctl start redis
# 开机自启
sudo systemctl enable redis
# 查看状态(active 即正常)
systemctl status redis
四、安全与性能配置(/etc/redis.conf)
bash
运行
sudo vi /etc/redis.conf
修改 / 添加以下关键配置:
ini
# 1. 绑定地址(远程用 0.0.0.0,本地用 127.0.0.1)
bind 0.0.0.0
# 2. 设置密码(必须,强密码)
requirepass YourStrongPassword123
# 3. 端口(默认 6379)
port 6379
# 4. 内存优化(根据服务器内存调整,例如 2G 服务器设 1G)
maxmemory 1gb
maxmemory-policy allkeys-lru
# 5. 关闭保护模式(允许远程)
protected-mode no
# 6. 日志
logfile /var/log/redis/redis.log
重启生效:
bash
运行
sudo systemctl restart redis
五、放行防火墙(6379 端口)
bash
运行
sudo firewall-cmd --add-port=6379/tcp --permanent
sudo firewall-cmd --reload
# 查看
firewall-cmd --list-ports
六、连接测试
本地连接
bash
运行
redis-cli
# 认证
AUTH YourStrongPassword123
# 测试
PING # 返回 PONG 正常
远程连接(从另一台机器)
bash
运行
redis-cli -h 服务器IP -p 6379
AUTH YourStrongPassword123
PING
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快