安装redis和安装php的redis扩展

参考:
https://www.cnblogs.com/ccw869476711/p/11804017.html(主要的)
https://blog.csdn.net/gu_wen_jie/article/details/79413365
https://blog.csdn.net/soulandswear/article/details/81482527(laravel使用)
https://www.cnblogs.com/mrszhou/p/8087753.html(laravel使用)
https://www.cnblogs.com/tenghuan/p/4608888.html (redis遇到的问题)
https://www.cnblogs.com/liangyou666/p/9577468.html (redis自启动)

linux安装redis客户端(其实就是redis5安装,好了后挪点配置文件和启动到/usr/local/redis里面去)

 yum install -y tcl

流程:redis.io/download找官方redis5.0,下载,解压,进入/redis5/src,make distclean,然后直接make test,如果没问题直接make && make install
以上就把redis安装上去了,为了启动方便了配置方便我们还需要新建/usr/local/redis/etc和bin
最后
拷贝redis5/redis.conf到etc中来mv /usr/local/redis5/redis.conf /usr/local/redis/etc/
同时修改他下面的daemonize yes使后台运行和daemonize允许访问ip

cd /usr/local/redis5/src
mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server /usr/local/redis/bin/

执行:./redis-server ../etc/redis.conf//开启服务

/usr/local/redis/bin/redis-cli // 运行测试redis

cp /usr/local/redis5/utils/redis_init_script /etc/init.d/redis // 加到直接启动中
chmod 777 /etc/init.d/redis

安装predis扩展

wget https://codeload.github.com/edtechd/phpredis/zip/php7
unzip php7
cd redis-php7
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
vim php.ini 增加extension=redis.so
php -m // 检查,不一定准最好看phpinfo

安装phpredis扩展(传送门
这个是c写的,上面是php写的且官方推荐的,但这个效率更高,后来公司开始用这个了,还折腾了点时间

git clone https://github.com/phpredis/phpredis.git
cd phpredis
phpize
./configure
make && make install
// 重启php

laravel使用redis

先来个composer引入相关资源

 composer require predis/predis

redis的配置文件在:config/database.php,.env文件那边也有设置

'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD',null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],
    ],

最后再php代码测一下

 use Illuminate\Support\Facades\Redis;
。。。。。。
  public function redisTest(){
        Redis::set('name','xiaoming');
        $name = Redis::get('name');

        echo 'redis实验'.$name;die;
    }

make test报错:“src/redis-benchmark”: no such file or directory,执行:

make distclean
make
make test

顺便设置下开机自启吧,免得每次自己启动,麻烦🙂

vim /etc/init.d/redis // 先创建个启动文件,里面路径要注意哦

    #!/bin/bash
#chkconfig: 22345 10 90
#description: Start and Stop redis

REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/etc/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
            while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    *)
        echo "Please use start or stop or restart as first argument"
        ;;
esac

chmod +x /etc/init.d/redis

chkconfig --add redis
chkconfig redis on
chkconfig --list   //查看所有注册的脚本文件
posted @ 2019-01-16 16:41  蜗牛使劲冲  阅读(3)  评论(0编辑  收藏  举报  来源