macOSでPhalconでhello, world

目標としては、macOS(10.12.5)、標準で入っているApachephp 5.6 7 + phalconを動作させて、hello, worldを印字する。

 

※最初はphp5.6で進めてたのですが、途中で思い直したので下記は途中から無かったことにされます

とりあえず、php 5.6とphalconモジュールのインストー

% brew install php56 php56-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.

✩✩✩✩ PHP CLI ✩✩✩✩

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.plist

The 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 にあるのはなにかなー?

% ls -la /usr/local/bin | grep -i php
lrwxr-xr-x 1 oxycaster admin 33 7 23 00:20 phar -> ../Cellar/php56/5.6.30_6/bin/phar
lrwxr-xr-x 1 oxycaster admin 38 7 23 00:20 phar.phar -> ../Cellar/php56/5.6.30_6/bin/phar.phar
lrwxr-xr-x 1 oxycaster admin 32 7 23 00:20 php -> ../Cellar/php56/5.6.30_6/bin/php
lrwxr-xr-x 1 oxycaster admin 36 7 23 00:20 php-cgi -> ../Cellar/php56/5.6.30_6/bin/php-cgi
lrwxr-xr-x 1 oxycaster admin 39 7 23 00:20 php-config -> ../Cellar/php56/5.6.30_6/bin/php-config
lrwxr-xr-x 1 oxycaster admin 35 7 23 00:20 phpize -> ../Cellar/php56/5.6.30_6/bin/phpize 

 ふむふむ。../Cellarなパスへのリンクだしbinがパスに含まれてるので、homebrewが作ったphpの実行バイナリへのリンクが入ってるっぽい

 

次に

export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH" 

が気になるので、とりあえず brew --prefix homebrew/php/php56 だけを実行してみる

% brew --prefix homebrew/php/php56
/usr/local/opt/php56

% ls -la /usr/local/opt/php56/bin
total 44760
drwxr-xr-x 8 oxycaster admin 272 7 23 00:20 .
drwxr-xr-x 16 oxycaster admin 544 7 23 00:20 ..
lrwxr-xr-x 1 oxycaster admin 9 1 19 2017 phar -> phar.phar
-rwxr-xr-x 1 oxycaster admin 14851 7 23 00:20 phar.phar
-rwxr-xr-x 1 oxycaster admin 11448936 7 23 00:20 php
-rwxr-xr-x 1 oxycaster admin 11426200 7 23 00:20 php-cgi
-rwxr-xr-x 1 oxycaster admin 4489 7 23 00:20 php-config
-rwxr-xr-x 1 oxycaster admin 4583 7 23 00:20 phpize

なるほど。大体何がどんな感じに配置されたのかは大体わかったので次に進む。

あと、今回はnginxではなくApacheのモジュールとしてPHPを動かす目標なので、FPMはとりあえず放っとく。

ハマり防止で、念のため一応CLIからのphpコマンドが、システムデフォルトではなく、今brewでインストールしたphpを実行してくれることを確認しておこう。

% where php
/usr/local/bin/php
/usr/bin/php

% which php
/usr/local/bin/php

% ls -la /usr/local/bin/php
lrwxr-xr-x 1 oxycaster admin 32 7 23 00:20 /usr/local/bin/php -> ../Cellar/php56/5.6.30_6/bin/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に配備されたらしい

どれどれ…

% cat /usr/local/etc/php/5.6/conf.d/ext-phalcon.ini
[phalcon]
extension="/usr/local/opt/php56-phalcon/phalcon.so" 

ext-phalcon.iniに記載されている内容からすると phalcon.so モジュール本体は /usr/local/opt/php56-phalcon/phalcon.so に入ったと。

とりあえずまずは指示通り、CLIからPHPコマンドでphalconモジュールが認識されてるか確認しましょう。

% php -i "(command-line 'phpinfo()')" | grep -i "phalcon"
Additional .ini files parsed => /usr/local/etc/php/5.6/conf.d/ext-phalcon.ini
PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
phalcon
phalcon => enabled
Author => Phalcon Team and contributors
phalcon.db.escape_identifiers => On => On
phalcon.db.force_casting => Off => Off
phalcon.orm.cast_on_hydrate => Off => Off
phalcon.orm.column_renaming => On => On
phalcon.orm.disable_assign_setters => Off => Off
phalcon.orm.enable_implicit_joins => On => On
phalcon.orm.enable_literals => On => On
phalcon.orm.events => On => On
phalcon.orm.exception_on_failed_save => Off => Off
phalcon.orm.ignore_unknown_columns => Off => Off
phalcon.orm.late_state_binding => Off => Off
phalcon.orm.not_null_validations => On => On
phalcon.orm.update_snapshot_on_save => On => On
phalcon.orm.virtual_foreign_keys => On => On 

CLIPHPコマンド的には大丈夫っぽい

のでとりあえず、次にapacheを起動しようと思ったけど、ここではたと気づく。

そういえばphp7も出てるし、どうせならphp7 + phalconにできないかなと。

というわけで、

ここまでやったことは無かったことにします

PHP公式によると2017/07/24時点では

Current Stable PHP 7.1.7

ということなので、7.2ではなく7.1を入れます

 

ほぼ下記を参考に(僕の記事いらないじゃん)

qiita.com

とりあえず標準のhttpdはとめて、brewでhttpd24を入れて使う(その方が楽そう&システム標準のApache弄るのなんとなく嫌)

% brew install httpd24 --with-privileged-ports 

 これで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をインストール。 

まずCLIPHPからphalconモジュールが見えてるか確認

% php -m | grep 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に

127.0.0.1    oxycaster-mac-6.local

が記載されていなかったので追記したことで、一個目のエラーは解決

二個目は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

ここから適当にコピペしてファイル作ってアクセスしてみた。

するとPHPソースコードがそのまま表示されてしまった。

qiita.com

もう一度こちらの記事を見る。

すると

<IfModule php7_module>
  AddType application/x-httpd-php .php
</IfModule>

 これですね。

というわけで、見落としてたので、上記をhttpd.confに書き加えて再起動。

うごいたーやったー。

おしまい。