macOSでPhalconでhello, world
目標としては、macOS(10.12.5)、標準で入っているApacheでphp 5.6 7 + phalconを動作させて、hello, worldを印字する。
※最初はphp5.6で進めてたのですが、途中で思い直したので下記は途中から無かったことにされます
とりあえず、php 5.6とphalconモジュールのインストール
すると、なにやらphp56が入った直後に下記のようなメッセージが表示される
The php.ini file can be found in:
/usr/local/etc/php/5.6/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"
PHP56 Extensions will always be compiled against this PHP. Please install them using --without-homebrew-php to enable compiling against system PHP.
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/php56)/bin:$PATH"✩✩✩✩ FPM ✩✩✩✩
To launch php-fpm on startup:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plistThe control script is located at /usr/local/opt/php56/sbin/php56-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.php56.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 php56 for more details.
To have launchd start homebrew/php/php56 now and restart at login:
brew services start homebrew/php/php56
php.ini は /usr/local/etc/php/5.6/php.ini にあるぞと。
/usr/local/bin にあるのはなにかなー?
ふむふむ。../Cellarなパスへのリンクだしbinがパスに含まれてるので、homebrewが作ったphpの実行バイナリへのリンクが入ってるっぽい
次に
が気になるので、とりあえず brew --prefix homebrew/php/php56 だけを実行してみる
なるほど。大体何がどんな感じに配置されたのかは大体わかったので次に進む。
あと、今回はnginxではなくApacheのモジュールとしてPHPを動かす目標なので、FPMはとりあえず放っとく。
ハマり防止で、念のため一応CLIからのphpコマンドが、システムデフォルトではなく、今brewでインストールしたphpを実行してくれることを確認しておこう。
大丈夫そう。
次に、phalconインストール完了時に下記が表示された
To finish installing phalcon for PHP 5.6:
* /usr/local/etc/php/5.6/conf.d/ext-phalcon.ini was created,
do not forget to remove it upon extension removal.
* Validate installation via one of the following methods:
*
* Using PHP from a webserver:
* - Restart your webserver.
* - Write a PHP page that calls "phpinfo();"
* - Load it in a browser and look for the info on the phalcon module.
* - If you see it, you have been successful!
*
* Using PHP from the command line:
* - Run `php -i "(command-line 'phpinfo()')"`
* - Look for the info on the phalcon module.
* - If you see it, you have been successful
phalcon.iniは/usr/local/etc/php/5.6/conf.d/ext-phalcon.iniに配備されたらしい
どれどれ…
ext-phalcon.iniに記載されている内容からすると phalcon.so モジュール本体は /usr/local/opt/php56-phalcon/phalcon.so に入ったと。
とりあえずまずは指示通り、CLIからPHPコマンドでphalconモジュールが認識されてるか確認しましょう。
のでとりあえず、次にapacheを起動しようと思ったけど、ここではたと気づく。
そういえばphp7も出てるし、どうせならphp7 + phalconにできないかなと。
というわけで、
ここまでやったことは無かったことにします
PHP公式によると2017/07/24時点では
Current Stable PHP 7.1.7
ということなので、7.2ではなく7.1を入れます
ほぼ下記を参考に(僕の記事いらないじゃん)
とりあえず標準のhttpdはとめて、brewでhttpd24を入れて使う(その方が楽そう&システム標準のApache弄るのなんとなく嫌)
これで80ポートで起動するように構成されてインストールされた。
% brew install php71 --with-httpd24
これでphpモジュールもビルドして配備してくれるらしいが…
configure: error: Cannot find libz
READ THIS: http://docs.brew.sh/Troubleshooting.html
途中でコケた。
調べてみたところ、
homebrewでphpのアップデート時に発生するlibzエラーの対処法 | cupOF Interests
という記事が。
深いことは考えず
% xcode-select --install
をやってから、
% brew install php71 --with-httpd24
をリトライ。今度はすんなり入った。
% brew install php71-phalcon
続いてphalconをインストール。
まずCLIのPHPからphalconモジュールが見えてるか確認
phalcon
OK. 次にapache動かしてみる。
% apachectl start
AH00557: httpd: apr_sockaddr_info_get() failed for oxycaster-mac-6.local
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
/etc/hostsに
が記載されていなかったので追記したことで、一個目のエラーは解決
二個目はhttpd.confかな
homebrewで入れたhttpd24のhttpd.confは /usr/local/etc/apache2/2.4/httpd.conf にあるので、この子に
ServerName localhost
を書いてやったら、エラー無くstart/stopできるようになった。
しかし、http://localhost:8080 でしかアクセスできない…。
--with-privileged-portsを付けてインストールしたはずなのに…。
とりあえず、httpd.confをもう一度開き、
Listen 8080
となっているところを
Listen 80
に変更してから
% apachectl start
(13)Permission denied: AH00072: make_sock: could not bind to address [::]:80
(13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs
なるほどー。なんか、OS側かなんかで80ポートは使えなくされてるのかなー。
だから「privileged-ports」なのかなー。
というわけで、何も考えず
% sudo apachectl start
したら動いた。
やったー。
最後はphalconの動作テストですね。
httpd.confの中身によると
/usr/local/var/www/htdocs/ がDocumentRootになってるっぽいから、とりあえずそこになんか置いてみよう。
とりあえずphalcon公式の
https://docs.phalconphp.com/en/3.2/application-micro
ここから適当にコピペしてファイル作ってアクセスしてみた。
もう一度こちらの記事を見る。
すると
<IfModule php7_module> AddType application/x-httpd-php .php </IfModule>
これですね。
というわけで、見落としてたので、上記をhttpd.confに書き加えて再起動。
うごいたーやったー。
おしまい。