Homebrewでサポートが切れたPHPをインストールする

仕事でMacにPHP7.1を入れる必要があったのですが、みんな大好きbrewをみたらFormulaeに7.1がいませんでした。
なのでこうしましたというお話です。

環境

なぜ7.1がいないのか

PHP7.1のサポートが切れているからです。
アクティブサポートが2018年末に、セキュリティサポートも2019年末に終わりました。

インストール方法

こちらを参考に進めます。

getgrav.org

インストール

サポートが終わった非推奨PHPはhomebrew-deprecatedというタブにいるとのこと。
なので、そこからインストールします。

$ brew install openldap libiconv  
$ brew tap exolnet/homebrew-deprecated  
$ brew install php@7.1

このinstall、けっこう時間がかかりました。

ちなみに、brewで古いバージョンのPHPをインストールしていた場合は、まずbrewのupgradeをして、インストール済みのものを削除する必要があるとのこと*1

パス設定

installが終わると、Apachへの設定情報や環境変数に追加するパスなどが表示されます。

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/

php@7.1 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 php@7.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/php@7.1/bin:$PATH"' >> /Users/<user名>/.bash_profile
  echo 'export PATH="/usr/local/opt/php@7.1/sbin:$PATH"' >> /Users/<user名>/.bash_profile

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


To have launchd start exolnet/deprecated/php@7.1 now and restart at login:
  brew services start exolnet/deprecated/php@7.1
Or, if you don't want/need a background service you can just run:
  php-fpm

まずは.bash_profileにパスを追加。

$ echo 'export PATH="/usr/local/opt/php@7.1/bin:$PATH"' >> /Users/<user名>/.bash_profile
$ echo 'export PATH="/usr/local/opt/php@7.1/sbin:$PATH"' >> /Users/<user名>/.bash_profile

.bash_profileを再読み込みして、phpのバージョンと場所を確認します。

$ source ~/.bash_profile

$ php -v
PHP 7.1.33 (cli) (built: Aug 20 2020 15:13:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.33, Copyright (c) 1999-2018, by Zend Technologies

$ which php
/usr/local/opt/php@7.1/bin/php

新しくインストールしたPHPが有効になっていることを確認できました!

*1:非推奨PHPをdeprecatedタブへ移動したことでパスが変わっているから、一旦きれいにしなければということでしょうか。
ということは、2018年春以降にインストールした人は対象外かな?(移動があったのがそのくらいの時期らしいので)
詳細はこちらへ。getgrav.org