您好,欢迎访问一九零五行业门户网

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为例子)
brew update                        #更新brew可安装包,建议每次执行一下
brew search php55                  #搜索php5.5
brew tap josegonzalez/php          #安装扩展   
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  错误,执行 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:$pathphp53 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:$pathto 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下通过 brew 安装不同版本的php,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息