一、安装redis
下载安装redis,官网:https://redis.io/download

wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar xzf redis-3.2.8.tar.gz
cd redis-3.2.8
make
没有报错的话,redis 就安装好了。

配置环境变量

    echo 'export PATH=$PATH:/usr/local/redis/bin' >> /etc/profile
    . /etc/profile

配置 redis.conf

cd redis-3.2.3
// 进入redis 源码包目录
mkdir /usr/local/redis/etc/
cp redis.conf /usr/local/redis/etc/
cd /usr.local/redis/etc/
vim redis.conf

    //redis.conf
    daemonize yes //后台运行
    pidfile /var/run/redis/redis.pid
    port 6379
    tcp-backlog 128    // tcp队列长度,默认为512,CentOS系统默认为128,此值不应该大于操作系统的该值
    logfile "/var/log/redis/redis.log"
    dir /var/redisdb    // redis数据存储目录
    // 其他参数目前就不需要了

    配置redis的服务脚本

    useradd -r -s /sbin/nologin -M redis
    mkdir /var/run/redis
    mkdir /var/log/redis
    mkdir /var/redisdb
    chown redis:redis /var/run/redis
    chown redis:redis /var/log/redis
    chown redis:redis /var/redisdb

    #!/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

二、安装php的redis模块 phpredis

为什么要用phpize,我们在安装php时:

后面的参数是我们要的模块,但是随着工作的需要,我们还要在加些模块,又不想重新编译php,这些我们就可以
用phpize了。

1.下载phpredis
#wget下载github上的文件
wget https://github.com/nicolasff/phpredis/archive/master.zip

2.开始编译phpredis扩展
A #unzip master.zip
B #解压后进入 cd phpredis-master
(#phpize 执行此命令
//I,先find / -name 'phpize' ,如果找到就完成命令执行如
// /opt/app/php/bin/phpize
//或者
//检查php扩展,出错的话:bash: phpize: command not found
//yum install php-devel
//我是安装php56u的所以:yum install php56u-devel

C #phpize
Configuring for:

            PHP Api Version:         20041225

            Zend Module Api No:      20050922

            Zend Extension Api No:   220051025

D config配置
            #./configure --with-php-config=/usr/local/bin/php-config (此目录为php安装的php-config,find找下)
                make
                make install
        成功后出现:

        Installing shared extensions:     /usr/lib/php/modules/

        #ls /usr/lib/php/modules/

        dbase.so  ldap.so  pdo.so  pdo_sqlite.so  phpcups.so  redis.so  snmp.so
        有需要的模块名称说明安装完毕

3.安装配置phpredis扩展
修改php.ini配置文件

            vim /etc/php.ini (找到php安装目录下这个配置文件)
            extention="redis.so"
            //添加这一行扩展配置

4, 重启php-fpm服务
service php-fpm restart 

5 .测试扩展库是否正常运行

            //在git clone下来的phpredis文件夹
            php tests/TestRedis.php --class Redis