linux php7 安装redis扩展

环境介绍:我的服务器是阿里云的centos主机,php 7.0.14

1 安装和配置redis

   参考方案 点击打开链接

  1.1 下载redis 

   进入软件安装目录

[php] view plain copy
 
  1. cd /usr/local/   

   下载最新版的redis

[php] view plain copy
 
  1. wget  http://download.redis.io/redis-stable.tar.gz   


   1.2 编译安装

[html] view plain copy
 
  1. tar xvzf redis-stable.tar.gz   
[html] view plain copy
 
  1. cd redis-stable  
[html] view plain copy
 
  1. make  
[html] view plain copy
 
  1. make install  

   
   可能遇到的问题

[html] view plain copy
 
  1. 如果make出现了异常:    
  2.     1)make[2]: cc: Command not found    
  3.        异常原因:没有安装gcc    
  4.        解决方案:yum install gcc-c++    
  5.     2)zmalloc.h:51:31: error: jemalloc/jemalloc.h: No such file or directory    
  6.     异常原因:一些编译依赖或原来编译遗留出现的问题    
  7.     解决方案:make distclean。清理一下,然后再make。    
  8.     
  9. 在make成功之后,需要make test 在make test 出现异常    
  10.     1) couldn't execute "tclsh8.5": no such file or directory    
  11.         异常原因:没有安装tcl    
  12.         解决方案:yum install -y tcl。  


  redis 功能说明

[html] view plain copy
 
  1. make命令执行完成后,会在src目录下生成5个可执行文件,分别是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump,它们的作用如下:    
  2. redis-server:Redis服务器的daemon启动程序    
  3. redis-cli:Redis命令行操作工具。当然,你也可以用telnet根据其纯文本协议来操作    
  4. redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能    
  5. redis-check-aof:更新日志检查    
  6. redis-check-dump:用于本地数据库检查    

  1.3 redis 配置

[html] view plain copy
 
  1. cp redis-server /usr/local/bin/    
  2.     cp redis-cli /usr/local/bin/    
  3.     然后新建目录,存放配置文件    
  4.     mkdir /etc/redis    
  5.     mkdir /var/redis    
  6.     mkdir /var/redis/log    
  7.     mkdir /var/redis/run    
  8.     mkdir /var/redis/6379   
[html] view plain copy
 
  1. 在redis解压根目录中找到配置文件模板,复制到如下位置。    
  2.     cp redis.conf /etc/redis/6379.conf通过vim命令修改    
  3.     daemonize yes    
  4.     pidfile /var/redis/run/redis_6379.pid    
  5.     logfile /var/redis/log/redis_6379.log    
  6.     dir /var/redis/6379    
  7.     最后运行redis:    
  8.     redis-server /etc/redis/6379.conf  
[html] view plain copy
 
  1. netstat –atln  

查看端口状态

有了127.0.0.1:6379这个说明redis已经成功运行了.

  1.4 redis操作尝试

[html] view plain copy
 
  1. redis 127.0.0.1:6379> set foo bar    
  2. OK    
  3. redis 127.0.0.1:6379> keys *    
  4. 1) "foo"    
  5. redis 127.0.0.1:6379> get foo    
  6. "bar"    
  7. redis 127.0.0.1:6379> del foo  



2 安装php_redis扩展

  扩展下载地址: https://github.com/phpredis/phpredis/tree/php7

下载压缩包

[html] view plain copy
 
  1. wget https://github.com/phpredis/phpredis/archive/php7.zip  

解压

[html] view plain copy
 
  1. unzip php7.zip  

编译安装

[html] view plain copy
 
  1. cd phpredis-php7  
  2. /usr/bin/phpize  
  3. ./configure --with-php-config=/usr/bin/php-config  
  4. make  
  5. make install  



备注说明: 请注意 phpize和php-config根据个人安装的php目录地址不一样,可以用find命令查看他们所在的地址

[html] view plain copy
 
  1. find / -name phpize  
  2. find / -name php-config  

配置

在php扩展配置目录里添加 redis.ini

通过phpinfo可以查看php的扩展配置目录

如上图,我的是在/etc/php.d/目录下,所以创建的redis.ini也在该目录下即可

redis.ini中写入

[html] view plain copy
 
  1. ;Enable redis extension module  
  2. extension=redis.so  

重启php-fpm

[html] view plain copy
 
  1. service php-fpm restart  


再次查看phpinfo就可以看到redis扩展已经有了

然后我们就可以愉快的使用redis了

 

原文地址:https://www.cnblogs.com/gaogaoxingxing/p/8427040.html