欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Linux、Nginx、php7 redis扩展安装教程

程序员文章站 2022-03-16 16:31:39
cat > /etc/profile.d/redis.sh << EOF export REDIS_HOME=/usr/local/redis export...

cat > /etc/profile.d/redis.sh << EOF

export REDIS_HOME=/usr/local/redis

export PATH=$PATH:$REDIS_HOME/bin’

EOF

. /etc/profile

源码安装redis

创建用户和组

groupadd redis

useradd -r -g redis -s /sbin/nologin -M redis

安装redis

cd /usr/local/src/

wget https://download.redis.io/releases/redis-3.2.5.tar.gz

tar -zxvf redis-3.2.5.tar.gz

cd redis-3.2.5/

make PREFIX=/usr/local/redis

make install

创建目录,授权

mkdir -p /usr/local/redis/data

mkdir -p /usr/local/redis/etc

mkdir -p /var/run/redis

mkdir -p /var/log/redis

chown -R redis:redis /usr/local/redis

chown -R redis:redis /var/run/redis

chown -R redis:redis /var/log/redis

配置文件

cp redis.conf /usr/local/redis/etc/

vim /usr/local/redis/etc/redis.conf

tcp-backlog 128   #tcp队列长度,CentOS系统默认为128,此值不应该大于操作系统的该值

daemonize yes    #改为yes,保证redis可以后台运行

pidfile /var/run/redis/redis.pid #redis pid

logfile “/var/log/redis/redis.log” #redis日志

dir /usr/local/redis/data/ #redis数据存储目录

—— 可选配置 ——

slaveof 192.168.1.66 6379      #主从复制,在副机上配置,例:IP 192.168.1.66,端口 6379

save 60 10000 #快照,例:60s内10000个更改就保存

appendonly yes #AOF持久化

添加环境变量

cat > /etc/profile.d/redis.sh << EOF

export REDIS_HOME=/usr/local/redis

export PATH=$PATH:$REDIS_HOME/bin’

EOF

. /etc/profile

或者

vim /etc/profile

修改为下面内容,保存退出

PATH=/usr/local/mysql/bin:/usr/local/php/bin:/usr/local/redis/bin:$PATH

使/etc/profile里的配置立即生效

source /etc/profile

创建服务

vim /etc/init.d/redisd

添加如下内容

!/bin/sh

#

chkconfig: 35 84 16

Simple Redis init.d script conceived to work on Linux systems

as it does use of the /proc filesystem.

. /etc/rc.d/init.d/functions

. /etc/sysconfig/network

[ “$NETWORKING” = “no” ] && exit 0

REDISPORT=6379

BASEDIR=/usr/local/redis

EXEC=BASEDIR/bin/redis?serverCLIEXEC=BASEDIR/bin/redis-cli

PIDFILE=/var/run/redis/redis.pid

CONF=”$BASEDIR/etc/redis.conf”

function start() {

if [ -f PIDFILE]thenecho“PIDFILE exists, process is already running or crashed”

else

echo -n “Starting Redis server…”

daemon –user=redis –pidfile=PIDFILEEXEC CONF

#EXEC CONF

echo

fi

}

functionstop(){

if[!-fPIDFILE ]

then

echo “PIDFILEdoesnotexist,processisnotrunning”elsePID=(cat PIDFILE)

echo“Stopping…”

#CLIEXEC -p REDISPORTshutdownkillproc?pPIDFILE EXEC-INT

echo-n“WaitingforRedissavingdata&shutdown.”

while[-x/proc/{PID} ]

do

sleep 1

echo -n “.”

done

echo -e “\nRedis stopped”

fi

}

case “1”instart)start;;stop)stop;;status)status?pPIDFILE $EXEC

;;

restart)

stop

start

;;

*)

echo “Please use start or stop as first argument”

;;

esac

为redisd添加可执行权限

chmod +x /etc/init.d/redisd

将redis加入系统服务

chkconfig –add redisd

启动redis

service redisd start

php-redis扩展安装

wget https://101.96.10.64/pecl.php.net/get/redis-3.1.3.tgz

tar zxvf redis-3.1.3.tgz

cd redis-3.1.3

/usr/local/php/bin/phpize

./configure –with-php-config=/usr/local/php/bin/php-config

make && make install

vim /usr/local/php/etc/php.ini

添加extension = redis.so

service php-fpm restart

php -m

查看是否支持redis

测试扩展库是否正常运行

php tests/TestRedis.php –class Redis