Symfony2などを使用する場合、php 5.3のintlという国際化用拡張モジュールがインストールされていないと警告が出たり一部の機能が使用することができません。
私のMacBook Airに標準で入っているphpは5.3.6ですが、intlライブラリは含まれていません。
そこで、今回はMac OSX 10.7 Lionのhomebrewでintlライブラリが有効なphpをコンパイルし、加えてSymfony2のcheck.phpと呼ばれる環境チェックスクリプトで警告が出ないようにしてみます。私のphpは標準のものに対してライブラリを加えているため、皆さんよりも追加でインストールする項目が少ないかもしれません。
intlオプションが有効なphp formula
phpは標準でインストールされているため、homebrewのformulaは有りません。そこで、非公式に作られているformulaを使用します。
ココらへんの手順はいくつかの記事を参考にしました。
Bash:
-
brew install https://github.com/adamv/homebrew-alt/raw/master/duplicates/php.rb --with-mysql --with-intl
このとき、使用可能なオプションはoptionsコマンドで見ることができます。
TEXT:
-
brew options https://github.com/adamv/homebrew-alt/raw/master/duplicates/php.rb
-
php
-
--with-mysql
-
Include MySQL support
-
--with-mariadb
-
Include MariaDB support
-
--with-pgsql
-
Include PostgreSQL support
-
--with-mssql
-
Include MSSQL-DB support
-
--with-fpm
-
Enable building of the fpm SAPI executable (implies --without-apache)
-
--without-apache
-
Build without shared Apache 2.0 Handler module
-
--with-intl
-
Include internationalization support
-
--without-readline
-
Build without readline support
-
--with-gmp
-
Include GMP support
依存関係のあるlibxml2, mcrypt, icu4cなどもコンパイルされたあと、php本体もコンパイルされます。少し時間がかかるので、気長に待ちましょう.。
apacheの設定
phpのコンパイルが終了すると、以下のようなメッセージが出てきます。
TEXT:
-
For 10.5 and Apache:
-
Apache needs to run in 32-bit mode. You can either force Apache to start
-
in 32-bit mode or you can thin the Apache executable.
-
-
To enable PHP in Apache add the following to httpd.conf and restart Apache:
-
LoadModule php5_module /usr/local/Cellar/php/5.3.8/libexec/apache2/libphp5.so
-
-
The php.ini file can be found in:
-
/usr/local/etc/php.ini
-
-
'Fix' the default PEAR permissions and config:
-
chmod -R ug+w /usr/local/Cellar/php/5.3.8/lib/php
-
pear config-set php_ini /usr/local/etc/php.ini
例えば、Webサーバーがapache であれば「LoadModule php5_module /usr/local/Cellar/php/5.3.8/libexec/apache2/libphp5.so」を/etc/apache2/httpd.confに追加しまししょう。(元々入っているのphp5_moduleの行があるので、そちらは先頭に「#」をつけてコメントアウトしておきましょう。)
また新しいphpのiniファイルは「/usr/local/etc/php.ini」にありますので標準の場所と異なるのを気をつけましょう。
PEARライブラリののパーミッションも直しておくと便利です。「chmod -R ug+w /usr/local/Cellar/php/5.3.8/lib/php」というコマンドはすぐに通るかも知れませんが、「pear config-set php_ini /usr/local/etc/php.ini」のときに「/User/hogehoge/.pearrc」が作れないといったエラーがあった場合は、touchコマンドなどで事前に作成してあげて、パーミッションを777にでもしておいてください。
この時点で「php --version」としても、まだ元々のphpコマンドの方を参照しているでしょう。「which php」とすると「/usr/bin/php」を参照しているためです。コマンドラインが読み込まれた段階でbrew側のコマンド群を優先的に読みこむようにしましょう。ここではシェルがbashの想定で設定ファイルを書きます。
としてこの1行を追加しましょう。
TEXT:
-
export PATH="$(brew --prefix)/bin:$PATH"
最後に、bashの設定を反映させます。
この時点でphp --versionすると、新しいほうのphpが参照されているでしょう。
TEXT:
-
PHP 5.3.8 (cli) (built: Dec 4 2011 13:40:01)
-
Copyright (c) 1997-2011 The PHP Group
-
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
timezoneの設定
新しいphp.iniが作成されたので、最低限の設定としてtimezoneの設定をしておかないと、いずれ警告が出てしまいます。
Bash:
-
vi /usr/local/etc/php.ini
diff形式でいうと、以下のように変更してください。
TEXT:
-
-;date.timezone =
-
+date.timezone = "Asia/Tokyo"
APCのインストール
この時点でintl込のphpが動くのですが、最後にSymfony2用にAPCライブラリもコンパイルして用意しましょう。
と打つだけです。
インストール後、以下のようなメッセージが表示されます。
TEXT:
-
To finish installing APC:
-
* Add the following lines to php.ini:
-
[apc]
-
extension="/usr/local/Cellar/apc/3.1.9/apc.so"
-
apc.enabled=1
-
apc.shm_segments=1
-
apc.shm_size=64M
-
apc.ttl=7200
-
apc.user_ttl=7200
-
apc.num_files_hint=1024
-
apc.mmap_file_mask=/tmp/apc.XXXXXX
-
apc.enable_cli=1
-
* Restart your webserver
-
* Copy "/usr/local/Cellar/apc/3.1.9/apc.php" to any site to see APC's usage.
書いてあるとおりに、「/usr/local/etc/php.ini」にapcの設定を追加し、webサーバーを再起動します。最後のスクリプトのコピーは、APCの便利スクリプトなので、今はいりません。
Symfony2のapp/check.phpを見る
Symfony2のapp/check.phpを使って、環境が整っているかチェックしてみましょう。(その前にSymfony2の本体をDLしておいてください。)
Symfony2のルートディレクトリに移動し、以下のコマンドでapp/check.phpを実行します。
TEXT:
-
********************************
-
* *
-
* Symfony requirements check *
-
* *
-
********************************
-
-
php.ini used by PHP: /usr/local/etc/php.ini
-
-
** WARNING **
-
* The PHP CLI can use a different php.ini file
-
* than the one used with your web server.
-
* If this is the case, please ALSO launch this
-
* utility from your web server.
-
** WARNING **
-
-
** Mandatory requirements **
-
-
OK Checking that PHP version is at least 5.3.2 (5.3.8 installed)
-
OK Checking that the "date.timezone" setting is set
-
OK Checking that app/cache/ directory is writable
-
OK Checking that the app/logs/ directory is writable
-
OK Checking that the json_encode() is available
-
OK Checking that the SQLite3 or PDO_SQLite extension is available
-
OK Checking that the session_start() is available
-
OK Checking that the ctype_alpha() is available
-
OK Checking that the token_get_all() is available
-
OK Checking that the APC version is at least 3.0.17
-
-
** Optional checks **
-
-
OK Checking that the PHP-XML module is installed
-
OK Checking that the token_get_all() function is available
-
OK Checking that the mb_strlen() function is available
-
OK Checking that the iconv() function is available
-
OK Checking that the utf8_decode() is available
-
OK Checking that the posix_isatty() is available
-
OK Checking that the intl extension is available
-
OK Checking that the intl ICU version is at least 4+
-
OK Checking that a PHP accelerator is installed
-
OK Checking that php.ini has short_open_tag set to off
-
OK Checking that php.ini has magic_quotes_gpc set to off
-
OK Checking that php.ini has register_globals set to off
-
OK Checking that php.ini has session.auto_start set to off
-
-
** Optional checks (Doctrine) **
-
-
OK Checking that PDO is installed
-
OK Checking that PDO has some drivers installed: mysql, sqlite, sqlite2
基本的な警告はなくなりました。これでSymfony2を楽しむ環境が整いましたね!