2017年9月16日土曜日

Mac Sierraにphp 7 を導入

macOS Sierra バージョン10.12.6 (16G29)にphp 7を導入することになった。
大いにハマったのでメモを残す。後陣の参考になれば、幸いである。

最初に、一度導入に成功するのだが、なんと、ウェブサーバーで使うことを全く考えずに、いろいろなサイトを参考に導入していた。
MAMP環境のphpを変更するために、php7を導入される方が多いと思うが、グーグルの検索結果の上位は、Apacheなどを前提としていない。よく確認して、参考にした方が良い。

筆者のMacには phpenvが導入されており、phpを切り替える準備ができている。インストールした一度インスートルした7.1.5は下記で削除した。
$ phpenv uninstall 7.1.5


さて、phpがApacheで動くためには、install時の設定を変更しなければならない。
HomebrewでApache、PHP、MySQL、phpMyAdmin追加phpenvを用いたPHPのインストールに掲載されている。

デフォルト設定を変更します。
$ vi ~/.phpenv/plugins/php-build/share/php-build/default_configure_options
以下を追加します。
--with-apxs2=/usr/sbin/apxs
おもむろに下記インストールコマンドを実行するとエラーが出ます。
$ phpenv uninstall 7.1.5

:(途中省略)

apxs:Error: /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.12.xctoolchain/usr/local/bin/apr-1-config not found!.
下記サイトでどうやらhttpd22 かhttpd24 が必要とわかります。現在は2.0でした。
https://github.com/Homebrew/homebrew-apache/pull/147

最初のサイトにApache の導入も出ていたので、そのまま実行します。
$ brew tap homebrew/dupes
$ brew tap homebrew/apache
$ brew search httpd
$ sw_vers -productVersion | grep -E '^10\.([89]|10)' >/dev/null && bash -c "[ -d /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain ] && sudo -u $(ls -ld /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain | awk '{print $3}') bash -c 'ln -vs XcodeDefault.xctoolchain /Applications/Xcode.app/Contents/Developer/Toolchains/OSX$(sw_vers -productVersion).xctoolchain' || sudo bash -c 'mkdir -vp /Applications/Xcode.app/Contents/Developer/Toolchains/OSX$(sw_vers -productVersion).xctoolchain/usr && for i in bin include lib libexec share; do ln -s /usr/${i} /Applications/Xcode.app/Contents/Developer/Toolchains/OSX$(sw_vers -productVersion).xctoolchain/usr/${i}; done'"
$ brew install httpd24
無事に導入できました。

起動・停止は下記のコマンドです。
brew services stop homebrew/apache/httpd24
brew services start homebrew/apache/httpd24
導入リトライで、下記エラーとなります。
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ext/opcache/.libs/opcache.a(shared_alloc_shm.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: /var/tmp/php-build/source/7.1.5/modules/opcache.a(shared_alloc_shm.o) has no symbols
/usr/share/httpd/build/instdso.sh SH_LIBTOOL='/usr/local/opt/apr/libexec/build-1/libtool' libs/libphp7.so /usr/libexec/apache2
install: /usr/libexec/apache2/libphp7.so: Operation not permitted
apxs:Error: Command failed with rc=4653056
 opcacheとapxsのエラーです。opcacheはbrewで探して、7.1を導入。
$ brew search opcache
$ brew install homebrew/php/php71-opcache
リトライするも当然apxsのエラーは消えない。
phpenvとphp7をインストールする あたりを参考にしたが、埒があかない。
なんてことはない、Apacheの場所が違うので、apxsも違う。Apache2.4のapxsを確認して、default_configure_options の設定を修正。
--with-apxs2=/usr/local/opt/httpd24/bin/apxs
これで、コンパイルが通った。

他にも、エラーが出たので、記録しておく。

まずは、brewの導入
$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
bison
configure: WARNING: This bison version is not supported for regeneration of the Zend/PHP parsers (found: 2.3, min: 204, excluded: ).

$ brew install bison@2.7
バージョンを指定しないと、3.0.4がインストールされたが、2.7でないとうまくいかない。
$ brew unlink bison
$ brew link bison@2.7 --force
上記でリンクを貼り直し、.bashrcに下記を追記する
echo 'export PATH="/usr/local/opt/bison@2.7/bin:$PATH"' 
$source .bashrcも忘れずに

re2c
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.

下記でインストールして解決。
$ brew install re2c
OpenSSL
configure: error: Cannot find OpenSSL's

libxmlも怪しかったので、同時に導入
$ brew install openssl libxml2
Jpeg
configure: error: jpeglib.h not found.

他も怪しかったので、同時に導入
$ brew install libjpeg libpng libmcrypt
icu4c
configure: error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix and make sure icu-config works.

下記でインストールして、リンクを貼り直して、解決。
$ brew install icu4c
$ brew link icu4c --force

0 件のコメント: