搭建LNAMP环境(五)- PHP7源码安装Redis和Redis拓展

上一篇:搭建LNAMP环境(四)- 源码安装PHP7

一、安装Redis

1.创建redis用户组和用户

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

2.下载redis源码包,将源码包放到/usr/local/src/目录下

下载页面:http://redis.io/download
这里用的是redis-3.2.5.tar.gz
下载地址:http://download.redis.io/releases/redis-3.2.5.tar.gz

3.进入src/目录

cd /usr/local/src/

4.解压源码包

tar -zxf redis-3.2.5.tar.gz

5.进入redis源码目录,编译安装

cd redis-3.2.5/

make PREFIX=/usr/local/redis install

6.创建redis文件目录

mkdir -p /usr/local/redis/data
mkdir -p /usr/local/redis/etc
mkdir -p /var/run/redis
mkdir -p /var/log/redis

7.将配置文件放入redis/etc/目录

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

8.修改redis配置文件redis.conf

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

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

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持久化

10.修改redis目录权限

chown -R redis:redis /usr/local/redis
chown -R redis:redis /var/run/redis
chown -R redis:redis /var/log/redis

11.将redis命令加入环境变量,修改profile文件

vim /etc/profile

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

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

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

source /etc/profile

14.将redis服务脚本加入到init.d/目录,创建redisd文件

vim /etc/init.d/redisd

15.加入下面内容,保存退出

#!/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-server
CLIEXEC=$BASEDIR/bin/redis-cli PIDFILE=/var/run/redis/redis.pid
CONF="$BASEDIR/etc/redis.conf" function start() {
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo -n "Starting Redis server..."
daemon --user=redis --pidfile=$PIDFILE $EXEC $CONF
#$EXEC $CONF
echo
fi
}
function stop() {
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
#$CLIEXEC -p $REDISPORT shutdown
killproc -p $PIDFILE $EXEC -INT
echo -n "Waiting for Redis saving data & shutdown ."
while [ -x /proc/${PID} ]
do
sleep 1
echo -n "."
done
echo -e "\nRedis stopped"
fi
} case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $PIDFILE $EXEC
;;
restart)
stop
start
;;
*)
echo "Please use start or stop as first argument"
;;
esac

16.为redisd添加可执行权限

chmod +x /etc/init.d/redisd

17.将redis加入系统服务

chkconfig --add redisd

18.修改服务的默认启动等级

chkconfig redisd on

19.启动redis

service redisd start

二、PHP7安装Redis拓展

1.yum安装git

yum -y install git

2.进入src/目录

cd /usr/local/src/

3.git下载phpredis拓展

git clone https://github.com/phpredis/phpredis.git

4.进入phpredis/目录

cd phpredis/

5.默认clone的php5的redis扩展,需要切换到php7的分支

git checkout php7

6.编译安装拓展

phpize

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

make && make install

7.修改php.ini文件

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

8.添加redis.so扩展配置,保存退出

extension=redis.so

9.重启Apache或php-fpm

service httpd restart
service php-fpm restart

10. 在web目录下添加php文件,如/usr/local/apache/htdocs/redis.php 或 /usr/local/nginx/html/redis.php

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('class','toefl');
print_r($redis->get('class'));

访问URL,如:http://192.168.8.9/redis.php
页面显示正常,则配置成功

Redis安装完毕!

下一篇:搭建LNAMP环境(六)- PHP7源码安装MongoDB和MongoDB拓展

上一篇:windows下与linux下安装redis及redis扩展


下一篇:Zeller公式推导及C#代码示例(待完善)