大多數語言編程的時候,都會碰到在不一樣版本下的代碼兼容性問題,測試代碼兼容性,或者調試老舊代碼,須要不一樣的語言版本環境.php
ruby有了rbenv,rvm,nodejs也有nvm,固然php也應該有個相似的 phpenv , php-version , phpbrew 來管理PHP版本,簡單查看了下這三個工具,我更偏向於phpbrew一些,其實我更願意叫它PHPbrew.node
若是你肯定須要使用phpbrew,請認真讀完本文,再去實踐,請注意!前方有坑(其實我更推薦 vagrant ).mysql
Brew & manage PHP versions in pure PHP at HOMEnginx
下載:git
curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrewchmod +x phpbrewsudo mv phpbrew /usr/bin/phpbrew
爲你的shell環境運行初始化腳本:github
phpbrew init
設置環境變量:sql
sudo vi ~/.bashrc#文件最後,插入下面這行代碼source ~/.phpbrew/bashrc#保存退出:wq
顯示已知版本:shell
phpbrew known===> Fetching release list...[==================================================================] 29.67/29.67KB 100%5.6: 5.6.3, 5.6.2, 5.6.1, 5.6.0 ...5.5: 5.5.19, 5.5.18, 5.5.17, 5.5.16, 5.5.15, 5.5.14, 5.5.13, 5.5.12 ...5.4: 5.4.35, 5.4.34, 5.4.33, 5.4.32, 5.4.31, 5.4.30, 5.4.29, 5.4.28 ...5.3: 5.3.29, 5.3.28, 5.3.27, 5.3.26, 5.3.25, 5.3.24, 5.3.23, 5.3.22 ...You can run `phpbrew update` to get a newer release list.
顯示舊版本:apache
phpbrew known --old
更新版本列表:編程
phpbrew known --update
默認配置安裝PHP:
phpbrew install 5.4.0 +default
測試安裝:
phpbrew install --test 5.4.0
顯示debug信息:
phpbrew -d install --test 5.4.0
安裝舊版本:
phpbrew install --old 5.2.13
清理編譯目錄:
phpbrew clean
顯示可用參數:
phpbrew variantsVariants: all, apxs2, bcmath, bz2, calendar, cgi, cli, ctype, curl, dba, debug, dom, embed, exif, fileinfo, filter, fpm, ftp, gcov, gd, gettext, gmp, hash, iconv, icu, imap, inifile, inline, intl, ipc, ipv6, json, kerberos, libgcc, mbregex, mbstring, mcrypt, mhash, mysql, opcache, openssl, pcntl, pcre, pdo, pgsql, phar, phpdbg, posix, readline, session, soap, sockets, sqlite, static, tidy, tokenizer, wddx, xml, xml_all, xmlrpc, zip, zlib, ztsVirtual variants: dbs: sqlite, mysql, pgsql, pdo mb: mbstring, mbregex neutral: default: bcmath, bz2, calendar, cli, ctype, dom, fileinfo, filter, ipc, json, mbregex, mbstring, mhash, mcrypt, pcntl, pcre, pdo, phar, posix, readline, sockets, tokenizer, xml, curl, openssl, ziUsing variants to build PHP: phpbrew install php-5.3.10 +default phpbrew install php-5.3.10 +mysql +pdo phpbrew install php-5.3.10 +mysql +pdo +apxs2 phpbrew install php-5.3.10 +mysql +pdo +apxs2=/usr/bin/apxs2
phpbrew install 5.3.10 +mysql +sqlite -- \ --enable-ftp --apxs2=/opt/local/apache2/bin/apxs
臨時使用:
phpbrew use 5.4.22
切換版本(設置默認版本):
phpbrew switch 5.4.18
關閉:
phpbrew off
phpbrew list
NGINX須要配合php-fpm使用,所以,若是是使用 LNMP
或者本身安裝的NGINX
+PHP
的運行環境,則須要在phpbrew安裝PHP的時候加上+fpm
模塊,才能使用phpbrew
的模塊管理.
phpbrew install 5.6.3 +fpm
啓動FPM:
phpbrew fpm start
中止FPM
phpbrew fpm stop
顯示php-fpm的模塊:
phpbrew fpm module
測試php-fpm的配置
phpbrew fpm test
配置php-fpm
phpbrew fpm config
更多 擴展安裝 啓用擴展 配置PHP 管理FPM 其餘組件(composer,phpunit).
OS X筆記:
安裝準備:
brew install automake autoconf curl pcre re2c mhash libtool icu4c gettext jpeg libxml2 mcrypt gmp libevent libpng freetypebrew link icu4cln -s /usr/local/include/freetype2 /usr/local/include/freetype2/freetype
安裝phpbrew:
curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrewchmod +x phpbrewsudo mv phpbrew /usr/bin/phpbrewphpbrew initsource ~/.phpbrew/bashrcphpbrew lookup-prefix homebrew
安裝php版本:
#一樣的在其餘系統平臺也會出現相似庫找不到的問題,須要手動指定路徑phpbrew install 5.3.29 +default +fpm +pdo +mysql +sqlite +gd -- --with-gd=shared --with-jpeg-dir=/usr/local/Cellar --with-png-dir=/usr/local/Cellar --enable-gd-native-ttf --with-freetype-dir=/usr/local/Cellar#print_r(gd_info());
注意:若是配置或編譯的時候出現報錯,請查看此頁面 Requirement
安裝PHP版本
測試默認config
phpbrew install 5.6.3 --test +defaulttail -f /home/rming/.phpbrew/build/php-5.6.3/build.log
默認:
phpbrew install 5.6.3 +default +fpm +pdo +mysql +sqlite +gdtail -f /home/rming/.phpbrew/build/php-5.6.3/build.log
精簡:
phpbrew install 5.6.3 +fpm +json +dbs +mb +curl +mcrypt +filtertail -f /home/rming/.phpbrew/build/php-5.6.3/build.log
+json
是由於phpbrew自己是個phar包,依賴於php的json函數
+fpm
是爲了nginx
,phpbrew安裝的php-fpm默認listen=127.0.0.1:9000
,所以須要把以前lnmp的配置文件fastcgi_pass
選項更改成 127.0.0.1:9000
;
+dbs
是由於沒有安裝mysqli擴展,想用個phpmyadmin都困難,囧;
+mb
是多種語言編碼字節數問題,爲了中文字符截斷和匹配使用
+curl
curl擴展 , 很經常使用的,你懂得
+mcrypt
phpmyadmin須要
+filter
filter_var過濾函數
寫個腳本切換php-fpm:
function nginxenv(){ phpbrew fpm stop &>/dev/null; phpbrew switch ${1}; phpbrew fpm start &>/dev/null;}
保存文件到~/.phpbrew/nginxenv
, 而後在~/.bashrc
最後加上 source ~/.phpbrew/nginxenv
, 而後命令行裏就能夠 使用nginxenv 5.3.29
這樣切換版本了.
參考: