m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

phpのphalconをセットアップ

phalcon

PHPの高速フレームワークの1つ
現時点ではhhvmが出ているので最速とはいえないけど拡張モジュールを使用しているのでかなり早いと思われる。

環境

CentOS 64bit(vagrant)

セットアップ

git clone
git clone git://github.com/phalcon/cphalcon.git

Cloning into 'cphalcon'...
remote: Reusing existing pack: 64539, done.
Receiving objects: 100% (64539/64539), 51.26 MiB | 606.00 KiB/s, done.
remote: Total 64539 (delta 0), reused 0 (delta 0)
Resolving deltas: 100% (49169/49169), done.
Checking connectivity... done.

ビルド
cd cphalcon/build/
sudo ./install
                                                                                                                                          • -

Libraries have been installed in:
/home/vagrant/cphalcon/build/64bits/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.

                                                                                                                                          • -

Build complete.
Don't forget to run 'make test'.

Installing shared extensions: /usr/lib64/php/modules/

Thanks for compiling Phalcon!
Build succeed: Please restart your web server to complete the installation

ビルド結果確認
$ ll /home/vagrant/cphalcon/build/64bits/modules
合計 3212
-rwxr-xr-x 1 root root 3286847  6月  8 07:14 2014 phalcon.so
$
モジュール確認
$ ll /usr/lib64/php/modules/ | grep phalcon.so
-rwxr-xr-x 1 root root 3286847  6月  8 07:14 2014 phalcon.so
$
拡張モジュールとしてconfファイルを作成して配置
$ sudo vim /etc/php.d/phalcon.ini
----
extension=phalcon.so
----

/etc/php.confの場合は読み込みの順番でエラーになる可能性がある

読み込み確認
$ php -i |grep Phalcon
Phalcon Framework => enabled
Phalcon Version => 1.3.2
$
確認2
$ php -r "print_r(get_loaded_extensions());" |grep phalcon
    [51] => phalcon
$

phalcon devtoolsのインストール

ディレクトリを作成して、composerを準備
mkdir phalcon
cd phalcon
curl -s http://getcomposer.org/installer | php
cat <<'_EOT_' > composer.json
{
    "require": {
        "phalcon/devtools": "dev-master"
    }
}
_EOT_
インストール
php composer.phar install

Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing phalcon/devtools (dev-master c412ace)
Cloning c412ace79f5812fa9c250a5518563c06f1886a0b

Writing lock file
Generating autoload files

インストールが完了したら配置場所を共通の場所へ変更
cd
sudo mv phalcon /usr/local/
sudo ln -s /usr/local/phalcon/vendor/phalcon/devtools/phalcon.php /usr/bin/phalcon
sudo chmod ugo+x /usr/bin/phalcon
実行確認
$ phalcon

Phalcon DevTools (1.3.2 ALPHA 1)

Available commands:
  commands (alias of: list, enumerate)
  controller (alias of: create-controller)
  model (alias of: create-model)
  all-models (alias of: create-all-models)
  project (alias of: create-project)
  scaffold
  migration
  webtools

$
プロジェクトの作成
$ phalcon project sample1

Phalcon DevTools (1.3.2 ALPHA 1)


  Success: Controller "index" was successfully created.


  Success: Project 'sample1' was successfully created.

$
作成結果
$ tree sample1/
sample1/
├── app
│   ├── cache
│   ├── config
│   │   ├── config.php
│   │   ├── loader.php
│   │   └── services.php
│   ├── controllers
│   │   ├── ControllerBase.php
│   │   └── IndexController.php
│   ├── models
│   └── views
│       ├── index
│       │   └── index.volt
│       ├── index.volt
│       └── layouts
├── index.html
└── public
    ├── css
    ├── files
    ├── img
    ├── index.php
    ├── js
    └── temp

14 directories, 9 files
$
ビルドインサーバで起動
$ cd sample1/public/
$ php -S 192.168.33.10:1234
結果

f:id:m_shige1979:20140608181931p:plain

nginxで起動
cat <<'_EOT_' > dev.example.com.conf
server {
    listen       80;
    server_name  dev.example.com
                 192.168.33.10
                 ;

    index index.php index.html index.htm;
    set $root_path '/var/www/dev1.example.com/public';
    root $root_path;

    access_log  /var/log/nginx/dev.example.com/access.log  main;
    error_log   /var/log/nginx/dev.example.com/error.log;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }
    location ~ (\.htaccess|\.git|\.svn) {
        deny  all;
    }
    
    charset utf-8;
    
}

_EOT_
service nginx restart
結果

f:id:m_shige1979:20140608181947p:plain

まとめ

普段意識しない拡張モジュールを組み込むからその辺りの対応で手こずりました。
パフォーマンスの差をどのように調べていくかの方法もちょっと調査してできることとできないことを理解していく必要がありそう