Mac用 brew 工具安装不同版本的php教程

Brew 是 Mac 下面的包管理工具,能在Mac中方便的安装软件或者卸载软件,只需要一个命令,非常方便,本文我们来看看用brew安装不同版本的php.

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为例子

  1. brew update #更新brew可安装包,建议每次执行一下
  2. brew search php55 #搜索php5.5
  3. brew tap josegonzalez/php #安装扩展<gihhub_user/repo>
  4. brew tap #查看安装的扩展列表
  5. brew install php55 #安装php5.5
  6. brew remove php55 #卸载php5.5
  7. brew upgrade php55 #升级php5.5
  8. brew options php55 #查看php5.5安装选项
  9. brew info php55 #查看php5.5相关信息
  10. brew home php55 #访问php5.5官方网站
  11. brew services list #查看系统通过 brew 安装的服务
  12. brew services cleanup #清除已卸载无用的启动配置文件
  13. brew services restart php55 #重启php-fpm

第二步,安装PHP

先添加brew的PHP扩展库:

  1. brew update
  2. brew tap homebrew/dupes
  3. brew tap homebrew/php
  4. 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.

  1. Options
  2. --disable-opcache
  3. Build without Opcache extension
  4. --disable-zend-multibyte
  5. Disable auto-detection of Unicode encoded scripts
  6. --homebrew-apxs
  7. Build against apxs in Homebrew prefix
  8. --with-apache
  9. Enable building of shared Apache 2.0 Handler module, overriding any options which disable apache
  10. --with-cgi
  11. Enable building of the CGI executable (implies --without-apache)
  12. --with-debug
  13. Compile with debugging symbols
  14. --with-fpm
  15. Enable building of the fpm SAPI executable (implies --without-apache)
  16. --with-gmp
  17. Build with gmp support
  18. --with-homebrew-curl
  19. Include Curl support via Homebrew
  20. --with-homebrew-libxslt
  21. Include LibXSLT support via Homebrew
  22. --with-homebrew-openssl
  23. Include OpenSSL support via Homebrew
  24. --with-imap
  25. Include IMAP extension
  26. --with-libmysql
  27. Include (old-style) libmysql support instead of mysqlnd
  28. --with-mssql
  29. Include MSSQL-DB support
  30. --with-pdo-oci
  31. Include Oracle databases (requries ORACLE_HOME be set)
  32. --with-phpdbg
  33. Enable building of the phpdbg SAPI executable (PHP 5.4 and above)
  34. --with-postgresql
  35. Build with postgresql support
  36. --with-thread-safety
  37. Build with thread safety
  38. --with-tidy
  39. Include Tidy support
  40. --without-bz2
  41. Build without bz2 support
  42. --without-mysql
  43. Remove MySQL/MariaDB support
  44. --without-pcntl //开源软件:phpfensi.com
  45. Build without Process Control support
  46. --without-pear
  47. Build without PEAR
  48. --without-snmp
  49. Build without SNmp support
  50. --HEAD
  51. Install HEAD version

HP编译过程中如果遇到 configure: error: Cannot find OpenSSL's <evp.h> 错误,执行 xcode-select --install 重新安装一下 Xcode Command Line Tools 即可解决该错误.

安装完php后,会有一段提示,请仔细阅读:

  1. Caveats
  2. To enable PHP in Apache add the following to httpd.conf and restart Apache:
  3. LoadModule php5_module /usr/local/opt/php53/libexec/apache2/libphp5.so
  4. The php.ini file can be found in:
  5. /usr/local/etc/php/5.3/php.ini
  6. ✩✩✩✩ PEAR ✩✩✩✩
  7. If PEAR complains about permissions, 'fix' the default PEAR permissions and config:
  8. chmod -R ug+w /usr/local/Cellar/php53/5.3.29/lib/php
  9. pear config-set php_ini /usr/local/etc/php/5.3/php.ini
  10. ✩✩✩✩ Extensions ✩✩✩✩
  11. If you are having issues with custom extension compiling, ensure that
  12. you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:
  13. PATH="/usr/local/bin:$PATH"
  14. PHP53 Extensions will always be compiled against this PHP. Please install them
  15. using --without-homebrew-php to enable compiling against system PHP.
  16. ✩✩✩✩ PHP CLI ✩✩✩✩
  17. If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,
  18. ~/.zshrc, ~/.profile or your shell's equivalent configuration file:
  19. export PATH="$(brew --prefix homebrew/php/php53)/bin:$PATH"
  20. To have launchd start php53 at login:
  21. ln -sfv /usr/local/opt/php53/*.plist ~/Library/LaunchAgents
  22. Then to load php53 now:
  23. launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php53.plist
  24. ==> Summary
  25. /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版本:

  1. echo 'export PATH="$(brew --prefix homebrew/php/php53)/bin:$PATH"' >> ~/.bash_profile #for php
  2. echo 'export PATH="$(brew --prefix homebrew/php/php53)/sbin:$PATH"' >> ~/.bash_profile #for php-fpm
  3. echo 'export PATH="/usr/local/bin:/usr/local/sbin:$PATH"' >> ~/.bash_profile #for other brew install soft
  4. source ~/.bash_profile #更新配置

如何卸载安装的 php5.3呢?

# 卸载:brew uninstall php53

# 清除缓存以及老旧版本文件:brew cleanup -s

测试一下效果:

  1. $ php -v
  2. PHP 5.3.29 (cli) (built: Jan 24 2015 12:40:58) (DEBUG)
  3. Copyright (c) 1997-2014 The PHP Group
  4. Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

mac系统自带的php:

  1. $ /usr/bin/php -v
  2. PHP 5.4.30 (cli) (built: Jul 29 2014 23:43:29)
  3. Copyright (c) 1997-2014 The PHP Group
  4. Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
  5. with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies

由于我们没有安装php的fpm模式,所以 php-fpm -v 命令显示的是mac自带的:

  1. $ php-fpm -v
  2. PHP 5.4.30 (fpm-fcgi) (built: Jul 29 2014 23:44:15)
  3. Copyright (c) 1997-2014 The PHP Group
  4. Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
  5. 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.