1. 确保Redis已安装

在设置Redis服务开机自启动之前,首先要确保Redis已经成功安装并可正常运行。如果还没有安装Redis,请按照Redis官方文档进行安装步骤。

2. 配置Redis服务

要设置Redis服务开机自启动,需要对Redis配置文件进行相应的设置:

sudo vi /etc/redis/redis.conf

在打开的配置文件中,查找以下两行:

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal the parent process, so redis
# can be run as a non-root process but supervised with privileges.
supervised no

将supervised的值从"no"改为"systemd":

supervised systemd

保存并关闭配置文件。

3. 设置Redis服务开机自启动

接下来,需要创建一个Redis服务配置文件,使Redis在系统启动时自动启动:

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

在打开的文件中,输入以下内容:

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

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

[Install]
WantedBy=multi-user.target

保存并关闭配置文件。

4. 启用Redis服务开机自启动

最后,需要启用Redis服务开机自启动:

sudo systemctl enable redis

现在,Redis已经被设置为开机自启动。可以通过以下命令检查Redis服务的状态:

sudo systemctl status redis

如果输出信息中显示Redis服务正在运行,那么说明设置成功。

通过以上步骤,您已经成功设置了Redis服务的开机自启动。