使用brew将Mac上的PHP5更新为PHP7

将Mac上安装的PHP5升级到PHP7。

這是作業的備忘錄。

最开始的状态

操作系统:OS X EL Capitan(10.11.6)

$ php -v
PHP 5.5.38 (cli) (built: Aug 21 2016 21:48:49)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

家庭制备

我参考了这个地方。在Mac上安装Homebrew。

用Homebrew搜索PHP7

在brew中搜索php@7。

$ brew search php@7
==> Searching local taps...
php@7.0                    php@7.1                    php@7.2
==> Searching taps on GitHub...
==> Searching blacklisted, migrated and deleted formulae...
...

安装PHP7

使用brew install php@7.1命令进行安装。

$ brew install php@7.1
==> Downloading https://homebrew.bintray.com/bottles/php@7.1-7.1.16_1.sierra.bottle.tar.gz
Already downloaded: /Users/$USER/Library/Caches/Homebrew/php@7.1-7.1.16_1.sierra.bottle.tar.gz
==> Pouring php@7.1-7.1.16_1.sierra.bottle.tar.gz
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set php_ini /usr/local/etc/php/7.1/php.ini system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set php_dir /usr/local/share/pear@7.1 system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set doc_dir /usr/local/share/pear@7.1/doc system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set ext_dir /usr/local/lib/php/pecl/20160303 system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set bin_dir /usr/local/opt/php@7.1/bin system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set data_dir /usr/local/share/pear@7.1/data system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set cfg_dir /usr/local/share/pear@7.1/cfg system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set www_dir /usr/local/share/pear@7.1/htdocs system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set man_dir /usr/local/share/man system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set test_dir /usr/local/share/pear@7.1/test system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear config-set php_bin /usr/local/opt/php@7.1/bin/php system
==> /usr/local/Cellar/php@7.1/7.1.16_1/bin/pear update-channels
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php@7.1/lib/httpd/modules/libphp7.so

    <FilesMatch .php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/7.1/

This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/php@7.1/bin:$PATH"' >> ~/.bash_profile
  echo 'export PATH="/usr/local/opt/php@7.1/sbin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/php@7.1/lib
    CPPFLAGS: -I/usr/local/opt/php@7.1/include


To have launchd start php@7.1 now and restart at login:
  brew services start php@7.1
Or, if you don't want/need a background service you can just run:
  php-fpm
==> Summary
?  /usr/local/Cellar/php@7.1/7.1.16_1: 514 files, 67MB

打通道路

在.bash_profile文件中添加。

export PATH="/usr/local/opt/php@7.1/bin:$PATH"
export PATH="/usr/local/opt/php@7.1/sbin:$PATH"

确认

$ php -v
PHP 7.1.16 (cli) (built: Apr 12 2018 03:25:50) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.16, Copyright (c) 1999-2018, by Zend Technologies

非常简单。(≧ω≦)