以下是在 CentOS 7.6 下使用 systemd 配置 Redis 后台运行的步骤:

  1. 安装 Redis

在 CentOS 7.6 中,可以使用以下命令安装 Redis:

sudo yum install redis
  1. 配置 Redis

编辑 Redis 配置文件 /etc/redis.conf,找到以下两行:

#daemonize no
#pidfile /var/run/redis.pid

daemonize 修改为 yes,并将 pidfile 的注释去掉,如下所示:

daemonize yes
pidfile /var/run/redis.pid
  1. 创建 systemd 服务文件

使用以下命令创建 Redis 的 systemd 服务文件 /etc/systemd/system/redis.service

sudo nano /etc/systemd/system/redis.service

将以下内容复制到文件中并保存:

[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis.conf
ExecStop=/usr/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target
  1. 启动 Redis

使用以下命令启动 Redis 服务:

sudo systemctl start redis
  1. 验证 Redis 是否在后台运行

使用以下命令验证 Redis 是否在后台运行:

sudo systemctl status redis

如果 Redis 正在后台运行,输出应该类似于:

● redis.service - Redis In-Memory Data Store
   Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-06-15 14:29:51 UTC; 1 day  ago
 Main PID: 12345 (redis-server)
    Tasks: 4 (limit: 4915)
   Memory: 2.5M
   CGroup: /system.slice/redis.service
           └─12345 /usr/bin/redis-server 127.0.0.1:6379

现在,Redis 应该在 CentOS 7.6 中以后台模式运行了。

CentOS 7.6 下 Redis 后台运行配置教程

原文地址: https://www.cveoy.top/t/topic/mjc6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录