Home > Database > Redis > body text

How to set up php environment redis

WBOY
Release: 2023-06-02 23:25:05
forward
784 people have browsed it

一、下载

1、http://download.redis.io/releases/redis-6.0.9.tar.gz下载到本地,文件传输上传到 /usr/local/src/
2、wget -P /usr/local/src/ http://download.redis.io/releases/redis-6.0.9.tar.gz

二、解压,编译,安装

> cd /usr/local/src
> tar xzf tar xzf redis-6.0.9.tar.gz
> cd redis-6.0.9
> make
Copy after login

编译完会默认将软件安装在当前目录,这里将整个解压缩文件移至local目录

mv /usr/local/src/redis-6.0.9 /usr/local/redis-6.0.9
Copy after login

三、更改配置 /usr/local/redis-6.0.9/redis.conf
1、更改默认端口为16379
2、设置redis服务以守护进程运行
3、设置redis可供远程访问
4、设置密码

> vim /usr/local/redis-6.0.9/redis.conf # 打开redis服务启动配置文件,
# vim中命令模式下输入“/关键字”    
# 类似于windows的文件中查询,字符n代表下一个,N代表上一个。
# 设置参数值 no 为 yes 并在命令模式下输入 “:wq” 代表保存并退出
> /port # 匹配到端口 改为16379,该配置是用来限制端口访问的
> /pid  #匹配pid文件,改为redis_16379,该配置是用来存储pid信息,主要是为了命名风格统一,与端口一致,如果不改默认端口,这里也不用改
> /daemonize # 匹配到守护进程配置,设置yes会以守护进程模型常驻
> /bind 127.0.0.1 #注释掉该行,或者注释掉所有的bind配置,改为bind 0.0.0.0,意味着允许任何ip访问,该配置是用来限制IP访问的
> /protected-mode yes #把yes改为no
> /requirepass #去除前面的#打开注释,并更改默认的密码为自定义密码【最好复杂点,以免暴力破解】
Copy after login

redis在开放远程访问,又不设置密码的情况下,相当于谁都可以访问redis服务器,且非常容易被服务器探针攻击,被注入挖矿木马或者勒索木马,如果服务器没有备份基本GG,所以
开放远程访问,一定要更改默认密码,即requirepass参数后面的字符串
开放远程访问,一定要更改默认密码,即requirepass参数后面的字符串
开放远程访问,一定要更改默认密码,即requirepass参数后面的字符串

四、设置开机自启动

vim /etc/init.d/redis
Copy after login

将如下配置复制粘贴保存

#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
#chkconfig: 2345 80 90
#description:auto_run
REDISPORT=16379
EXEC=/usr/local/redis-6.0.9/src/redis-server
CLIEXEC=/usr/local/redis-6.0.9/src/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis-6.0.9/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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC  -p $REDISPORT shutdown
                # 有设置密码要加 -a “密码”  参数,如下
                # $CLIEXEC -a "自定义的密码" -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac
Copy after login

设置命令可被执行

> chmod -R 0777 /etc/init.d/redis
Copy after login

检验自启动脚本

> service redis start # 检验服务启动
> service redis stop # 检验服务关闭
// 均正常提示
> chkconfig redis on # 设置开机自动执行redis开机自启动脚本
> reboot # 重启,ssh重连
# 重启后
> netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      839/redis-server 0. 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1056/sshd
Copy after login

验证开机自启动成功
安装完毕

五、服务器上使用

> /usr/local/redis-6.0.9/src/redis-cli -p 16379 -a "配置文件里设置的密码"
Copy after login

The above is the detailed content of How to set up php environment redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template