Mac下通過 brew 安裝不同版本的php
阿新 • • 發佈:2019-02-02
正 文:
MAC OS X 10.9.5系統裡已經自帶了 apache 和 php ,只是預設沒有啟用,我們將他們啟用即可。
APACHE的預設版本是 Apache/2.2.26 (Unix),php版本是PHP 5.4.30。 由於除錯程式需要,我需要同時安裝低版本的php5.3,但是又不希望刪除系統預裝的php 5.4,或升級/降級系統的php5.4,那麼怎麼辦呢?這個時候,就可以通過brew的方式安裝新的php版本。 第一步,先安裝 brew Brew 是 Mac 下面的包管理工具,通過 Github 託管適合 Mac 的編譯配置以及 Patch,可以方便的安裝開發工具。 Mac 自帶ruby 所以安裝起來很方便,同時它也會自動把git也給你裝上。官方網站: http://brew.sh 在mac下終端裡直接輸入命令列:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 安裝完成之後,建議執行一下自檢:brew doctor 如果看到Your system is ready to brew. 那麼你的brew已經可以開始使用了。 常用命令: (所有軟體以PHP5.5為例子)
brew update #更新brew可安裝包,建議每次執行一下
brew search php55 #搜尋php5.5
brew tap josegonzalez/php #安裝擴充套件<gihhub_user/repo>
brew tap #檢視安裝的擴充套件列表
brew install php55 #安裝php5.5
brew remove php55 #解除安裝php5.5
brew upgrade php55 #升級php5.5
brew options php55 #檢視php5.5安裝選項
brew info php55 #檢視php5.5相關資訊
brew home php55 #訪問php5.5官方網站
brew services list #檢視系統通過 brew 安裝的服務
brew services cleanup #清除已解除安裝無用的啟動配置檔案
brew services restart php55 #重啟php-fpm 第二步,安裝PHP
先新增brew的PHP擴充套件庫:
brew update
brew tap homebrew/dupes
brew tap homebrew/php
brew tap josegonzalez/homebrew-php 可以使用 brew options php53 命令來檢視安裝php5.3的選項,這裡我用下面的選項安裝:
brew install php53 --with-apache --with-gmp --with-imap --with-tidy --with-debug 請注意:如果你希望以mac下的apache作為web server,編譯時要加 --with-apache;如果你的web server 是 nginx這類,就需要加上 --with-fpm。
Options
--disable-opcache
Build without Opcache extension
--disable-zend-multibyte
Disable auto-detection of Unicode encoded scripts
--homebrew-apxs
Build against apxs in Homebrew prefix
--with-apache
Enable building of shared Apache 2.0 Handler module, overriding any options which disable apache
--with-cgi
Enable building of the CGI executable (implies --without-apache)
--with-debug
Compile with debugging symbols
--with-fpm
Enable building of the fpm SAPI executable (implies --without-apache)
--with-gmp
Build with gmp support
--with-homebrew-curl
Include Curl support via Homebrew
--with-homebrew-libxslt
Include LibXSLT support via Homebrew
--with-homebrew-openssl
Include OpenSSL support via Homebrew
--with-imap
Include IMAP extension
--with-libmysql
Include (old-style) libmysql support instead of mysqlnd
--with-mssql
Include MSSQL-DB support
--with-pdo-oci
Include Oracle databases (requries ORACLE_HOME be set)
--with-phpdbg
Enable building of the phpdbg SAPI executable (PHP 5.4 and above)
--with-postgresql
Build with postgresql support
--with-thread-safety
Build with thread safety
--with-tidy
Include Tidy support
--without-bz2
Build without bz2 support
--without-mysql
Remove MySQL/MariaDB support
--without-pcntl
Build without Process Control support
--without-pear
Build without PEAR
--without-snmp
Build without SNmp support
--HEAD
Install HEAD version PHP編譯過程中如果遇到 configure: error: Cannot find OpenSSL's <evp.h> 錯誤,執行 xcode-select --install 重新安裝一下 Xcode Command Line Tools 即可解決該錯誤(來源:https://github.com/Homebrew/homebrew-php/issues/1181)。 安裝完php後,會有一段提示,請仔細閱讀:
Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php5_module /usr/local/opt/php53/libexec/apache2/libphp5.soThe php.ini file can be found in:
/usr/local/etc/php/5.3/php.ini✩✩✩✩ PEAR ✩✩✩✩If PEAR complains about permissions, 'fix' the default PEAR permissions and config:
chmod -R ug+w /usr/local/Cellar/php53/5.3.29/lib/php
pear config-set php_ini /usr/local/etc/php/5.3/php.ini✩✩✩✩ Extensions ✩✩✩✩If you are having issues with custom extension compiling, ensure that
you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH: PATH="/usr/local/bin:$PATH"PHP53 Extensions will always be compiled against this PHP. Please install them
using --without-homebrew-php to enable compiling against system PHP.✩✩✩✩ PHP CLI ✩✩✩✩If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,
~/.zshrc, ~/.profile or your shell's equivalent configuration file: export PATH="$(brew --prefix homebrew/php/php53)/bin:$PATH"To have launchd start php53 at login:
ln -sfv /usr/local/opt/php53/*.plist ~/Library/LaunchAgents
Then to load php53 now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php53.plist
==> Summary
/usr/local/Cellar/php53/5.3.29: 480 files, 31M, built in 12.9 minutes
等待PHP編譯完成,開始安裝PHP常用擴充套件,擴充套件安裝過程中 brew會自動安裝依賴包,使用 brew search php53- 命令,可以檢視還有哪些擴充套件可以安裝,然後執行 brew install php53-XXX 就可以了。 由於Mac自帶了php和php-fpm,因此需要新增系統環境變數PATH來替代自帶PHP版本:
echo 'export PATH="$(brew --prefix homebrew/php/php53)/bin:$PATH"' >> ~/.bash_profile #for php
echo 'export PATH="$(brew --prefix homebrew/php/php53)/sbin:$PATH"' >> ~/.bash_profile #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbin:$PATH"' >> ~/.bash_profile #for other brew install soft
source ~/.bash_profile #更新配置 如何解除安裝安裝的 php5.3呢?
# 解除安裝
brew uninstall php53
# 清除快取以及老舊版本檔案
brew cleanup -s 測試一下效果:
$ php -v
PHP 5.3.29 (cli) (built: Jan 24 2015 12:40:58) (DEBUG)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies# mac系統自帶的php
$ /usr/bin/php -v
PHP 5.4.30 (cli) (built: Jul 29 2014 23:43:29)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies 由於我們沒有安裝php的fpm模式,所以 php-fpm -v 命令顯示的是mac自帶的:
$ php-fpm -v
PHP 5.4.30 (fpm-fcgi) (built: Jul 29 2014 23:44:15)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies
這個時候,我們使用 phpinfo 函式顯示出來的php版本還是max自帶的PHP5.4,需要我們修改 apache的配置檔案 httpd.conf,載入的php5模組路徑指向剛剛安裝的 php5.3目錄裡的 libphp5.so:
LoadModule php5_module /usr/local/opt/php53/libexec/apache2/libphp5.so
重啟apache,phpinfo() 腳本里顯示的 php 版本就變成了 PHP Version 5.3.29。
MAC OS X 10.9.5系統裡已經自帶了 apache 和 php ,只是預設沒有啟用,我們將他們啟用即可。
APACHE的預設版本是 Apache/2.2.26 (Unix),php版本是PHP 5.4.30。 由於除錯程式需要,我需要同時安裝低版本的php5.3,但是又不希望刪除系統預裝的php 5.4,或升級/降級系統的php5.4,那麼怎麼辦呢?這個時候,就可以通過brew的方式安裝新的php版本。 第一步,先安裝 brew Brew 是 Mac 下面的包管理工具,通過 Github 託管適合 Mac 的編譯配置以及 Patch,可以方便的安裝開發工具。 Mac 自帶ruby 所以安裝起來很方便,同時它也會自動把git也給你裝上。官方網站:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 安裝完成之後,建議執行一下自檢:brew doctor 如果看到Your system is ready to brew. 那麼你的brew已經可以開始使用了。 常用命令: (所有軟體以PHP5.5為例子)
brew update #更新brew可安裝包,建議每次執行一下
brew search php55 #搜尋php5.5
brew tap josegonzalez/php #安裝擴充套件<gihhub_user/repo>
brew tap #檢視安裝的擴充套件列表
brew install php55 #安裝php5.5
brew remove php55 #解除安裝php5.5
brew upgrade php55 #升級php5.5
brew options php55 #檢視php5.5安裝選項
brew info php55 #檢視php5.5相關資訊
brew home php55 #訪問php5.5官方網站
brew services list #檢視系統通過 brew 安裝的服務
brew services cleanup #清除已解除安裝無用的啟動配置檔案
brew services restart php55 #重啟php-fpm 第二步,安裝PHP
先新增brew的PHP擴充套件庫:
brew update
brew tap homebrew/dupes
brew tap homebrew/php
brew tap josegonzalez/homebrew-php 可以使用 brew options php53 命令來檢視安裝php5.3的選項,這裡我用下面的選項安裝:
brew install php53 --with-apache --with-gmp --with-imap --with-tidy --with-debug 請注意:如果你希望以mac下的apache作為web server,編譯時要加 --with-apache;如果你的web server 是 nginx這類,就需要加上 --with-fpm。
--disable-opcache
Build without Opcache extension
--disable-zend-multibyte
Disable auto-detection of Unicode encoded scripts
--homebrew-apxs
Build against apxs in Homebrew prefix
--with-apache
Enable building of shared Apache 2.0 Handler module, overriding any options which disable apache
--with-cgi
Enable building of the CGI executable (implies --without-apache)
--with-debug
Compile with debugging symbols
--with-fpm
Enable building of the fpm SAPI executable (implies --without-apache)
--with-gmp
Build with gmp support
--with-homebrew-curl
Include Curl support via Homebrew
--with-homebrew-libxslt
Include LibXSLT support via Homebrew
--with-homebrew-openssl
Include OpenSSL support via Homebrew
--with-imap
Include IMAP extension
--with-libmysql
Include (old-style) libmysql support instead of mysqlnd
--with-mssql
Include MSSQL-DB support
--with-pdo-oci
Include Oracle databases (requries ORACLE_HOME be set)
--with-phpdbg
Enable building of the phpdbg SAPI executable (PHP 5.4 and above)
--with-postgresql
Build with postgresql support
--with-thread-safety
Build with thread safety
--with-tidy
Include Tidy support
--without-bz2
Build without bz2 support
--without-mysql
Remove MySQL/MariaDB support
--without-pcntl
Build without Process Control support
--without-pear
Build without PEAR
--without-snmp
Build without SNmp support
--HEAD
Install HEAD version PHP編譯過程中如果遇到 configure: error: Cannot find OpenSSL's <evp.h> 錯誤,執行 xcode-select --install 重新安裝一下 Xcode Command Line Tools 即可解決該錯誤(來源:https://github.com/Homebrew/homebrew-php/issues/1181)。 安裝完php後,會有一段提示,請仔細閱讀:
Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php5_module /usr/local/opt/php53/libexec/apache2/libphp5.soThe php.ini file can be found in:
/usr/local/etc/php/5.3/php.ini✩✩✩✩ PEAR ✩✩✩✩If PEAR complains about permissions, 'fix' the default PEAR permissions and config:
chmod -R ug+w /usr/local/Cellar/php53/5.3.29/lib/php
pear config-set php_ini /usr/local/etc/php/5.3/php.ini✩✩✩✩ Extensions ✩✩✩✩If you are having issues with custom extension compiling, ensure that
you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH: PATH="/usr/local/bin:$PATH"PHP53 Extensions will always be compiled against this PHP. Please install them
using --without-homebrew-php to enable compiling against system PHP.✩✩✩✩ PHP CLI ✩✩✩✩If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,
~/.zshrc, ~/.profile or your shell's equivalent configuration file: export PATH="$(brew --prefix homebrew/php/php53)/bin:$PATH"To have launchd start php53 at login:
ln -sfv /usr/local/opt/php53/*.plist ~/Library/LaunchAgents
Then to load php53 now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php53.plist
==> Summary
/usr/local/Cellar/php53/5.3.29: 480 files, 31M, built in 12.9 minutes
等待PHP編譯完成,開始安裝PHP常用擴充套件,擴充套件安裝過程中 brew會自動安裝依賴包,使用 brew search php53- 命令,可以檢視還有哪些擴充套件可以安裝,然後執行 brew install php53-XXX 就可以了。 由於Mac自帶了php和php-fpm,因此需要新增系統環境變數PATH來替代自帶PHP版本:
echo 'export PATH="$(brew --prefix homebrew/php/php53)/bin:$PATH"' >> ~/.bash_profile #for php
echo 'export PATH="$(brew --prefix homebrew/php/php53)/sbin:$PATH"' >> ~/.bash_profile #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbin:$PATH"' >> ~/.bash_profile #for other brew install soft
source ~/.bash_profile #更新配置 如何解除安裝安裝的 php5.3呢?
# 解除安裝
brew uninstall php53
# 清除快取以及老舊版本檔案
brew cleanup -s 測試一下效果:
$ php -v
PHP 5.3.29 (cli) (built: Jan 24 2015 12:40:58) (DEBUG)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies# mac系統自帶的php
$ /usr/bin/php -v
PHP 5.4.30 (cli) (built: Jul 29 2014 23:43:29)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies 由於我們沒有安裝php的fpm模式,所以 php-fpm -v 命令顯示的是mac自帶的:
$ php-fpm -v
PHP 5.4.30 (fpm-fcgi) (built: Jul 29 2014 23:44:15)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies
這個時候,我們使用 phpinfo 函式顯示出來的php版本還是max自帶的PHP5.4,需要我們修改 apache的配置檔案 httpd.conf,載入的php5模組路徑指向剛剛安裝的 php5.3目錄裡的 libphp5.so:
LoadModule php5_module /usr/local/opt/php53/libexec/apache2/libphp5.so
重啟apache,phpinfo() 腳本里顯示的 php 版本就變成了 PHP Version 5.3.29。