Write the Code. Change the World.

8月 12

需求

mac自带的php版本是5.6,想要使用7.1的特性,就得想办法升级php版本。

过程

  1. 如果存在homebrew安装的旧版本,请先卸载。
  2. 安装php7。

先卸载。

brew services list #查看homebrew安装的程序
brew uninstall php70 #假如之前安装过php70
rm -rf /usr/local/etc/php/7.0 #删除对应的配置文件


安装前准备。

brew update

brew tap homebrew/dupes

brew tap homebrew/versions

brew tap homebrew/homebrew-php

brew search php #这个时候,你可以看到众多php版本

前php的release版本是7.1.6。通过brew search php命令查看服务器上可安装的php版本,当前最新版本为7.1.5

通过

brew options php71 

命令来查看安装php7.1的选项。如果服务器是nginx,请带参数 --width-fpm。如果服务器是apache,就用--width-httpd24

完整命令:

brew install php71 --with-fpm --with-gmp --with-imap --with-tidy --with-debug

如果安装失败,提示:configure: error: Cannot find libz。这个时候,请先执行:

xcode-select --install

然后,再安装。

php安装成功后,会出现下边这样的提示:

The php.ini file can be found in:
    /usr/local/etc/php/7.1/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"

PHP71 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/php71)/bin:$PATH"

GMP has moved to its own formula, please install it by running: brew install php71-gmp

✩✩✩✩ FPM ✩✩✩✩

To launch php-fpm on startup:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist

The control script is located at /usr/local/opt/php71/sbin/php71-fpm

OS X 10.8 and newer come with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:

  PATH="/usr/local/sbin:$PATH"

You may also need to edit the plist to use the correct "UserName".

Please note that the plist was called 'homebrew-php.josegonzalez.php71.plist' in old versions of this formula.

With the release of macOS Sierra the Apache module is now not built by default. If you want to build it on your system you have to install php with the --with-httpd24 option. See  brew options php71 for more details.

To have launchd start homebrew/php/php71 now and restart at login:
  brew services start homebrew/php/php71
==> Summary
  /usr/local/Cellar/php71/7.1.8_20: 345 files, 44.3MB, built in 5 minutes 55 seconds

就是要进行一些配置。

配置环境变量

mac自带的php-fpm的位置 /usr/sbin/php-fpm

由于mac自带了php和php-fpm,因此需要添加系统环境变量PATH来替代自带的php版本。

echo 'export PATH="$(brew --prefix php71)/bin:$PATH"' >> ~/.bash_profile #for php
echo 'export PATH="$(brew --prefix php71)/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

分别执行上边的命令。source命令使得配置的环境变量生效。

echo $PATH;

查看环境变量指向。

php -v

查看当前php的版本,已经是安装是php7.1.5

设置php-fpm 以及启动

cp /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist
brew services start homebrew/php/php71

其实,这些,在安装完php时,提示里已经有了。

启动php-fpm时,如果出现address已经占用,可以先kill再启动。

killall php-fpm

测试

先查看 php-fpm,nginx 的进程。如果进程没启动,就要先启动进程了。nginx里解析也要对。

ps -ef|grep nginx
ps -ef|grep php-fpm
sudo nginx #如果没启动的话
sudo php-fpm #如果没启动的话

参考文章

http://www.jianshu.com/p/a7dabecb8838
http://jingyan.baidu.com/article/a3761b2bfa77701576f9aadb.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注