Home  >  Article  >  Backend Development  >  Install different versions of php through brew on Mac

Install different versions of php through brew on Mac

WBOY
WBOYOriginal
2016-08-08 09:30:411193browse
MAC OS X 10.9.5 system already comes with apache and php, but they are not enabled by default. We can enable them.
The default version of APACHE is Apache/2.2.26 (Unix), and the php version is PHP 5.4.30.  Due to the need to debug the program, I need to install a lower version of php5.3 at the same time, but I don’t want to delete the system’s pre-installed php5.4, or upgrade/downgrade the system’s php5.4, so what should I do? At this time, you can install the new php version through brew. The first step is to install brew Brew is a package management tool for Mac. It hosts compilation configurations and patches suitable for Mac through Github, which makes it easy to install development tools. Mac comes with ruby, so it is very convenient to install, and it will also automatically install git for you. Official website: http://brew.sh   Directly enter the command line in the mac terminal:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

After the installation is complete, it is recommended to execute Do a self-check: brew doctor If you see Your system is ready to brew. Then your brew is ready to be used. Common commands: (All software uses PHP5.5 as an example)

brew update brew tap josegonzalez/php #Install extension
brew tap                                                                                             #Uninstall php5.5
brew upgrade php55 #Upgrade php5.5
brew options php55 #View php5. 5 Installation options
brew info php55                                                                                                                                                         #                                                                                                           #  
brew services cleanup #Clear uninstalled and useless startup configuration files
brew services restart php55 #Restart php-fpm



The second step is to install PHP

First add brew’s PHP extension library:
brew updateb rew tap homebrew/dupes
brew tap homebrew/ phpbrew tap josegonzalez/homebrew-php
You can use the


brew options php53

command to view the options for installing php5.3. Here I use the following options to install:

brew install php53 --with-apache - -with-gmp --with-imap --with-tidy --with-debug Please note: If you want to use apache under mac as the web server, you need to add --with-apache when compiling; if your web server is nginx, you need to add --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 in stead 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 a bove )
--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

If you encounter a configure: error: Cannot find OpenSSL's error during PHP compilation, execute xcode-select --install and reinstall Xcode Command Line Tools to resolve the error. (Source: https://github.com/Homebrew/homebrew-php/issues/1181). After installing php, there will be a prompt, please read it carefully:

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/launchagentsthen to load PHP53 now:

LAUNCTL LOAD ~/ Library/launchagents/Homebrew. mxcl.php53.plist==> Summary
/usr/local/Cellar/php53/5.3.29: 480 files, 31M, built in 12.9 minutes



Wait for PHP to be compiled and start installing PHP commonly used Extension, during the extension installation process, brew will automatically install dependent packages. Use the brew search php53- command to check which extensions can be installed, and then execute brew install php53-XXX. Since Mac comes with php and php-fpm, you need to add the system environment variable PATH to replace the built-in PHP version:
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 #Update configuration

How to uninstall the installed php5.3?
# Uninstall

brew uninstall php53
# Clear cache and old version files
brew cleanup -s
Test the effect:

$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 # The php that comes with the mac system
$ /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

Since we did not install the fpm mode of php, so php-fpm - The v command displays the one that comes with 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



At this time, we use phpinfo The PHP version displayed by the function is still PHP5.4 that comes with max. We need to modify the apache configuration file httpd.conf. The path of the loaded php5 module points to libphp5.so in the php5.3 directory just installed:

LoadModule php5_module /usr/local/opt/php53/libexec/apache2/libphp5.so


Restart apache, and the php version displayed in the phpinfo() script will become PHP Version 5.3.29.

The above introduces how to install different versions of php through brew on Mac, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn