NginxとFuelPHPの開発環境構築

Nginx、php-fpm、FuelPHPの環境で開発することになったので、 開発環境を構築してみました。

<参考書籍>
ハイパフォーマンスHTTPサーバ Nginx入門
はじめてのフレームワークとしてのFuelPHP
<参考サイト>
FuelPHPをNginxで動かしてみる

PHPインストール

ソースのダウンロードと展開

$ wget -O php-5.3.15.tar.gz http://jp2.php.net/get/php-5.3.15.tar.gz/from/jp.php.net/mirror
--2012-08-13 18:13:07--  http://jp2.php.net/get/php-5.3.15.tar.gz/from/jp.php.net/mirror
jp2.php.net (jp2.php.net) をDNSに問いあわせています... 49.212.134.217
jp2.php.net (jp2.php.net)|49.212.134.217|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: http://jp.php.net/distributions/php-5.3.15.tar.gz [続く]
--2012-08-13 18:13:08--  http://jp.php.net/distributions/php-5.3.15.tar.gz
jp.php.net (jp.php.net) をDNSに問いあわせています... 61.195.146.164
jp.php.net (jp.php.net)|61.195.146.164|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 14806681 (14M) [application/x-gzip]
`php-5.3.15.tar.gz' に保存中

100%[======================================>] 14,806,681  67.7K/s   時間 3m 49s

2012-08-13 18:16:57 (63.0 KB/s) - `php-5.3.15.tar.gz' へ保存完了 [14806681/14806681]
$
$ tar zxf php-5.3.15.tar.gz

phpのビルドに必要なライブラリのインストール

$ sudo apt-get install libxml2-dev libevent-dev

PHP-FPMを有効化してconfigure

$ ./configure --enable-fpm

PHPインストール

$ sudo make all install

PHP-FPM用ユーザ作成

$ sudo useradd --shell /sbin/nologin php-fpm

PHP-FPM設定ファイル作成

$ sudo cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
$ sudo vi /usr/local/etc/php-fpm.conf
$ diff /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
140,141c140,141
< user = nobody
< group = nobody
---
> user = php-fpm
> group = php-fpm

PHP-FPM起動

$ sudo /usr/local/sbin/php-fpm
$ ps aux | grep php-fpm | grep -v grep
root     20053  0.0  0.1  15688  2032 ?        Ss   20:28   0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
php-fpm  20054  0.0  0.1  15688  1576 ?        S    20:28   0:00 php-fpm: pool www      
php-fpm  20055  0.0  0.1  15688  1576 ?        S    20:28   0:00 php-fpm: pool www      

nginxインストール(php-fpm有効化)

gccがインストールされていることを確認

$ which gcc
/usr/bin/gcc

PCRE(Perl Compatible Regular Expression: Perl互換正規表)ライブラリをインストール

$ sudo apt-get install libpcre3 libpcre3-dev

zlibライブラリインストール

$ sudo apt-get install zlib1g zlib1g-dev

OpenSSLインストール

$ sudo apt-get install openssl libssl-dev

Nginxのソースのダウンロードと展開

$ wget http://nginx.org/download/nginx-1.2.3.tar.gz
--2012-08-13 11:03:03--  http://nginx.org/download/nginx-1.2.3.tar.gz
nginx.org (nginx.org) をDNSに問いあわせています... 206.251.255.63
nginx.org (nginx.org)|206.251.255.63|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 723294 (706K) [application/octet-stream]
`nginx-1.2.3.tar.gz' に保存中

100%[======================================>] 723,294     68.0K/s   時間 11s   

2012-08-13 11:03:14 (65.1 KB/s) - `nginx-1.2.3.tar.gz' へ保存完了 [723294/723294]

$ 
$ tar zxf nginx-1.2.3.tar.gz 
$ 

Nginx用ユーザ作成

$ sudo useradd --shell /sbin/nologin nginx

Nginxビルド、インストール

$ cd nginx-1.2.3/
$ ./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module
$ make
$ sudo make install

Nginx起動確認

$ sudo /usr/local/nginx/sbin/nginx
$ ps aux | grep nginx | grep -v grep
root     14718  0.0  0.0   5792   684 ?        Ss   17:58   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx    14719  0.0  0.1   5948  1316 ?        S    17:58   0:00 nginx: worker process      

PHP-FPM,Nginx,FuelPHPの連携設定

$ sudo vi /usr/local/nginx/conf/nginx.conf
$ diff /usr/local/nginx/conf/nginx.conf.default /usr/local/nginx/conf/nginx.conf
37a38,39
>  root /var/www/fuelphp-sample/public;
>  index index.php;
39c41
<         #charset koi8-r;
---
>         charset utf-8;
41c43,44
<         #access_log  logs/host.access.log  main;
---
>         access_log  logs/fuelphp-sample.access.log;
>         error_log   logs/fuelphp-sample.error.log;
42a46
>         # すべてのアクセスをindex.phpに振る。
44,45c48
<             root   html;
<             index  index.html index.htm;
---
>             try_files $uri /index.php?$uri$args;
48,54c51,57
<         #error_page  404              /404.html;
< 
<         # redirect server error pages to the static page /50x.html
<         #
<         error_page   500 502 503 504  /50x.html;
<         location = /50x.html {
<             root   html;
---
>         # phpを実行できるようにする。
>         location ~ .*\.php$ {
>             fastcgi_pass   127.0.0.1:9000;
>             fastcgi_index  index.php;
>             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
>             #fastcgi_param  FUEL_ENV production; #リリース時にはコメントを外して有効にする。
>             include        fastcgi_params;
57,78c60,65
<         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
<         #
<         #location ~ \.php$ {
<         #    proxy_pass   http://127.0.0.1;
<         #}
< 
<         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
<         #
<         #location ~ \.php$ {
<         #    root           html;
<         #    fastcgi_pass   127.0.0.1:9000;
<         #    fastcgi_index  index.php;
<         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
<         #    include        fastcgi_params;
<         #}
< 
<         # deny access to .htaccess files, if Apache's document root
<         # concurs with nginx's one
<         #
<         #location ~ /\.ht {
<         #    deny  all;
<         #}
---
>         # すべての不可視ファイルをアクセス不可にする。
>         location ~ /\. {
>             access_log off;
>             log_not_found off;
>             deny all;
>         }
$ cat /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
    root /var/www/fuelphp-sample/public;
    index index.php;

        charset utf-8;

        access_log  logs/fuelphp-sample.access.log;
        error_log   logs/fuelphp-sample.error.log;

        # すべてのアクセスをindex.phpに振る。
        location / {
            try_files $uri /index.php?$uri$args;
        }

        # phpを実行できるようにする。
        location ~ .*\.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            #fastcgi_param  FUEL_ENV production; #リリース時にはコメントを外して有効にする。
            include        fastcgi_params;
        }

        # すべての不可視ファイルをアクセス不可にする。
        location ~ /\. {
            access_log off;
            log_not_found off;
            deny all;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
$ sudo /usr/local/nginx/sbin/nginx -s reload

Xdebugインストール

Xdebugコンパイルに必要なコマンドをインストール

$ sudo apt-get install autoconf gcc g++

Xdebugのソースをダウンロード、展開

$ wget http://xdebug.org/files/xdebug-2.2.1.tgz--2012-08-13 21:14:58--  http://xdebug.org/files/xdebug-2.2.1.tgz
xdebug.org (xdebug.org) をDNSに問いあわせています... 82.113.146.227
xdebug.org (xdebug.org)|82.113.146.227|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 248057 (242K) [application/x-gtar-compressed]
`xdebug-2.2.1.tgz' に保存中

100%[======================================>] 248,057     53.8K/s   時間 5.6s  

2012-08-13 21:15:04 (42.9 KB/s) - `xdebug-2.2.1.tgz' へ保存完了 [248057/248057]
$ tar zxf xdebug-2.2.1.tgz 
$ cd xdebug-2.2.1/

コンパイル用設定

$ phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
configure.in:3: warning: prefer named diversions
configure.in:3: warning: prefer named diversions
$ which php-config
/usr/local/bin/php-config
$ ./configure --enable-xdebug --with-php-config=/usr/local/bin/php-config

コンパイル、インストール

$ make
$ sudo make install
$ ls -l /usr/local/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so 
-rwxr-xr-x 1 root root 669138  813 21:53 /usr/local/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so

PHP設定(Phar設定、Xdebug設定)

php.iniを用意

$ sudo cp /home/akanuma/src/php-5.3.15/php.ini-development /usr/local/lib/php.ini

Pharを正しく扱うためのdetect_unicode設定と、xdebugの設定を追加

$ sudo vi /usr/local/lib/php.ini
$ diff /home/akanuma/src/php-5.3.15/php.ini-development /usr/local/lib/php.ini 
928a929,930
> detect_unicode = Off
> 
1914a1917,1929
> 
> [xdebug]
> zend_extension = "/usr/local/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
> xdebug.collect_params = 4
> xdebug.dump.GET = "*"
> xdebug.dump.POST = "*"
> ;xdebug.profiler_enable = 0
> xdebug.remote_enable = 1
> ;xdebug.remote_handler = "dbgp"
> ;xdebug.remote_host = "localhost"
> ;xdebug.remote_log = "/var/log/xdebug/xdebug.log"
> ;xdebug.remote_mode = "req"
> ;xdebug.remote_port = 9000

リモートデバッグ用のログを準備しておく

$ sudo touch /var/log/xdebug/xdebug.log
$ sudo chmod 666 /var/log/xdebug/xdebug.log 
$ ls -l /var/log/xdebug/xdebug.log 
-rw-rw-rw- 1 root root 0  813 23:11 /var/log/xdebug/xdebug.log

php-fpmを再起動し、http://localhost/ にアクセスして、 with Xdebug v2.2.1 の記述があることを確認する。

Eclipseインストール

Eclipseの公式サイトからダウンロードするか、パッケージマネージャを使ってEclipseをインストールします。詳細は割愛します。

PDTインストール

Eclipseの[Help] メニューの [Install New Software] からPDTをインストールします。詳細は割愛します。

MakeGoodインストール

Eclipseの[Help] メニューの [Install New Software] からMakeGoodをインストールします。詳細は割愛します。

PHPUnitインストール

PEARのチャネルを更新します。

$ sudo pear update-channels
[sudo] password for akanuma: 
Updating channel "doc.php.net"
Channel "doc.php.net" is up to date
Updating channel "pear.php.net"
Channel "pear.php.net" is up to date
Updating channel "pecl.php.net"
Channel "pecl.php.net" is up to date

PEARパッケージをアップグレードします。

$ sudo pear upgrade -a PEAR

インストール済みのすべてのパッケージをアップグレードします。

$ sudo pear upgrade-all
Will upgrade channel://pear.php.net/console_getopt
Will upgrade channel://pear.php.net/archive_tar
downloading Console_Getopt-1.3.1.tar ...
Starting to download Console_Getopt-1.3.1.tar (Unknown size)
........done: 21,504 bytes
downloading Archive_Tar-1.3.10.tar ...
Starting to download Archive_Tar-1.3.10.tar (Unknown size)
...done: 98,304 bytes
upgrade-all ok: channel://pear.php.net/Console_Getopt-1.3.1
upgrade-all ok: channel://pear.php.net/Archive_Tar-1.3.10

新規チャネルを自動的に探すようにPEARの設定を変更します。

$ sudo pear config-set auto_discover 1
config-set succeeded

キャッシュをクリアします。

$ sudo pear clear-cache
reading directory /tmp/pear/cache
28 cache entries cleared

PHPUnitをインストール

$ sudo pear install -a pear.phpunit.de/PHPUnit

インストールされたPHPUnitのバージョン確認

$ phpunit --version
PHPUnit 3.6.12 by Sebastian Bergmann.

Gitインストール

Gitをインストールします。

$ sudo apt-get install git

インストールされたGitのバージョンを確認します。

$ git --version
git version 1.7.9.5

FuelPHPインストール

curlをインストールします。

$ sudo apt-get install curl

FuelPHPのoilコマンドをインストールします。

$ curl get.fuelphp.com/oil | sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   244  100   244    0     0    204      0  0:00:01  0:00:01 --:--:--   628
$ which oil
/usr/bin/oil

HTTPサーバのWebディレクトリにFuelPHPをインストールします。

$ cd /var/www
$ sudo oil create fuelphp-sample

http://localhost/ にアクセスして、FuelPHPのページが表示されることを確認します。

FuelPHP設定

FuelPHPの言語設定、タイムゾーン設定、ロギング設定、default_charsetの設定を変更します。

$ cp fuel/app/config/config.php fuel/app/config/config.php.bak
$ vi fuel/app/config/config.php 
$ diff fuel/app/config/config.php.bak fuel/app/config/config.php
12a13,15
> // set default charset
> ini_set('default_charset', 'UTF-8');
> 
76c79
<  'language'           => 'en', // Default language
---
>  'language'           => 'ja', // Default language
78c81
<  'locale'             => 'en_US', // PHP set_locale() setting, null to not set
---
>  'locale'             => null, // PHP set_locale() setting, null to not set
89c92
<  'default_timezone'   => 'UTC',
---
>  'default_timezone'   => 'Asia/Tokyo',
101c104
<  'log_threshold'    => Fuel::L_WARNING,
---
>  'log_threshold'    => Fuel::L_ALL,

Eclipse/PDT設定

PHP実行可能ファイルの設定

[Window] メニューから [Preferences] を選択して、左カラムから、「PHP」の「PHP Executables」を選択し、[Add] ボタンを押します。

「Name」は「PHP」とし、「Executable path」に「/usr/local/bin/php」を設定します。 「PHP debugger」は「XDebug」を選択して[Finish]ボタンを押します。

次に左カラムから、 「PHP」の「Debug」を選択し、「PHP Debugger」に「XDebug」を、「PHP Executable」に「PHP」を選択し、[Apply] ボタンを押し、[OK]ボタンを押して終了します。