前言
自己买了个阿里云服务器,在安装redis之后,想要将redis注册为系统服务,并设置开机自启,走了些弯路,但最终弄好,记录下,也希望能帮到广大码农。
1.安装gcc
如果没有需要先进行安装,使用一下命令,但一般阿里云服务器都会有
yum install cpp
2.安装redis
去redis官网,https://redis.io/download,这步可以跟着官网的步骤走,随意找个路径。个人一般把安装的包放在/usr/local/下
$ wget http://download.redis.io/releases/redis-4.0.11.tar.gz
$ tar xzf redis-4.0.11.tar.gz
$ cp redis-4.0.11 /usr/local/redis
$ cd /usr/local/redis
$ make
3.启动redis
进入/redis/下,使用命令./src/redis-server就能启动redis,然后使用命令./src/redis-cli登录redis
./src/redis-server --启动redis
./src/redis-cli --登录redis
4.使用mkdir /etc/redis创建路径,将/redis下的redis.conf复制到/etc/redis
[root@izwz9aj4op8q3l4o3fvvo8z utils]# mkdir /etc/redis
[root@izwz9aj4op8q3l4o3fvvo8z redis]# cp redis.conf /etc/redis/6379.conf
使用vi /etc/redis/6379.conf找到daemonize no将其改为yes
5.将redis注册为系统服务
进入/utils路径,将redis_init_script复制到/etc/init.d/redisd
[root@izwz9aj4op8q3l4o3fvvo8z redis]# cd utils/
[root@izwz9aj4op8q3l4o3fvvo8z utils]# ll
total 76
-rw-rw-r-- 1 root root 593 Aug 4 06:44 build-static-symbols.tcl
-rw-rw-r-- 1 root root 1303 Aug 4 06:44 cluster_fail_time.tcl
-rw-rw-r-- 1 root root 1070 Aug 4 06:44 corrupt_rdb.c
drwxrwxr-x 2 root root 4096 Aug 4 06:44 create-cluster
-rwxrwxr-x 1 root root 2137 Aug 4 06:44 generate-command-help.rb
drwxrwxr-x 3 root root 4096 Aug 4 06:44 graphs
drwxrwxr-x 2 root root 4096 Aug 4 06:44 hashtable
drwxrwxr-x 2 root root 4096 Aug 4 06:44 hyperloglog
-rwxrwxr-x 1 root root 9567 Aug 4 06:44 install_server.sh
drwxrwxr-x 2 root root 4096 Aug 4 06:44 lru
-rw-rw-r-- 1 root root 1277 Aug 4 06:44 redis-copy.rb
-rwxrwxr-x 1 root root 1447 Sep 27 22:21 redis_init_script
-rwxrwxr-x 1 root root 1047 Aug 4 06:44 redis_init_script.tpl
-rw-rw-r-- 1 root root 1762 Aug 4 06:44 redis-sha1.rb
drwxrwxr-x 2 root root 4096 Aug 4 06:44 releasetools
-rwxrwxr-x 1 root root 3787 Aug 4 06:44 speed-regression.tcl
-rwxrwxr-x 1 root root 693 Aug 4 06:44 whatisdoing.sh
[root@izwz9aj4op8q3l4o3fvvo8z utils]# ^C
[root@izwz9aj4op8q3l4o3fvvo8z utils]# cp redis_init_script /etc/init.d/redisd
修改/etc/init.d/redisd中的内容,将# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database添加在#!/bin/sh之下
[root@izwz9aj4op8q3l4o3fvvo8z utils]# vi /etc/init.d/redisd
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO
REDISPORT=6379
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
6.添加开机启动
chkconfig redisd on
7.启动redis
service redisd start