美文网首页
Ubuntu配置Redis并设置为开机启动

Ubuntu配置Redis并设置为开机启动

作者: Yvonne_ft_more | 来源:发表于2017-01-10 17:24 被阅读589次
    莫奈-阿让特伊帆船
    • 安装
      在Redis官网 (https://redis.io/download) 下载最新的稳定版本
      wget http://download.redis.io/releases/redis-3.2.6.tar.gz
      tar xzf redis-3.2.6.tar.gz
      cd redis-3.2.6
      make

    • 配置Redis
      打开redis-3.2.6文件夹下redis.conf,修改如下:
      daemonize no 改为 daemonize yes
      logfile "" 改为 logfile "/var/log/redis/redis.log"

    • 设置开机启动
      在/etc/init.d/文件夹下创建一个redis文件,输入以下:

    #!/bin/sh
    #Configurations injected by install_server below....
    
    EXEC=/tmp/redis-3.2.6/src/redis-server   # redis-server的路径
    CLIEXEC=/tmp/redis-3.2.6/src/redis-cli   # redis-cli的路径
    PIDFILE=/var/run/redis_6379.pid  # redis.conf里有这个参数,将其复制过来
    CONF="/tmp/redis-3.2.6/redis.conf"  # redis.conf的路径
    REDISPORT="6379"  # 端口
    ###############
    # SysV Init Information
    # chkconfig: - 58 74
    # description: redis is the redis daemon.
    ### BEGIN INIT INFO
    # Provides: redis
    # Required-Start: $network $local_fs $remote_fs
    # Required-Stop: $network $local_fs $remote_fs
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Should-Start: $syslog $named
    # Should-Stop: $syslog $named
    # Short-Description: start and stop redis
    # Description: Redis daemon
    ### END INIT INFO
    
    
    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
            ;;
        status)
            PID=$(cat $PIDFILE)
            if [ ! -x /proc/${PID} ]
            then
                echo 'Redis is not running'
            else
                echo "Redis is running ($PID)"
            fi
            ;;
        restart)
            $0 stop
            $0 start
            ;;
        *)
            echo "Please use start, stop, restart or status as first argument"
            ;;
    esac
    

    然后将改文件改为可执行
    chmod +x redis
    将该执行文件加入开机启动
    update-rc.d redis defaults

    • 启动redis
      /etc/init.d/redis start
    矢志不渝的愚者

    相关文章

      网友评论

          本文标题:Ubuntu配置Redis并设置为开机启动

          本文链接:https://www.haomeiwen.com/subject/pezkbttx.html