Mac下安装redis连接PHP

1,下载并且安装redis服务,mac下载地址:http://redis.io/ 

2,解压之后打开控制台进入当前解压目录:cd redis-3.2.0

3,sudo make test 测试编译,sudo make install 然后安装。

4,安装完成之后启动redis服务:redis-server

30696:M 09 Aug 16:25:55.593 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.0 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 30696
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
 
30696:M 09 Aug 16:25:55.600 # Server started, Redis version 3.2.0
30696:M 09 Aug 16:25:55.601 * The server is now ready to accept connections on port 6379

如果有Creating Server TCP listening socket *:6379: bind: Address already in use字样,说明已经启动了不用再去启动了。

5,启动redis客户端,进行测试:redis-cli

127.0.0.1:6379> 

然后就可以进行测试了,设置键值,获取值:

skyzizhudeMacBook-Pro:~ Ashes$ redis-cli
127.0.0.1:6379> set ashes "ashes value by ashes of time"
OK
127.0.0.1:6379> get ashes
"ashes value by ashes of time"
127.0.0.1:6379>

安装参考:http://www.runoob.com/redis/redis-install.html 

安装成功之后,和PHP进行连接:

1,手下下载生成PHP依赖库文件的源文件:

http://github.com/nicolasff/phpredis

解压之后进入当前文件夹,cd phpredis,然后进行编译,生成。

1>,sudo phpize

//phpize默认会走/usr/bin/phpize文件

//sudo phpize的时候会报错找不到自己安装PHP的路径,打开phpize文件之后进行修改成自己php路径:prefix=‘/Applications/XAMPP/xamppfiles’datarootdir=‘/Applications/XAMPP/xamppfiles/php’

2>,sudo ./configure

Cannot find autoconf. Please check your autoconf installation and the$PHP_AUTOCONF environment variable. Then, rerun this script.//在configure的时候会提示brew没有安装,或者安装了没有喝autoconf连接,如下为安装和连接方法。

$ brew install autoconf
Warning: autoconf-2.69 already installed, it's just not linked    
$ brew link autoconf
Linking /usr/local/Cellar/autoconf/2.69... 28 symlinks created

3>,sudo make

//sudo make的时候会发现报错,报错的原因是找不到PHP依赖的文件,打开刚刚解压的phpredis文件夹找到Makefile文件,然后修改如下内容:

//修改为自己PHP路径
phpincludedir = /Applications/XAMPP/xamppfiles/include/php
EXTENSION_DIR = /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20131226
PHP_EXECUTABLE = /Applications/XAMPP/xamppfiles/bin/php
INCLUDES = -I/Applications/XAMPP/xamppfiles/include/php -I/Applications/XAMPP/xamppfiles/include/php/main -I/Applications/XAMPP/xamppfiles/include/php/TSRM -I/Applications/XAMPP/xamppfiles/include/php/Zend -I/Applications/XAMPP/xamppfiles/include/php/ext -I/Applications/XAMPP/xamppfiles/include/php/ext/date/lib

配置好路径之后make。

4>,sudo make install

安装成之后会生成一个redis.so文件,然后在php.ini中加入:extension=redis.so

安装连接参考连接:http://www.yiibai.com/redis/redis_php.html

PHPinfo:

安装并且连接成功。

测试

<?php
   //Connecting to Redis server on localhost 
   $redis = new Redis(); 
   $redis->connect('127.0.0.1', 6379); 
   echo "Connection to server sucessfully<br>"; 
   $redis->set("myKey", "myValue"); 
   echo "Stored string in redis::<br>" .$redis->get("myKey"); 
?>

启动服务 Redis-server

关闭服务 redis-cli shutdown

客户端启动 redis-cli

参考:http://www.yiibai.com/redis/redis_php.html

Leave a Reply

Required fields are marked *