在 Debian 上配置 Redis,通常分为安装、基础配置、安全配置、开机自启、常用调优几个部分。下面按步骤说明(以 Debian 11/12 + Redis 6/7 为例)。
sudo apt update
sudo apt install redis-server -y
安装完成后,Redis 会自动以 systemd 方式运行。
查看状态:
sudo systemctl status redis-server
Debian 中 Redis 默认配置文件为:
/etc/redis/redis.conf
⚠️ 修改前先备份:
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.bak
编辑配置:
sudo nano /etc/redis/redis.conf
默认只监听本地:
bind 127.0.0.1 ::1
bind 0.0.0.0
protected-mode yes
yes:未设密码且非本地访问会被拒绝(安全)requirepass your_strong_password
客户端连接时需:
redis-cli -a your_strong_password
port 6379
可修改为非默认端口提升安全性。
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
生产环境通常 RDB + AOF 同时开启。
maxmemory 512mb
maxmemory-policy allkeys-lru
常见策略:
noeviction:不删除(写报错)allkeys-lru:删最久未用volatile-lru:仅删有过期时间的logfile /var/log/redis/redis-server.log
daemonize no
Debian 下通常由 systemd 管理,不要改 daemonize。
sudo systemctl restart redis-server
开机自启:
sudo systemctl enable redis-server
sudo ufw allow from 192.168.1.0/24 to any port 6379
❌ 不建议直接 ufw allow 6379
redis-cli
127.0.0.1:6379> ping
PONG
带密码:
redis-cli -a password ping
✅ 使用强密码
✅ 绑定内网 IP
✅ 启用 AOF
✅ 限制 maxmemory
✅ 禁止公网暴露 Redis
✅ 使用专用低权限用户(Debian 已默认 redis)
如果你有具体需求(比如:主从复制 / 集群 / Docker / 远程访问 / 性能调优),可以告诉我,我可以给你更针对性的配置示例。