系統環境:
$ 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
表明服務已啟動。