[Homebrew] ローカルのPHP5.5からPHP7.0へアップグレードする

環境

  • Homebrew 1.1.7
  • php55
  • Server version: Apache/2.4.25 (Unix)

Homebrewのupdate

$ brew doctor
$ brew update
$ brew upgrade

PHP5を消す

$ brew unlink php55
$ brew uninstall php55
# うまく消せない場合
$ brew tap --repair

PHP7のインストール

https://github.com/Homebrew/homebrew-php/blob/master/README.md
GithubのREADMEを参照しつつ、php7をインストール。

# Setup the homebrew/dupes tap which has dependencies we need:
$ brew tap homebrew/dupes

# Setup the homebrew/versions tap which has dependencies we need:
$ brew tap homebrew/versions

# Then, run the following in your command-line:
$ brew tap homebrew/homebrew-php

# Note: For a list of available configuration options run:
$ brew options php70

$ brew install php70 --with-httpd24
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
yank

==> Installing php70 from homebrew/php
==> Downloading https://php.net/get/php-7.0.14.tar.bz2/from/this/mirror
==> Downloading from https://secure.php.net/distributions/php-7.0.14.tar.bz2
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/php70/7.0.14_7 --localstatedir=/usr/local/var --sysconfdir=/usr/local/etc/php/7.0 --with-config-file-path=/usr/local/etc/php/7.0 --with-config-file-sc
==> make
==> make install
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module    /usr/local/opt/php70/libexec/apache2/libphp7.so

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

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

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

PHP70 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/php70)/bin:$PATH"

✩✩✩✩ FPM ✩✩✩✩

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

The control script is located at /usr/local/opt/php70/sbin/php70-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.php70.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 php70  for more details.

To have launchd start homebrew/php/php70 now and restart at login:
  brew services start homebrew/php/php70
==> Summary
?  /usr/local/Cellar/php70/7.0.14_7: 333 files, 49.9M, built in 5 minutes 51 seconds

httpd.confの設定

--with-http24 を追加することにより、Apache用のPHPモジュール libphp7.so が生成され、
さらにApacheの設定ファイル httpd.conf へ、それを読み込むLoadModuleの記述が下記のように追加される。

// To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module        /usr/local/Cellar/php70/7.0.14_7/libexec/apache2/libphp7.so
<FilesMatch .php$>
    SetHandler application/x-httpd-php
</FilesMatch>

最後に DirectoryIndexindex.php が含まれているか、確認。

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

環境変数の設定

export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"

Apacheリスタート後、PHPのversionを確認する。

$ php -v                                                                                                                                     PHP 7.0.14 (cli) (built: Jan 18 2017 00:18:59) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

OK。