ubuntu16 下安装redis 以及设置其为开机启动

1.下载redis安装包

sudo wget http://download.redis.io/releases/redis-3.2.6.tar.gz

2.解压

tar -zxvf  redis-3.2.6.tar.gz

3.会在当前目录下生成文件夹redis-3.2.6,我把它移动到了/usr/local/redis目录下:

ubuntu16 下安装redis 以及设置其为开机启动

4.如果没有安装gcc,需要先安装:下面编译make时候需要用到

进入到redis-3.2.6目录下,依次执行下面两条命令:

sudo make
sudo make install

5.

现在进入先前解压后得到的文件夹(我的在/usr/local/redis),复制配置文件redis.conf到/etc/redis/下,并用vi命令编辑该文件,将“daemonize no”修改为“daemonize yes”,即设置成作为后台进程运行,修改完成后保存退出。 
   
  进入到/usr/local/bin目录下,执行命令:(默认端口6379)

  redis-server /etc/redis/redis.conf
redis-cli -p 6379

  然后执行命令ping,若输出为pong,则证明服务成功启动。

ubuntu16 下安装redis 以及设置其为开机启动

执行quit命令退出,现在可以通过下面的命令查看到该进程:

2.设置为开机启动

编写脚本,在目录/etc/init.d/目录下面

vim /etc/init.d/redis

 #!/bin/sh
# chkconfig: 2345 10 90
# description: Start and Stop redis PATH=/usr/local/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis/redis.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
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE exists, process is not running."
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x $PIDFILE ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|fore-reload}"
exit 1
esac

添加脚本的执行权限

sudo chmod  777  /etc/init.d/redis

设置开机自动启动
       sudo update-rc.d redis defaults

报错:

insserv: warning: script 'K01redis' missing LSB tags and overrides
insserv: warning: script 'redis' missing LSB tags and overrides

解决办法:

1).insserv: warning: script 'K01redis' missing LSB tags and overrides

安装一个中文语言,系统就知道zh_CN.UTF-8了,这个时候用perl就不会报错了

 
sudo apt-get install language-pack-zh-hans

2)insserv: warning: script 'redis' missing LSB tags and overrides

修改目录/etc/init.d/下面的redis文件,在文件头部#!/bin/sh下面添加如下内容:

 
### BEGIN INIT INFO
# Provides: OSSEC HIDS
# Required-Start: $network $remote_fs $syslog $time
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: OSSEC HIDS
### END INIT INFO 设置后似乎不起作用:记几个命令: 手动启动命令:
  redis-server /etc/redis/redis.conf
密码进入:
redis-cli -h 192.168.111.128 -p 6379 -a 123456
上一篇:在ubuntu16下安装virtualenv+virtualenvwrapper


下一篇:BZOJ 3043: IncDec Sequence 差分 + 思维