Ubuntu 24.04 安装 Redis 7.2.5 稳定版

我爱海鲸 2026-01-04 09:47:43 暂无标签

简介乌班图

Ubuntu 24.04 安装 Redis 7.2.x 稳定版(避坑版,全路径 /opt/redis)

一、文档说明

本文档针对 Ubuntu 24.04 LTS 系统,解决编译安装 Redis 过程中「端口占用、systemd 解析错误、权限冲突、密码语法错误」等常见问题,最终采用「Redis 原生守护进程 + 简易开机自启」方案,确保服务稳定运行,所有文件统一部署至 /opt/redis

二、环境准备

1. 系统更新与依赖安装(解决 pkg-config 缺失)

bash
 
运行
 
 
 
 
# 更新系统包
sudo apt update && sudo apt upgrade -y

# 安装完整编译依赖(含解决网络问题的阿里云源替换)
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo tee /etc/apt/sources.list <<EOF
deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
EOF
sudo apt update
sudo apt install -y build-essential tcl libjemalloc-dev wget curl vim pkg-config libssl-dev lsof net-tools
 

2. 创建 Redis 专用用户(安全必备)

bash
 
运行
 
 
 
 
sudo groupadd -r redis
sudo useradd -r -g redis -s /sbin/nologin -d /opt/redis redis
 

三、创建 /opt/redis 目录结构

bash
 
运行
 
 
 
 
# 创建主目录及子目录
sudo mkdir -p /opt/redis/{bin,conf,data,log,run}

# 设置权限(redis用户专属)
sudo chown -R redis:redis /opt/redis
sudo chmod 750 /opt/redis/{data,log,run}
sudo chmod 755 /opt/redis/{bin,conf}

# 预创建日志文件(避免启动时权限不足)
sudo touch /opt/redis/log/redis-server.log
sudo chown redis:redis /opt/redis/log/redis-server.log
 

四、下载并编译安装 Redis

1. 下载 Redis 7.2.x 稳定版

bash
 
运行
 
 
 
 
cd /tmp
wget https://download.redis.io/releases/redis-7.2.5.tar.gz
tar -zxvf redis-7.2.5.tar.gz
cd redis-7.2.5
 

2. 编译并安装至 /opt/redis/bin

bash
 
运行
 
 
 
 
# 清理旧编译文件
make clean

# 编译(指定 jemalloc 内存分配器)
make MALLOC=jemalloc

# 安装到 /opt/redis/bin
sudo make install PREFIX=/opt/redis

# 验证安装
ls /opt/redis/bin  # 应看到 redis-server、redis-cli 等文件
 

五、配置 Redis(避坑版核心配置)

1. 复制并编辑配置文件

bash
 
运行
 
 
 
 
sudo cp /tmp/redis-7.2.5/redis.conf /opt/redis/conf/redis.conf
sudo vim /opt/redis/conf/redis.conf
 

2. 核心配置(直接替换,避免语法错误)

ini
 
 
 
 
 
# 基础运行配置
daemonize yes          # 原生守护进程模式(避开systemd坑)
bind 127.0.0.1         # 仅本地访问,禁止外网
protected-mode yes     # 保护模式
port 6379              # 监听端口
requirepass Redis123456  # 密码用纯字母数字,避免解析错误
supervised no          # 完全关闭systemd监督

# 路径配置(全指向 /opt/redis)
dir /opt/redis/data
logfile /opt/redis/log/redis-server.log
pidfile /opt/redis/run/redis-server.pid

# 性能配置
maxmemory 1G           # 按服务器内存调整(4G内存设2G)
maxmemory-policy allkeys-lru  # 内存淘汰策略
loglevel notice        # 日志级别
 

六、启动 Redis(原生守护进程模式,避坑核心)

1. 释放 6379 端口(解决占用问题)

bash
 
运行
 
 
 
 
# 强制杀死占用6379端口的进程
sudo fuser -k 6379/tcp

# 验证端口释放
sudo lsof -i:6379 || echo "6379端口已释放"
 

2. 启动 Redis(redis 用户运行)

bash
 
运行
 
 
 
 
# 以redis用户启动(守护进程模式)
sudo -u redis /opt/redis/bin/redis-server /opt/redis/conf/redis.conf

# 验证启动结果
ps aux | grep redis-server | grep -v grep  # 应看到redis进程
sudo lsof -i:6379                          # 应看到6379端口被redis监听
/opt/redis/bin/redis-cli -a Redis123456 PING  # 应返回PONG
 

七、配置开机自启(替代 systemd,简易稳定)

bash
 
运行
 
 
 
 
# 创建 rc.local 开机自启脚本
sudo tee /etc/rc.local <<'EOF'
#!/bin/bash
# 启动Redis(redis用户运行)
sudo -u redis /opt/redis/bin/redis-server /opt/redis/conf/redis.conf
exit 0
EOF

# 添加执行权限
sudo chmod +x /etc/rc.local

# 验证开机自启(可选,重启服务器测试)
# sudo reboot
 

八、Redis 常用管理命令(避坑版)

功能 命令    
启动 Redis sudo -u redis /opt/redis/bin/redis-server /opt/redis/conf/redis.conf    
停止 Redis /opt/redis/bin/redis-cli -a Redis123456 shutdown    
重启 Redis sudo fuser -k 6379/tcp && sudo -u redis /opt/redis/bin/redis-server /opt/redis/conf/redis.conf    
查看运行状态 ps aux grep redis-server grep -v grep
查看端口监听 sudo lsof -i:6379    
查看日志 tail -f /opt/redis/log/redis-server.log    
连接客户端 /opt/redis/bin/redis-cli -a Redis123456    
验证配置语法 /opt/redis/bin/redis-server --test-config /opt/redis/conf/redis.conf    

九、安全加固(生产环境必做)

1. 防火墙限制端口访问

bash
 
运行
 
 
 
 
# 启用ufw防火墙
sudo ufw enable

# 仅允许指定IP访问6379(替换为你的客户端IP)
sudo ufw allow from 192.168.1.100 to any port 6379

# 查看防火墙规则
sudo ufw status
 

2. 禁用危险命令(可选)

bash
 
运行
 
 
 
 
sudo vim /opt/redis/conf/redis.conf
# 添加以下配置
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command CONFIG "REDIS_CONFIG_123"

# 重启Redis生效
/opt/redis/bin/redis-cli -a Redis123456 shutdown
sudo -u redis /opt/redis/bin/redis-server /opt/redis/conf/redis.conf
 

3. 定期备份数据

bash
 
运行
 
 
 
 
# 创建每日备份脚本
sudo tee /opt/redis/backup_redis.sh <<'EOF'
#!/bin/bash
BACKUP_DIR=/opt/redis/backup
mkdir -p $BACKUP_DIR
cp -r /opt/redis/data $BACKUP_DIR/data_$(date +%Y%m%d)
# 保留7天备份
find $BACKUP_DIR -name "data_*" -mtime +7 -delete
EOF

# 添加执行权限并配置定时任务
sudo chmod +x /opt/redis/backup_redis.sh
sudo crontab -e
# 添加以下内容(每日凌晨1点备份)
0 1 * * * /opt/redis/backup_redis.sh
 

十、常见问题排查

1. 启动失败:端口占用

bash
 
运行
 
 
 
 
sudo fuser -k 6379/tcp  # 强制释放端口
sudo -u redis /opt/redis/bin/redis-server /opt/redis/conf/redis.conf
 

2. 连接失败:密码错误

bash
 
运行
 
 
 
 
# 重置密码
/opt/redis/bin/redis-cli
127.0.0.1:6379> AUTH Redis123456
127.0.0.1:6379> CONFIG SET requirepass "NewRedisPwd123"
127.0.0.1:6379> CONFIG REWRITE  # 永久保存
 

3. 日志无输出 / 权限错误

bash
 
运行
 
 
 
 
# 重置目录权限
sudo chown -R redis:redis /opt/redis
sudo chmod 750 /opt/redis/{data,log,run}
 

十一、卸载 Redis(可选)

bash
 
运行
 
 
 
 
# 停止Redis
/opt/redis/bin/redis-cli -a Redis123456 shutdown

# 删除目录及文件
sudo rm -rf /opt/redis
sudo rm -f /etc/rc.local

# 删除用户/组
sudo userdel redis
sudo groupdel redis

# 清理源码
sudo rm -rf /tmp/redis-7.2.5*
 

十二、最终验证

执行以下命令,全部正常则安装成功:
bash
 
运行
 
 
 
 
# 1. 进程运行
ps aux | grep redis-server | grep -v grep

# 2. 端口监听
sudo lsof -i:6379

# 3. 功能测试
/opt/redis/bin/redis-cli -a Redis123456 SET test "success"
/opt/redis/bin/redis-cli -a Redis123456 GET test  # 输出 "success"

# 4. 日志无报错
tail -10 /opt/redis/log/redis-server.log
 

核心避坑总结

  1. 放弃复杂的 systemd 服务配置,采用 Redis 原生 daemonize yes 模式,避开「protocol 错误、密码解析错误」;
  2. 密码使用纯字母数字,避免命令行解析异常;
  3. 提前释放 6379 端口,解决「Address already in use」启动失败;
  4. 统一目录权限为 redis 用户,避免「Permission denied」隐性错误;
  5. 用 rc.local 替代 systemd 实现开机自启,简单稳定。
至此,Redis 7.2.x 稳定运行在 /opt/redis 目录下,满足生产环境基本使用需求,且避开了 Ubuntu 24.04 下的各类启动坑。

你好:我的2025