RedHat5.8 环境下编译安装 Redis 并将其注册为系统服务
系统环境:
$ cat /etc/issue
Red Hat Enterprise Linux Server release 5.8 (Tikanga)
Kernel \r on an \m
1. 下载安装
1.1 下载
官方下载地址:http://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
注意红色字体部分:
- chkconfig、description 两行注释必须,缺一不可,否则 service redis does not support chkconfig;
- $EXEC $CONF &,后边的 &,表示将服务转到后台运行;
- EXEC、CLIEXEC、CONF 等三处路径都要改。
2.2 复制配置文件
将 /home/webapp/redis-3.0.4 下提供的 redis.conf 默认配置文件拷贝到 redis 安装根目录下并重命名为 6379.conf:
$ cp /home/webapp/redis-3.0.4/redis.conf /opt/redis/6379.conf
2.3 环境变量设置
追加以下内容到 /etc/profile 文件:
/opt/redis/bin:/sbin:$PATH
执行 . /etc/profile 以使配置立即生效并使用 echo $PATH 验证之。
2.4 服务注册
$ chkconfig --add redis
No news is good news,没有任何输出证明服务注册成功。
如果提示 chkconfig command not found,首先 rpm -q chkconfig 检查是否安装了 chkconfig,已安装的话检查 PATH 里是否有 /sbin。
3. 服务启动及验证
$ sudo service redis start
服务启动,执行
$ redis-cli ping
PONG
表明服务已启动。
参考资料
下面关于Redis的文章您也可能喜欢,不妨参考下:
Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里