编译安装工作:
#下载redis安装包 wget http://download.redis.io/releases/redis-4.0.10.tar.gz #解压安装包 tar -zxvf redis-4.0.10.tar.gz -C /usr/local/ #重命名为redis mv /usr/local/redis-4.0.10 /usr/local/redis #进入安装目录 cd /usr/local/redis #编译并安装 make && make install
修改redis.conf配置:
#进入配置文件所在目录 cd /usr/local/redis #开启守护进程模式(即能够后台运行) sed -i ‘s/daemonize\ no/daemonize\ yes/g‘ redis.conf #关闭protected-mode模式,使外部网络可以直接访问(生产慎用) sed -i ‘s/protected-mode\ yes/protected-mode\ no/g‘ redis.conf #设置redis连接密码 sed -i ‘s/#\ requirepass\ foobared/requirepass 123456/g‘ redis.conf #对过期事件进行通知发送(可用于token过期提醒) sed -i ‘s/notify-keyspace-events\ ""/notify-keyspace-events\ Ex/g‘ redis.conf
编写Redis启动脚本:
#创建redis脚本 vi /etc/init.d/redis #向脚本中写入以下内容:
#!/bin/bash # # author:liusha # description: Redis Server start|stop|restart|status PATH=/usr/local/bin:/sbin:/usr/bin:/bin # redis端口号 REDISPORT=6379 # redis进程id PIDFILE=/var/run/redis.pid # redis.conf所在目录的决定对路径 CONF="/usr/local/redis/redis.conf" # redis.cli所在目录的决定对路径 REDIS_CLI=/usr/local/redis/src/redis-cli # redis-server所在目录的决定对路径 REDIS_SERVER=/usr/local/redis/src/redis-server #启动服务 start() { if [ "$?"="0" ]; then echo "Redis is running..." fi if [ -f $PIDFILE ]; then echo "$PIDFILE exists, Redis Server is already running or crashed." else echo "Starting Redis Server..." $REDIS_SERVER $CONF fi } #停止服务 stop() { if [ ! -f $PIDFILE ]; then echo "$PIDFILE exists, Redis Server is not running." else PID=$(cat $PIDFILE) echo "Stopping..." $REDIS_CLI -p $REDISPORT SHUTDOWN sleep 2 while [ -x $PIDFILE ]; do echo "Waiting for Redis Server to shutdown..." sleep 1 done echo "Redis Server stopped" fi } #服务状态 status() { if [ "$?"="0" ]; then echo "Redis is running..." else if [ -f $PIDFILE ]; then echo "$PIDFILE exists, Redis Server is already running or crashed." else echo "Redis Server is not running." fi fi } #重启服务 restart() { stop sleep 1s start } case $1 in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) echo "Usage: /etc/init.d/redis {start|stop|restart|status}" >&2 exit 1 ;; esac
启动Redis服务:
#开启redis服务
chkconfig redis on
#启动redis服务
service redis start
注:要使远程能够登录访问,还需开启防火墙和配置安全组规则(阿里云服务器),可参考https://www.cnblogs.com/54hsh/p/13355413.html
远程登录验证结果: