CodlessCode
PHP

phpbrew で PHP 7.1 / 8.0 をインストールした( + openssl / + gd )

日常業務でさまざまなバージョンのPHPを触るので、
phpbrew でバージョン管理しています。
今日は PHP 7.1 の案件をやることになって、
phpbrew でインストールしました。

で、いろいろエラーが出たので、
次は困らないように備忘録として置いておこうと思います。

近いうちに、この続きとして
PHP 8.1 を phpbrew でインストールしたときの備忘録もまとめようと思ってます。

日常業務のログ…ということで、文体がバラバラなんですが、
すこしでも私と同じようなことで困っている方の助けになればと思います。

 

  

phpbrwe で PHP をインストールする方法 結論

この時の環境

  • macOS Big Sur(バージョン11.6)
  • phpbrew – 1.27.0
  • cliframework core: 2.5.4
  • いつも使ってるPHP – php-8.0.3
  • パッケージ管理はHomebrew
  • 依存関係のライブラリはbrew install済み

.zshrc に以下を追記

export CFLAGS="-Wno-error=implicit-function-declaration -DU_DEFINE_FALSE_AND_TRUE=1"
export CXXFLAGS="-Wno-error=implicit-function-declaration -DU_DEFINE_FALSE_AND_TRUE=1"

PHP 7.1 のインストール

$ phpbrew install 7.1.33 +default +mysql +fpm +zlib="$(brew --prefix zlib)" +openssl=/opt/homebrew/opt/openssl@1.1 -- --with-external-pcre=$(brew --prefix pcre2)

(… ターミナル再起動 …)

PHP 7.1 拡張機能のインストール

$ phpbrew switch php-7.1.33
(… ターミナル再起動 …)
$ phpbrew ext install gd -- --with-external-gd="$(brew --prefix gd)" --with-jpeg-dir="$(brew --prefix jpeg)" --with-png-dir="$(brew --prefix libpng)"  --with-webp-dir="$(brew --prefix webp)" --with-freetype-dir="$(brew --prefix freetype)" --with-libz-dir="$(brew --prefix libzip)" --with-zlib-dir="$(brew --prefix zlib)"
(… ターミナル再起動 …)

PHP 8.0.11 のインストール(おまけ)

$ phpbrew install php-8.0.11 +default +mysql +fpm +openssl=/opt/homebrew/opt/openssl@1.1 +zlib="$(brew --prefix zlib)" -- --with-external-pcre=$(brew --prefix pcre2)
(… ターミナル再起動 …)

PHP 8.0.11 拡張機能のインストール(おまけ)

$ phpbrew switch php-8.0.11
(… ターミナル再起動 …)
$ phpbrew ext install gd -- --with-external-gd="$(brew --prefix gd)" --with-jpeg --with-webp --with-freetype --with-xpm
(… ターミナル再起動 …)
 
 

試行錯誤と経緯

  

🧨 openssl の指定

ビルド時にopensslオプションを指定する必要がありますが、必要なのはバージョン1系なので、openssl@1の最新がインストールされているか確認しましょう。

$ ls /opt/homebrew/opt | grep openssl

openssl
openssl@1.1  # 👈これがあったらOK
openssl@3

私の場合はインストール済みだったので、オプションで+openssl=/opt/homebrew/opt/openssl@1.1と指定すればOKでした。
なければ以下のようにしてインストールします。

$ brew install openssl@1.1
$ brew install openssl@1.*  # 👈これでもOK

==> Summary
🍺  /opt/homebrew/Cellar/openssl@1.1/1.1.1m: 8,081 files, 18MB
==> Running `brew cleanup openssl@1.1`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Removing: /opt/homebrew/Cellar/openssl@1.1/1.1.1l_1... (8,073 files, 18MB)

ls /opt/homebrew/opt | grep opensslで、openssl@1.1があることを確認できると思います。
インストールしたopensslバージョン1系をビルドオプションで指定しましょう!

  

🧨 PHP 7.1.33 で gd を有効にする

ログで詳細を確認すると…

$ tail $HOME/.phpbrew/build/php-7.2.34/ext/gd/build.log

checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... re2c
checking for re2c version... 2.2 (ok)
checking for gawk... no
checking for nawk... no
checking for awk... awk
checking if awk is broken... no
checking for GD support... yes, shared
checking for the location of libwebp... no
checking for the location of libjpeg... no
checking for the location of libpng... no
checking for the location of libz... no
checking for the location of libXpm... no
checking for FreeType 2... no
checking whether to enable JIS-mapped Japanese font support in GD... no
If configure fails try --with-webp-dir=< DIR >
If configure fails try --with-jpeg-dir=< DIR >

オプションを指定して
libwebp、libjpeg、libpng、libz、FreeType 2
の箇所をyesにすると成功した。

参考ページはこちら。
https://www.php.net/manual/ja/image.installation.php

PHP 7.4以降であるかどうかで、configureオプションが微妙に変わってくる。

 

HomebrewでインストールしたGDのパスを指定

PHP 7.4 前は –with-gd(PHPにバンドルされたGDライブラリを指定するオプション)を使用する。

PHP 7.4以降/8系では –with-external-gd を使ってgdのパスを指定する。
誤ったオプションを使うと「unrecognize」されて無効になる。
config.logを cat / grep して確認したりする。

$ brew install gd
--with-gd="$(brew --prefix gd)" オプションを追加
 

libwebpの指定

$ brew install webp
--with-webp-dir="$(brew --prefix webp)" オプションを追加
 

libjpegの指定

$ brew install jpeg
--with-jpeg-dir="$(brew --prefix jpeg)" オプションを追加
 

libpngの指定

$ brew install libpng
--with-png-dir="$(brew --prefix libpng)" オプションを追加

PHP 7.4以降/8系では PNGサポート必須になっており、オプションは不要。

7.4 以前は任意だけど、これ有効にしたっけ?ってならないように。

 

zlibの指定

PNGをサポートする場合は zlib 必須なのでオプションをつける。

$ brew install zlib
--with-zlib-dir="$(brew --prefix zlib)"

PHP 7.4以降/8系では、libpng と zlib が無いとそもそもビルドできない
(PNGサポート必須になっており、その流れで zlib も必須なので)

7.4 以前 の場合は –with-png-dir と –with-zlib-dir をセットで指定する。

 

FreeTypeの指定

$ brew install freetype
--with-freetype-dir="$(brew --prefix freetype)" オプションを追加
 

最終的なコマンド

$ phpbrew ext install gd -- --with-gd="$(brew --prefix gd)" --with-jpeg-dir="$(brew --prefix jpeg)" --with-png-dir="$(brew --prefix libpng)"  --with-webp-dir="$(brew --prefix webp)" --with-freetype-dir="$(brew --prefix freetype)" --with-zlib-dir="$(brew --prefix zlib)"
 

成功した時のconfig.log抜粋

configure:4541: checking for GD support
configure:4582: result: yes, shared
configure:4591: checking for the location of libwebp
configure:4607: result: /opt/homebrew/opt/webp
configure:4618: checking for the location of libjpeg
configure:4634: result: /opt/homebrew/opt/jpeg
configure:4645: checking for the location of libpng
configure:4661: result: /opt/homebrew/opt/libpng
configure:4672: checking for the location of libz
configure:4688: result: /opt/homebrew/opt/zlib
configure:4698: checking for the location of libXpm
configure:4714: result: no
configure:4723: checking for FreeType 2
configure:4739: result: /opt/homebrew/opt/freetype

意図した通りのパスになっていればOK。
念の為$ cat /XXX/config.log | grep unrecognizedして、無効になったオプション等ないか見ておく。

 

🧨 PHP 8.0.11 で gd を有効にする

最終的なコマンド

$ phpbrew ext install gd -- --with-external-gd="$(brew --prefix gd)" --with-jpeg --with-webp --with-freetype --with-xpm
 

7.4以降/8系での補足

  • –with-○○-dir のオプションがいくつか廃止されているため、–with-○○ に置き換えたりする。
  • libpngとzlibは必須になっているため、事前にインストールしておく。そしてオプション不要。
  • PHP7.4以前でXPMサポートを有効にするのは断念した。8系では–with-xpmで勝った。簡単すぎて腑に落ちない…
 

成功したときのconfig.log抜粋

configure:4509: checking for GD support
configure:4549: result: yes, shared
configure:4558: checking for external libgd
configure:4574: result: /opt/homebrew/opt/gd
configure:4584: checking for libwebp
configure:4600: result: yes
configure:4611: checking for libjpeg
configure:4627: result: yes
configure:4637: checking for libXpm
configure:4653: result: yes
configure:4662: checking for FreeType 2
configure:4678: result: yes
 

ちゃんとyesになってるのでOK。

ちなみに、gd以外の「–with-○○-dir」で指定したオプションは unrecognized となっていて
全部 result: no って言われた。

PHPのバージョンによってオプションが変わるので注意。

 
 

その他のエラー試行錯誤

🧨 passing argument to parameter ‘rsa’ here

#     define OSSL_DEPRECATED(since) __attribute__((deprecated))
^
$HOME/.phpbrew/build/php-8.0.11/ext/openssl/openssl.c:6393:6: warning: passing 'const struct rsa_st *' to parameter of type 'RSA *' (aka 'struct rsa_st *') discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
EVP_PKEY_get0_RSA(pkey),
^~~~~~~~~~~~~~~~~~~~~~~
/opt/homebrew/Cellar/openssl@3/3.0.0_1/include/openssl/rsa.h:289:29: note: passing argument to parameter 'rsa' here
RSA *rsa, int padding);
^
108 warnings and 1 error generated.
make: *** [ext/openssl/openssl.lo] Error 1

私の環境にはopenssl@1.1openssl@3があった。

現在Requiredは@1.1なので、オプションで以下のように指定した。
Not Found opensslみたいなエラーもこれで解決できた。

+openssl=/opt/homebrew/opt/openssl@1.1
 
 

🧨 configure: error: PNG support requires ZLIB. Use –with-zlib-dir=< DIR >

PNGサポートを有効にする場合は zlib 必須っていうことのエラー。

  • PHPビルド時は +zlib=”$(brew –prefix zlib)”
  • 拡張機能インストール時は –with-zlib-dir=“$(brew —prefix zlib)”
 
 

🧨 PEAR package PHP_Archive not installed: generated phar will require PHP’s phar extension be enabled.

ログ無くしてうろ覚え…
オプションで指定して回避。

--with-external-pcre=$(brew --prefix pcre2)
 
 

phpbrew で PHP インストールする方法 まとめ(3つ)

調べてると、phpenv でも似たようなエラーが散見された。
エラーを調査するときは、「phpenv gd 有効」みたいにphpenvとして検索してもヒントになったかも。
—–

PHP のビルドは7.4以前か 7.4 / 8.0 系で変わるので、configureオプションは臨機応変に変えていく。
—–

ビルドに成功しても、config.logとかで「そのオプション、unrecognized!」と言われていることがある。
この場合、そのオプションは無効になって無視される。

エラーがなくても
build.logやconfig.logを $ cat XXX.log | grep unrecognized などしてログは確認する。
(結構あったんだよね…反省)
—–