RedHat5.8 环境下编译安装 Redis 并将其注册为系统服

系统环境:
$ cat /etc/issue
Red Hat Enterprise Linux Server release 5.8 (Tikanga)
Kernel \r on an \m

1. 下载安装 1.1 下载

官方下载地址:Redis.io/download
下载最新稳定版 redis-3.0.4.tar.gz,大小仅 1.3 MB。

1.2 安装

将下载的安装包放在用户目录下,如 /home/webapp,打算将其安装到 /opt/redis
$ sudo mkdir /opt/redis
$ cd /home/webapp
$ tar xzf redis-3.0.4.tar.gz
$ cd redis-3.0.4
$ sudo make PREFIX=/opt/redis install

之后查看 /opt/redis,下边只生成了一个目录,该目录下只有六个文件:
redis-benchmark  redis-check-dump  redis-sentinel
redis-check-aof  redis-cli         redis-server

安装完成。

2. 注册为系统服务 2.1 编辑服务脚本

查看安装包自带服务脚本:
$ cat /home/webapp/redis-3.0.4/utils/redis_init_script
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

将其拷贝到 /etc/rc.d/init.d 目录并重命名为 redis
$ sudo cp /home/webapp/redis-3.0.4/utils/redis_init_script /etc/rc.d/init.d/redis
然后依据 redis 的安装路径修改 /etc/rc.d/init.d/redis 为:
#!/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.

REDISPORT=6379
EXEC=/opt/redis/bin/redis-server
CLIEXEC=/opt/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/opt/redis/${REDISPORT}.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF &
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

注意红色字体部分:

chkconfigdescription 两行注释必须,缺一不可,否则 service redis does not support chkconfig

$EXEC $CONF &,后边的 &,表示将服务转到后台运行;

EXECCLIEXECCONF 等三处路径都要改。

2.2 复制配置文件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8fc4dfba0a17e9d675e67b017b9d5204.html