タイトル通り、FreeBSDでnginx、FastCGIでPHPを動かす場合。
lighttpd + phpの場合はこちら。
必要なもの。
- nginx
- spawn-fcgi
- php(fastCGI)
概要
nginxは静的コンテンツ向けのwebサーバ。
そのままでは動的コンテンツは扱えない。
どうしても必要なら、よそで動的コンテンツを生成してもらわなければならない。
その仕組みの一つにFastCGIがある。
FastCGIは動的コンテンツの受け渡しのためのインタフェースである。
spawn-fcgiはFastCGIプロセスの管理を行う。
ここで、PHPにはFastCGIサーバモードというのがある。
指定されたポートで待ち受けてリクエストに応えるというもの。
spawn-fcgiとPHPのFastCGIサーバモードを組み合わせれば、PHPのFastCGIインタフェースができあがる。
nginxはPHPコンテンツのリクエストを受けたら、FastCGIを通してphp-cgiにコンテンツを生成してもらう、というわけ。
参考。まあwikipediaなんですけどね。
nginx
http://ja.wikipedia.org/wiki/Nginx
CGI
http://ja.wikipedia.org/wiki/Common_Gateway_Interface
FastCGI
http://ja.wikipedia.org/wiki/FastCGI
インストール
FreeBSDなら、pkgで前章の三つをインストールする。
特に難しいこともないので割愛。
phpはpkgそのままでfastCGI対応になっているので意識する必要なし。
PHPの確認
FastCGI対応かどうかを念のため確認。
/usr/local/bin/php-cgiというのがインストールされていればよい。
念のため-vで確認。
1 2 3 4 |
$ php-cgi -v PHP 5.4.23 (cgi-fcgi) (built: Dec 19 2013 21:06:30) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies |
spawn-fcgiの設定
spawn_fcgiには特定の設定ファイルがないのでrc.confに直接書き込む。
起動スクリプト/usr/local/rc.d/spawn-fcgiの中身を見るとどういったオプションがあるかが分かる。
nginx、spawn-fcgiの起動設定と一緒にやってしまおう。
下記を/etc/rc.confにひとまず貼りつける。
コメントアウトしてあるのはデフォルト設定。
変えたい箇所があれば#を外して設定する。
注意!
spawn-fcgiのspawnとfcgiの間はハイフン(ダッシュ)”-“であるが、/etc/rc.confでの記載にはアンダースコア”_”にすること。
言い方を変えると、spawn-fcgi_enableなどと書かないこと。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# # nginx, with PHP FastCGI # nginx_enable="YES" spawn_fcgi_enable="YES" ##spawn_fcgi_app="/usr/local/bin/php-cgi" ##spawn_fcgi_pidfile="/var/run/spawn-fcgi.pid" ##spawn_fcgi_username="www" ##spawn_fcgi_groupname="www" ##spawn_fcgi_bindaddr="127.0.0.1" ##spawn_fcgi_bindport="9000" ##spawn_fcgi_bindsocket_mode="0777" ##spawn_fcgi_children="5" ##spawn_fcgi_max_requests="1000" ##spawn_fcgi_path_env="/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin" |
最終的に以下のようにした。
個人用のサーバなのでささやかに。
(冗長なので一部のコメント部分は除く)
1 2 3 4 5 6 7 8 |
# # nginx, with PHP FastCGI # nginx_enable="YES" spawn_fcgi_enable="YES" spawn_fcgi_bindport="8000" spawn_fcgi_children="3" spawn_fcgi_max_requests="100" |
spawn-fcgiの起動確認
serviceコマンドで起動。
1 2 3 |
$ sudo service spawn-fcgi start Starting spawn_fcgi. spawn-fcgi: child spawned successfully: PID: 7486 |
sockstatで待ち受けポートを確認
1 2 3 4 5 6 7 |
$ sockstat -l4 USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS www php-cgi 7489 0 tcp4 127.0.0.1:8000 *:* www php-cgi 7488 0 tcp4 127.0.0.1:8000 *:* www php-cgi 7487 0 tcp4 127.0.0.1:8000 *:* www php-cgi 7486 0 tcp4 127.0.0.1:8000 *:* (略) |
phpの動作確認
nginxにはサンプルの設定ファイルがついてくる。
それをベースに、たとえば以下のような設定ファイルを作る。
location ~ \.php$のブロックが重要。
fastcgi_pass 127.0.0.1:8000;のポートを先のswawn-fcgiと合わせること。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; root /usr/local/www/php-test; index index.html; fastcgi_index index.php; location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:8000; } } } |
/usr/local/www/php-testにindex.htmlをつくる。また、test.phpを作る。
index.html
1 |
It works! |
test.php
1 2 3 |
<?php phpinfo(); ?> |
nginxをスタートしておき;
1 2 3 4 5 6 7 8 9 |
$ sudo service nginx restart Performing sanity check on nginx configuration: nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful Stopping nginx. Performing sanity check on nginx configuration: nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful Starting nginx. |
ブラウザで繋ぐ。
スクリーンショット撮るのが面倒なのでw3mで。
下記の通り/usr/local/www/nginx/index.htmlの内容が表示される。
1 2 |
$ w3m 127.0.0.1 It works! |
index.htmlの内容が表示された。
これで通常のwebサーバとしては問題なく動作していることが確認できた。
ではphpの動作確認
PHP infoの結果が表示される。
内部的には、nginxが127.0.0.1:8000で待っているphp-cgiにtest.phpを実行させて、その結果を返している。
1 2 3 4 5 6 7 8 9 10 11 12 |
$ w3m 127.0.0.1/test.php PHP Logo PHP Version 5.4.23 FreeBSD copper 10.0-RC2 FreeBSD 10.0-RC2 #0 r259404: Sun Dec System 15 08:18:20 UTC 2013 root@snap.freebsd.org:/usr/obj/usr/src/ sys/GENERIC amd64 Build Date Dec 19 2013 21:05:25 (略) |
以上