上篇博客總結(jié)了下如何部署redis服務(wù),可以參考linux下redis服務(wù)的搭建?,F(xiàn)在要在php環(huán)境下使用redis,需要在php環(huán)境下添加redis擴(kuò)展。
思路很簡單,安裝php,安裝redis,添加redis擴(kuò)展,三個(gè)步驟。(PS: 我是新建另一臺虛擬機(jī)進(jìn)行安裝,所以php也需要安裝)。
環(huán)境介紹及準(zhǔn)備
VMware虛擬機(jī),centos6.3
新虛擬機(jī)需要安裝一些常見的工具包,包括gcc在內(nèi)的等。
yum -y install gcc gcc-c++ libxml2 libxml2-devel bzip2 bzip2-devel libmcrypt libmcrypt-devel openssl openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel readline readline-devel libxslt-devel perl perl-devel psmisc.x86_64 recode recode-devel libtidy libtidy-devel epel-release libmcrypt-devel
一、php安裝
下載、安裝,configure時(shí)指定安裝目錄及配置文件目錄
[root@localhost software]# wget http://cn2.php.net/distributions/php-5.6.32.tar.gz[root@localhost software]# tar zxvf php-5.6.32.tar.gz [root@localhost software]# cd php-5.6.32[root@localhost php-5.6.32]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/config[root@localhost php-5.6.32]# make && make install
此時(shí)PHP以安裝好,在我們指定的配置文件目錄(/usr/local/php/config)新建響應(yīng)的目錄,并且新建php.ini文件,此時(shí)配置文件為空,所有配置為默認(rèn)配置,需要改動的話,在此文件中添加即可。操作如下:
[root@localhost ~]# cd /usr/local/php[root@localhost php]# mkdir config[root@localhost php]# vim config/php.ini
二、redis安裝
下載解壓redis源碼并進(jìn)入redis源碼
[root@localhost php]# cd /root/software/[root@localhost software]# wget http://pecl.php.net/get/redis-3.1.3.tgz[root@localhost software]# tar zxvf redis-3.1.3.tgz[root@localhost software]# cd redis-3.1.3
生成configure文件
生成config需要使用php安裝目錄下的phpize文件(路徑在/usr/local/php/bin/)下
[root@localhost redis-3.1.3]# /usr/local/php/bin/phpize
執(zhí)行configure,生成makefile文件
這一步需要使用PHP安裝目錄下的php-config
[root@localhost redis-3.1.3]# ./configure --with-php-config=/usr/local/php/bin/php-config
編譯 安裝
[root@localhost redis-3.1.3]# make[root@localhost redis-3.1.3]# make install
會生成redis.so文件,并返回文件路徑,即成功。如圖
三、php配置文件添加redis擴(kuò)展
先查看目前的PHP擴(kuò)展情況
[root@localhost redis-3.1.3]# cd /usr/local/php[root@localhost php]# bin/php -m|grep redis
無任何返回如圖
進(jìn)入配置文件添加 extension=redis.so
[root@localhost php]# vim config/php.ini
再次查看PHP擴(kuò)展情況,有了返回內(nèi)容
[root@localhost php]# bin/php -m|grep redis
擴(kuò)展安裝完成,可以再php中使用redis相關(guān)類。
PS:
phpredis參考文檔
redis類的命名空間是根 即在根命名空間下實(shí)例化
$redis = new \Redis();