Ubuntu安装redis并将其设置为服务

1. http://redis.io/ 下载稳定版redis

2. 编译redis

tar -zxvf redis-xxx.tar.gz
cd redis-xxx
make
sudo make install

3. 修改配置文件

vi redis.conf

daemonize yes
dir   /data1/data/redis/  # 设置你自己的持久化目录
logfile "/var/log/redis/redis.log" # 设置你自己的log目录, 目录必须时已存在的
#其他优化配置自行百度

4. 移动配置文件到/etc目录

sudo cp redis.conf /etc/

5. 设置redis 自启动

1. 查找redis-server 和redis-cli 位置
$ whereis redis-server
$ redis-server: 你的redis-server所在路径
$ whereis redis-cli
$ redis-cli: 你的redis-cli所在路径
2. 编写脚本
sudo vi /etc/init.d/redis
#!/bin/sh
#Configurations injected by install_server below....
EXEC=/usr/local/bin/redis-server   # redis-server的路径
CLIEXEC=/usr/local/bin/redis-cli   # redis-cli的路径
PIDFILE=/var/run/redis_6379.pid  # redis.conf里有这个参数,将其复制过来
CONF="/etc/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

3. 保存退出,设置权限

sudo chmod +x /etc/init.d/redis

4. 设置redis 为开机自启动

# 设为开机启动:
$ chkconfig redis on
# 打开redis命令:
$ service redis start
# 关闭redis命令:
$ service redis stop
# 取消开机启动:
$ chkconfig redis off
    原文作者:Alex_Paul
    原文地址: https://www.jianshu.com/p/940244687eaf
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞