nginx で php-fpm を
    動かしてみた
      Webサーバ勉強会 #4
            2011/11/11
               do_aki
do_aki (どぅーあき)
• |所属| > 株式会社もしも
        (ドロップシッピング・アフィリエイトASP)
• |仕事| > インフラ構築・運用 兼
         Webアプリケーション開発・運用
• |出現| > 渋谷・山手線沿線
• |特性| > PHPer



                         http://do-aki.net/
経緯とか
• Web サーバ勉強会のお題が埋まってた
• せっかくだし nginx 使って何かしたい

• チューニンガソン2(10/1)で、 Mediawiki が
  php-fpm (5.4) でうまく動かないとか言って
  た
自分の nginx の認識
• 今まで動かしたこと無かった

• 軽快・設定がシンプル
• シングルスレッド
• イベント駆動的な?
   程度の知識しか持ってない
結果




問題ない     (たぶん)




       ※mediawiki を動かすのは
環境
• Scientific Linux 6.0

• nginx version 1.0.9 (latest stable)
• PHP version 5.4.0 beta2
         (昨日 RC1でちゃったケド)
最初に configure
./configure 
 --prefix=/usr 
 --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf 
 --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid 
 --lock-path=/var/lock/nginx.lock
 --user=nginx --group=nginx 
 --with-http_ssl_module --with-http_flv_module 
 --with-http_gzip_static_module 
 --http-log-path=/var/log/nginx/access.log 
 --http-client-body-temp-path=/var/run/nginx/client/ 
 --http-proxy-temp-path=/var/run/nginx/proxy/ 
 --http-fastcgi-temp-path=/var/run/nginx/fcgi/ 
 --http-uwsgi-temp-path=/var/run/nginx/uwsgi/ 
 --http-scgi-temp-path=/var/run/nginx/scgi/


あらかじめ pcre-devel パッケージをインストール (たぶん openssl-devel zlib-devel)
とりあえず試す
• make & make install
• $ sudo /usr/sbin/nginx

• t オプションで設定ファイルチェック
• SIGHUP 送ると設定再読み込み
     ドキュメント結構しっかりしてるね!
ドキュメントルートが分からず

                       基準パスはどこ?
location / {
      root html;
      index index.html index.htm;
}
ドキュメントルートが分からず

                       絶対パスなら確実
location / {
      root /var/www/html;
      index index.html index.htm;
}
server {
  listen 8888;         nginx.conf 80から変更
  server_name localhost;

    location / {
      root /var/www/html;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;              fastcgi 用設定
       include /etc/nginx/fastcgi.conf;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root html;
    }
}
一方 php-fpm
./configure 
 --cache-file=./config.cache 
 --with-layout=GNU 
 --disable-debug 
 --disable-rpath 
 --enable-zip 
 --enable-mbstring=ja 
 --enable-mbregex 
 --with-openssl 
 --with-zlib 
 --enable-ftp 
 --enable-sockets 
 --enable-sysvsem --enable-sysvshm --enable-sysvmsg 
 --with-apxs2=/usr/sbin/apxs 

--enable-fpm
php-fpm を実行
• make & make install
• sudo /usr/local/sbin/php-fpm
• default port は 9000

• make install しなくても、設定ファイルさえ正
  しい場所に置いておけば動く
• php-5.4.0beta2/sapi/fpm/php-fpm を実行でも
  可能 (設定ファイルの場所はエラーで表示)
mediawiki 表示
• 何故か表示されない
• phpinfo() を実行するだけのスクリプトは
  動く

• Response Header みると
  – Location: http://localhost:8888/...
phpinfo を比較
$_SERVER["SERVER_NAME"]

apache => 192.168.29.128

nginx   =>   localhost
Fastcgi.conf
    (あるいはfastcgi_params)


fastcgi_param SERVER_NAME $server_name;




      server_name localhost;
server {
  listen    8888;      nginx.conf
    server_name 192.168.29.128;
    location / {
      root /var/www/html;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
       include /etc/nginx/fastcgi.conf;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
      root html;
    }
}
まとめ
• nginx × php-fpm あっさり動いちゃった

• むしろ、チューニンガソン2で動かなかった
  理由は何だったんだ……?
• 当時は aplha3 だったから?
• モジュールによって?

• php-fpm は EXPERIMENTAL なので、プロダク
  ションでは使わないようにね!
• ありがとうございました

Webサーバ勉強会4 nginx で php-fpm を動かしてみた

  • 1.
    nginx で php-fpmを 動かしてみた Webサーバ勉強会 #4 2011/11/11 do_aki
  • 2.
    do_aki (どぅーあき) • |所属|> 株式会社もしも (ドロップシッピング・アフィリエイトASP) • |仕事| > インフラ構築・運用 兼 Webアプリケーション開発・運用 • |出現| > 渋谷・山手線沿線 • |特性| > PHPer http://do-aki.net/
  • 3.
    経緯とか • Web サーバ勉強会のお題が埋まってた •せっかくだし nginx 使って何かしたい • チューニンガソン2(10/1)で、 Mediawiki が php-fpm (5.4) でうまく動かないとか言って た
  • 4.
    自分の nginx の認識 •今まで動かしたこと無かった • 軽快・設定がシンプル • シングルスレッド • イベント駆動的な? 程度の知識しか持ってない
  • 6.
    結果 問題ない (たぶん) ※mediawiki を動かすのは
  • 7.
    環境 • Scientific Linux6.0 • nginx version 1.0.9 (latest stable) • PHP version 5.4.0 beta2 (昨日 RC1でちゃったケド)
  • 8.
    最初に configure ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/run/nginx/client/ --http-proxy-temp-path=/var/run/nginx/proxy/ --http-fastcgi-temp-path=/var/run/nginx/fcgi/ --http-uwsgi-temp-path=/var/run/nginx/uwsgi/ --http-scgi-temp-path=/var/run/nginx/scgi/ あらかじめ pcre-devel パッケージをインストール (たぶん openssl-devel zlib-devel)
  • 9.
    とりあえず試す • make &make install • $ sudo /usr/sbin/nginx • t オプションで設定ファイルチェック • SIGHUP 送ると設定再読み込み ドキュメント結構しっかりしてるね!
  • 10.
    ドキュメントルートが分からず 基準パスはどこ? location / { root html; index index.html index.htm; }
  • 11.
    ドキュメントルートが分からず 絶対パスなら確実 location / { root /var/www/html; index index.html index.htm; }
  • 12.
    server { listen 8888; nginx.conf 80から変更 server_name localhost; location / { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi 用設定 include /etc/nginx/fastcgi.conf; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
  • 13.
    一方 php-fpm ./configure --cache-file=./config.cache --with-layout=GNU --disable-debug --disable-rpath --enable-zip --enable-mbstring=ja --enable-mbregex --with-openssl --with-zlib --enable-ftp --enable-sockets --enable-sysvsem --enable-sysvshm --enable-sysvmsg --with-apxs2=/usr/sbin/apxs --enable-fpm
  • 14.
    php-fpm を実行 • make& make install • sudo /usr/local/sbin/php-fpm • default port は 9000 • make install しなくても、設定ファイルさえ正 しい場所に置いておけば動く • php-5.4.0beta2/sapi/fpm/php-fpm を実行でも 可能 (設定ファイルの場所はエラーで表示)
  • 15.
    mediawiki 表示 • 何故か表示されない •phpinfo() を実行するだけのスクリプトは 動く • Response Header みると – Location: http://localhost:8888/...
  • 16.
    phpinfo を比較 $_SERVER["SERVER_NAME"] apache =>192.168.29.128 nginx => localhost
  • 17.
    Fastcgi.conf (あるいはfastcgi_params) fastcgi_param SERVER_NAME $server_name; server_name localhost;
  • 18.
    server { listen 8888; nginx.conf server_name 192.168.29.128; location / { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include /etc/nginx/fastcgi.conf; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
  • 19.
    まとめ • nginx ×php-fpm あっさり動いちゃった • むしろ、チューニンガソン2で動かなかった 理由は何だったんだ……? • 当時は aplha3 だったから? • モジュールによって? • php-fpm は EXPERIMENTAL なので、プロダク ションでは使わないようにね!
  • 20.