背景
PHPなんて相当昔に使って以来全く使っていなかったのだけど、わけあってちょっとしたサンプルを動かして挙動確認をしなくてはならなくなり、MacにPHP/PECL環境を作ったので備忘録。
環境
macOS Catalina 10.15.3
やりたいこと
PHPで書かれているサンプルプログラムをもらったのだが、その中でOAuth1.0を使っているので、
- PHP
- OAuth
が動く環境を作りたい
結論
多分以下をやればよい。
% brew instal php
% brew install autoconf
% brew install pcre
% pecl install oauth
以下も必要かもしれない。
% brew update
% brew upgrade
以下苦心の跡
まずMacにPHPをインストールしないとダメだろうと思った
・・・しなくてはならないと思っていたのですが、普通に最初からphpコマンドがあることを知らなかった。
% which php
/usr/bin/php
へぇ・・・と思った
でもこれが落とし穴でしたね。実際インストールし直さないとダメだったわけです。
PECL環境を作る
今回のサンプルは、oauth認証のAPI実行プログラムだったのだけど、そのためにはこいつが必要らしい。
そのためにはPECLが使えないといけないのだけど、さすがにpeclコマンドはなかった。
のでそのインストール方法。
こちらが参考になりました。
https://qiita.com/applexco/items/f10e87477dc5d41cf751
ここに書かれているとおりすすめると、こんなことを聞かれます。
% php -d detect_unicode=0 ./go-pear.phar
Below is a suggested file layout for your new PEAR installation. To
change individual locations, type the number in front of the
directory. Type 'all' to change all of them or simply press Enter to
accept these locations.
1. Installation base ($prefix) : /Users/yuzu/pear
2. Temporary directory for processing : /tmp/pear/install
3. Temporary directory for downloads : /tmp/pear/install
4. Binaries directory : /Users/yuzu/pear/bin
5. PHP code directory ($php_dir) : /Users/yuzu/pear/share/pear
6. Documentation directory : /Users/yuzu/pear/docs
7. Data directory : /Users/yuzu/pear/data
8. User-modifiable configuration files directory : /Users/yuzu/pear/cfg
9. Public Web Files directory : /Users/yuzu/pear/www
10. System manual pages directory : /Users/yuzu/pear/man
11. Tests directory : /Users/yuzu/pear/tests
12. Name of configuration file : /Users/yuzu/.pearrc
1-12, 'all' or Enter to continue:
特に変更する気もなかったのでallとしました。
あとは $HOME/pear/bin
にコマンドがインストールされるのでそこにPATHを通せばOK。
OAuthのインストール
peclでOAuthをインストールしようとしてみます。
% pecl install oauth
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
downloading oauth-2.0.5.tgz ...
Starting to download oauth-2.0.5.tgz (49,641 bytes)
.............done: 49,641 bytes
6 source files, building
running: phpize
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
こんな感じでNGになりました。
autoconfがないのが原因らしいので追加。
% brew install autoconf
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
aws-elasticbeanstalk firebase-cli nushell
cfn-lint fonttools serverless
chronograf glib sphinx-doc
cointop gmp subversion
devspace istioctl terragrunt
dwarfutils juju topgrade
erlang jupyterlab wtf
ethereum kube-aws
exploitdb node@12
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.catalina.bott
==> Downloading from https://akamai.bintray.com/ca/ca510b350e941fb9395522a03f9d2
######################################################################## 100.0%
==> Pouring autoconf-2.69.catalina.bottle.4.tar.gz
🍺 /usr/local/Cellar/autoconf/2.69: 67 files, 3.0MB
再チャレンジ。
(略)
configure: error: Couldn't find pcre.h, try installing the libpcre development/headers package
ERROR: `/private/tmp/pear/install/oauth/configure --with-php-config=/usr/bin/php-config' failed
まだなんか出る。
今度はpcreがないのが原因らしいので同じくbewする。
% brew install pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.44.catalina.bottle.t
==> Downloading from https://akamai.bintray.com/f8/f8ac266e04f984fa55091a43f0fdc
######################################################################## 100.0%
==> Pouring pcre-8.44.catalina.bottle.tar.gz
🍺 /usr/local/Cellar/pcre/8.44: 204 files, 5.5MB
再び。
In file included from /private/tmp/pear/install/oauth/oauth.c:12:
/private/tmp/pear/install/oauth/php_oauth.h:18:10: fatal error: 'php.h' file not found
#include "php.h"
^~~~~~~
1 error generated.
make: *** [oauth.lo] Error 1
ERROR: `make' failed
うーん。
このphp.hについては、こちらを参考にしたのですが、私の場合はこれだけでは解消しませんでした。
何か違いがあるのでしょうね。
で見つけたのが こちら 。
brewでphpインストールし直す というもの。
% brew install php
ただこれだけだと従来のphp(/usr/bin/php)が先にヒットするので、/usr/local/binが先にヒットするようにPATHをいじる必要はあります。
ここまでやると、 pecl install oauth
が通るようになりました。
まとめ
ということで、macOS Catalinaでphpサンプル(要peclモジュール)を動かすやり方についてまとめました。
ホント久しぶりにPHP触ったのと、今回のサンプルを動かしたかっただけなので環境づくりもやっつけ感満載ですが参考になれば。