redis自动安装脚本(只安装redis)

Redis自动安装部署

本文总共分为4个部分:

redis自动安装脚本目录结构说明;

redis自动安装脚本内容;

redis的操作系统服务脚本说明;

本文引用的资料链接地址。

说明:本文未涉及redis内存分配器部分的内容。

第一部分

Redis自动安装脚本目录结构说明:

# ls 查看当前目录包含以下3个文件

-- install_redis.sh        # redis自动安装脚本

-- redis-2.8.9.tar.gz      # redis安装包(需make,make test,make install)

-- redis_service_script   # redis 操作系统服务脚本(用于配置redis服务开机自启动,以及redis服务管理命令)

第二部分

###########################redis自动安装脚本内容开始(install_redis.sh)######################

#!/bin/bash

# Version:  v1.0 by JiangYanguo on 2014-05-14
# Desc:     auto make, make install and configure redis


REDIS_CONF="/etc/redis.conf"

# 检查redis的安装包是否存在
if [ "`ls redis-*.tar.gz >/dev/null 2>&1 ; echo $?`" -ne 0 ]
then
   echo "Error: redis*.tar.gz file doesn't exists."
   exit
else
  REDIS_PACKAGE="`ls -ltr redis-*.tar.gz | head -1 | awk '{print $NF}' `"
fi

# 安装redis,make,make test,make install
# install redis

echo "Info: install redis..."

tar zxf ${REDIS_PACKAGE}

REDIS_DIR="`ls -ltr | egrep '^d' | awk '{print $NF}'`"

cd ${REDIS_DIR}

pwd

make

make test

if [ "$?" -nt 0 ];then
 echo "Some dependent packages are needed."
 exit
fi

make install


echo "Info: set ${REDIS_CONF} ..."

cd ..

# 设置redis配置文件,data目录为/home/redis
# set /etc/redis.conf
cp redis.conf /etc/

sed -i 's/^daemonize\(.*\)/#daemonize\1/g'  ${REDIS_CONF}
sed -i '2 adaemonize yes' ${REDIS_CONF}

sed -i 's/^dir\(.*\)/#dir\1/g' ${REDIS_CONF}
sed -i '2 adir \/home\/redis\/' ${REDIS_CONF}


# set memory allocate method
[ "`egrep 'vm.overcommit_memory=1' /etc/sysctl.conf | wc -l`" -eq 0 ] && echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
/sbin/sysctl -p

# add redis user
useradd redis

# set redis data dir
mkdir -p /home/redis/
chown -R redis.redis /home/redis

#设置redis服务开机自启动,也是redis的管理服务
# set redis service
echo "Info: set redis service ..."
cp redis_service_script /etc/init.d/redis
chmod +x /etc/init.d/redis
chkconfig --add redis
chkconfig redis on

#
echo "Info: finish installing redis."
echo ""

########################## redis自动安装脚本内容结束 (install_redis.sh)#####################################

第三部分

########################## redis的操作系统服务脚本内容开始(redis_service_script)#########################################

#!/bin/bash

# redis - Startup script for redis

# chkconfig: 2345 85 152345 85 15
# description: startup script fZ�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vciByZWRpczxicj4KIyBwcm9jZXNzbmFtZTogcmVkaXMtc2VydmVyPGJyPgojIGNvbmZpZzogL2V0Yy9yZWRpcy5jb25mPGJyPgojIHBpZGZpbGU6IC92YXIvcnVuL3JlZGlzLnBpZDxicj4KIyBkYXRhZmlsZXBhdGg6IC9ob21lL3JlZGlzLzxicj4KPGJyPgojPGJyPgpSRURJU1BPUlQ9NjM3OTxicj4KRVhFQz0vdXNyL2xvY2FsL2Jpbi9yZWRpcy1zZXJ2ZXI8YnI+ClJFRElTX0NMST0vdXNyL2xvY2FsL2Jpbi9yZWRpcy1jbGk8YnI+CiAgIDxicj4KUElERklMRT0mcXVvdDsvdmFyL3J1bi9yZWRpcy5waWQmcXVvdDs8YnI+CkNPTkY9JnF1b3Q7L2V0Yy9yZWRpcy5jb25mJnF1b3Q7PGJyPgo8YnI+CiMjIHN0YXJ0PGJyPgpmdW5jdGlvbiBzdGFydCgpezxicj4KICBlY2hvICZxdW90O0luZm86IHN0YXJ0IHJlZGlzLi4uJnF1b3Q7PGJyPgogICBpZiBbIC1mICRQSURGSUxFIF08YnI+CiAgIHRoZW48YnI+CiAgICAgICBlY2hvICZxdW90OyRQSURGSUxFIGV4aXN0cywgcHJvY2VzcyBpcyBhbHJlYWR5IHJ1bm5pbmcgb3IgY3Jhc2hlZCZxdW90Ozxicj4KICAgZWxzZTxicj4KICAgICAgIGVjaG8gJnF1b3Q7U3RhcnRpbmcgUmVkaXM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cjxicj4KIyMgaW5mbzxicj4KZnVuY3Rpb24gaW5mbygpezxicj4KICBlY2hvICZxdW90O0luZm86IHJlZGlzIGluZm8uLi4mcXVvdDs8YnI+CiAgaWYgWyAtZiAkUElERklMRSBdO3RoZW48YnI+CiAgICAgICRSRURJU19DTEkgaW5mbzxicj4KICBlbHNlPGJyPgogICAgIGVjaG8gJnF1b3Q7JFBJREZJTEUgZG9lcyBub3QgZXhpc3QsIHByb2Nlc3MgaXMgbm90IHJ1bm5pbmcmcXVvdDs8YnI+CiAgZmk8YnI+Cn08YnI+Cjxicj4KIyMgc3RhdHVzPGJyPgpmdW5jdGlvbiBzdGF0dXMoKXs8YnI+CiAgUkVESVNfU0VSVkVSX0NPVU5UPSZxdW90O2BuZXRzdGF0IC1ubHRwIA==" grep redis-server | wc -l`"
 if [ -f $PIDFILE ] && [ "${REDIS_SERVER_COUNT}" -gt 0 ]
 then
     echo "Info: redis is running."
 elif [ ! -f $PIDFILE ]
 then
     echo "$PIDFILE does not exist, process is not running"
 elif [ "${REDIS_SERVER_COUNT}" -eq 0 ]
 then
     echo "Redis is not listening any port."
 fi

}

case "$1" in
 start)
   start
   ;;
 stop)
   stop
   ;;
 restart)
   restart
   ;;
 info)
   info
   ;;
 status)
   status
   ;;
 *)
   echo "Usage: $0 {start|stop|restart|info|status}"
   
esac

########################## redis的操作系统服务脚本内容结束(redis_service_script)#########################################

第四部分

本文参考的资料链接地址:

Redis安装: http://www.cnblogs.com/zhuhongbao/archive/2013/06/04/3117997.html

Redis安装: http://blog.chinaunix.net/uid-790245-id-3766268.html

转自红客联盟: http://www.2cto.com/os/201405/300925.html

你可能感兴趣的:(redis)