IBM BlueMixでCakePhpを動かす

性分的にオープンβもクローズドβも二つ返事で「やります」と言ってしまうwので、
IBM BlueMixというPaaSを使ってみることに。。


個人的にはphpさえあれば満足な人なので、
ひとまずCakePHPを動かすのを目標にしてみました。
(なぜかBlueMixでCakePHPを動かしたって記事をググっても見当たらないし・・)



なにはともあれ、BlueMixのアカウントを作成します。

IBM BlueMix
https://ace.ng.bluemix.net/


次にCreate An Applicationします。
その際に「Library for Java」を選んでおきます。
(って言うのもBlueMixではPHPはサポートしてないwけど、
 phpのBuildPackがあるのでそれを使います)







で、Applicationのコンテナができるのでここにプログラムを入れるんですが
プログラムをアップロードするのはCloudFoundryが提供しているcliでデプロイします


https://github.com/cloudfoundry/cli/releases
から自分は以下をダウンロードしてインストールしました


Installers
Mac OS X 64 bit



これでcfコマンドが使えるようになるのでセットアップします

$ cf api https://api.ng.bluemix.net
Setting api endpoint to https://api.ng.bluemix.net...
OK

API endpoint: https://api.ng.bluemix.net (API version: 2.2.0)
Not logged in. Use 'cf login' to log in.

loginはBlueMixのIBM IDのログインパスワード

$ cf login
API endpoint: https://api.ng.bluemix.net

Email> xxxxxx@xxxxxx.com

Password> 
Authenticating...
OK

Targeted org xxxxxx@xxxxxx.com

Targeted space dev


API endpoint: https://api.ng.bluemix.net (API version: 2.2.0)
User:         xxxxxx@xxxxxx.com
Org:          xxxxxx@xxxxxx.com
Space:        dev

ひとまずHelloしましょう

$ mkdir bluemixhello
$ cd bluemixhello/
$ vi index.php
---
<?php
echo "hello world";
---


作ったhello worldアプリ(w)をBlueMixにデプロイします
デプロイする際はcf-php-build-packを使います

$ cf push -b https://github.com/dmikusa-pivotal/cf-php-build-pack.git imaiworks1
Updating app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

Uploading imaiworks1...
Uploading app files from: /Users/imaiworks/bluemixhello
Uploading 159, 1 files
OK

Stopping app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

Starting app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
-----> Downloaded app package (4.0K)
OK
-----> Downloaded app buildpack cache (12M)
Cloning into '/tmp/buildpacks/cf-php-build-pack'...
Installing HTTPD
Installing PHP
Finished: [2014-06-29 04:33:39.205132]
-----> Uploading droplet (12M)

1 of 1 instances running

App started

Showing health and status for app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

requested state: started
instances: 1/1
usage: 512M x 1 instances
urls: imaiworks1.mybluemix.net

     state     since                    cpu    memory          disk   
#0   running   2014-06-29 01:33:48 PM   0.0%   47.3M of 512M   34.2M of 1G  

これでBlieMixに上がったのでブラウザで確認します。

死ぬほど簡単。。



次は本題のCakePHP


ここでちょっとぶつかったのですが
cf-php-build-packのDocumentRootがhtdocsで
CakePHPは概ねapp/webrootなのでできればこれにしたい
ディレクトリに入れるなら別にそんなことしなくてもいいけどね)
ので、いろいろ試してこれかな?って思ったのは
cf-php-build-packのdefaults/config/httpd/2.4.x/httpd.conf内の

     DocumentRoot "${HOME}/htdocs"

     DocumentRoot "${HOME}/htdocs/app/webroot"

と書き換えることと、


cakephpのrootにあるindex.php

     define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib');

     define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . '../lib');

としたこと。


改変したbuilpackは
https://github.com/imaiworks/cf-php-build-pack-cakephp
にあげてありますのでご自由に・・


ってことでCakePHPの準備をします
cakephp-2.5.2.tar.gzをダウンロード

tar xzvf cakephp-2.5.2.tar.gz
cd cakephp-2.5.2
vi index.php
---
-        define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . 'lib');
+        define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . '../lib');
---

(SaltやDB設定は個々にやっておいてくださいまし)


BlueMixにデプロイします

$ cf push -b https://github.com/imaiworks/cf-php-build-pack-cakephp.git imaiworks1
Updating app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

Uploading imaiworks1...
Uploading app files from: /Users/imaiworks/bluemix/cakephp-2.5.2
Uploading 5.5M, 1440 files
OK

Stopping app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK

Starting app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com...
OK
-----> Downloaded app package (2.1M)
-----> Downloaded app buildpack cache (12M)
Cloning into '/tmp/buildpacks/cf-php-build-pack-cakephp'...
Installing HTTPD
Installing PHP
Finished: [2014-06-29 04:51:29.022169]
-----> Uploading droplet (13M)

1 of 1 instances running

App started

Showing health and status for app imaiworks1 in org xxxxx@xxxxxxxx.com / space dev as xxxxx@xxxxxxxx.com..
OK

requested state: started
instances: 1/1
usage: 512M x 1 instances
urls: imaiworks1.mybluemix.net

     state     since                    cpu    memory          disk   
#0   running   2014-06-29 01:51:41 PM   0.0%   59.3M of 512M   46.5M of 1G   

たぶんこれでうまくいくはずです




cfを知らないので多くは語れないですが、
BlueMixはGoogle AppEngineとかHeroku的な感覚でした。
AWSだとAWS Elastic Beanstalkに近いですね。
今今動いているシステムをBlueMixに移すのはちょっと厳しいけど
新しくサービスを作る立ち上げるのならAWSよりはるかに簡単に扱えます。
で、コンテンツの更新は基本DBやCache等で行い
ファイル更新andデプロイでは基本やらないなどの
ちょっと違った考え方で作りこむのがいいでしょうね。
(気軽にデプロイ出来るとはいえ、設定ミスるとデプロイでコンテナが破棄されたままになるのはちょっと危険・・)
あとは、ログの収集やCronやバッチを動かすのはどうするんだろ?ってところと
デプロイしたアプリが世代管理されていて
いざという時、世代を戻せるとかそういうリカバリ機能があるといいなー


BlueMix、使いこなせたらインフラ管理しなくてもいいから超楽しいかもー



こちらの記事はとても参考にさせていただきました。
ありがとうございました。


まだプログラマーですが何か?
BlueMix 上で PHP アプリを動かす
http://dotnsf.blog.jp/archives/1000985586.html