CentOS7.3安装Redis5.0.5并安装PHP-redis扩展

一、下载

官网:https://redis.io/download   我们下载 Stable 稳定版

# cd /mydata
# wget http://download.redis.io/releases/redis-5.0.5.tar.gz

最后出现
100%[==============================================================>] 1,975,750   1.06MB/s   in 1.8s   
下载完成

二、安装redis5.0.5

1、安装到/usr/local/redis5.0.5/目录下

# cd /mydata/
# tar -zxvf redis-5.0.5.tar.gz -C /usr/local/    //直接解压到/usr/local/目录下

2、编译安装

# cd /usr/local/redis-5.0.5/
# make            //或者 make MALLOC=libc  

最后出现
Hint: It's a good idea to run 'make test' ;)
make[1]: Leaving directory `/usr/local/redis-5.0.5/src'
说明编译成功

继续
# cd ./src/             // 或者不用进入此目录,直接执行make install,也会自动进入此目录,
# make install          // 将此./src目录下的文件加到/usr/local/bin目录
最后出现:
Hint: It's a good idea to run 'make test' ;)
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/usr/local/redis-5.0.5/src'

OK安装完成
此时查看
# ls  -al /usr/local/bin/
已经存在了:
......
-rwxr-xr-x 1 root root 4366592 May 24 18:40 redis-benchmark
-rwxr-xr-x 1 root root 8111800 May 24 18:40 redis-check-aof
-rwxr-xr-x 1 root root 8111800 May 24 18:40 redis-check-rdb
-rwxr-xr-x 1 root root 4806824 May 24 18:40 redis-cli
lrwxrwxrwx 1 root root      12 May 24 18:40 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 8111800 May 24 18:40 redis-server

如果make 报错,查看是否安装gcc  如果没有安装,使用 yum -y install gcc gcc-c++ 安装即可
3、检测是否安装成功

# cd /usr/local/redis-5.0.5/src/
# ./redis-server

出现下图 

说明已经安装成功了,ctrl+c退出。

三、配置redis服务器(默认无密码)

1、配置以后台方式启动

# vim /usr/local/redis-5.0.5/redis.conf
大约136行
daemonize no   将值改为yes 保存退出

2、指定redis.conf文件启动

#cd /usr/local/redis-5.0.5/src/
# ./redis-server   ../redis.conf                //临时启动服务器,(如果上一步不修改配置文件,这里加个& 也表示后台启动)     
出现
4821:C 24 May 2019 19:33:02.970 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4821:C 24 May 2019 19:33:02.970 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=4821, just started
4821:C 24 May 2019 19:33:02.970 # Configuration loaded

说明启动成功

测试:
# ./redis-cli 
127.0.0.1:6379> set username 'niuxitong'
OK
127.0.0.1:6379> get username
"niuxitong"
127.0.0.1:6379>

3、设置开启重启

关闭redis服务进程

# ps -aux | grep redis        //查看redis进程
root      4822  0.0  0.7 153892  7640 ?        Ssl  19:33   0:00 ./src/redis-server 127.0.0.1:6379
root      5557  0.0  0.0 112708   972 pts/0    R+   19:41   0:00 grep --color=auto redis
使用 kill -9 命令杀死此进程
# kill -9 4822
# kill -9 5557

# 删除此进程文件(如果有)
# rm -f /var/run/redis_6379.pid       #只要启动过,就会有此文件存在

# netstat  -nlpt    查看端口号是否还存在6379

设置开启重启

# cd /usr/local/redis-5.0.5/
# mkdir /etc/redis                        //在/etc目录下新建redis目录
# cp ./redis.conf  /etc/redis/6379.conf         //将redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf    

# cp ./utils/redis_init_script /etc/init.d/redisd    //将redis的启动脚本复制一份放到/etc/init.d目录下,并重命名为redisd

# cd /etc/init.d/
# chkconfig  --add  redisd
# chkconfig  redisd on  

# 启动redis服务器
# service redisd  start


#任意为执行
# redis-cli 
即可进入
# service redisd  stop 关闭redis服务

四、扩展:配置redis密码服务

1、配置redis密码为niu123456

# vim /etc/redis/6379.conf
# 大约507行  找到 # requirepass foobared

去掉前面的#号,并修改为 requirepass niu123456

2、修改启动脚本

# vim /etc/init.d/redisd
大约39行,在执行停止命令的程序中
把 
$CLIEXEC -p $REDISPORT shutdown 
修改为:
$CLIEXEC -a "niu123456" -p $REDISPORT shutdown

3、客户端访问需要设置auth 密码

[root@localhost ~]# redis-cli 
127.0.0.1:6379> auth niu123456
OK
127.0.0.1:6379>

五、安装php-redis扩展

1、PHP官方下载redis扩展
http://pecl.php.net/package/redis

# cd /mydata
# wget http://pecl.php.net/get/redis-5.0.2.tgz   --下载stable稳定版
最后出现:
100%[==============================================>] 
下载完成

2、使用phpize编译安装
通过查找,我们的phpize位于/usr/local/php/bin/phpize 下,并且php-config也位于此目录下

# tar -zxvf redis-4.3.0.tgz
# cd ./redis-4.3.0
# /usr/local/php/bin/phpize            //使用phpize生成安装文件
出现:
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731

这时此目录生成了configure文件和其它一些文件


# ./configure --with-php-config=/usr/local/php/bin/php-config     指定PHP配置路径
最后出现
......
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h

继续:
# make 
出现:
......
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

继续:
# make install
出现:
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/

查看PHP扩展目录
# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/ 
出现:
-rwxr-xr-x 1 root root 3584228 May  7 01:12 opcache.a
-rwxr-xr-x 1 root root 1976936 May  7 01:12 opcache.so
-rwxr-xr-x 1 root root 2390504 May 27 16:07 redis.so         //redis.so就是我们要的PHPredis扩展

OK安装完成

3、配置php.ini

# vim /etc/php.ini            //PHP配置文件
在最后一行添加如下一行:

extension=redis.so

# service php-fpm  restart    //重启php
# php  -m                     //查看PHP扩展
能看到:
...
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis        <---------------redis扩展安装成功  
Reflection
session
shmop

或者使用phpinfo()查看

OK PHP已经可以支持redis了。

4、通过php-redis程序测试 
创建redis.php文件到根目录

# vim redis.php
<?php
header("Content-Type:text/html; charset=utf-8");
error_reporting(E_ERROR | E_PARSE); 
@set_time_limit(0);  

$redis = new Redis();
$redis->connect('127.0.0.1', 6379) or die('Redis连接失败1,是否开启了redis-server服务?');
//$redis->auth("niu123456");  //如果有密码就启用此命令
$redis->set('username', '看到此变量的值,说明phpredis已经成功!');
echo $redis->get('username');
?>

浏览可以看到: OK PHP可以使用redis了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值