CentOS5.2にnginx + php-fpm(PHP FastCGI Process Manager) + XCacheの環境を構築する



nginxのことについて調べていると、たまたま
Nginx + PHP + PHP-FPM on Debian Etch 4.0
というエントリーを見つけました。


早速CentOSで試してみます。

MySQLは使いたいので、インストール


yum install mysql-server



php-fpmとは?
php-fpm(PHP FastCGI Process Manager)
php-fpm - is a patch for php4/5 to greatly improve FastCGI SAPI usage in production.

紹介文から推測すると、要するにphpのパッチ形式でモジュールは提供され、
これを適用すると、FastCGIのサーバAPIが改善されます。
(という風に捉らえました。。。)


まずは、PHPのソースコードと、php-fpmのソースをダウンロード


###PHPのソース
#wget http://jp.php.net/distributions/php-5.2.8.tar.gz

###php-fpmのソース
#wget http://php-fpm.anight.org/downloads/head/php-5.2.8-fpm-0.5.10.diff.gz

###PHPのソースを展開
#tar zxvf php-5.2.8.tar.gz

###PHPのソースにphp-fpmを適用
#gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.8 -p1

patching file configure
patching file configure.in
patching file libevent/aclocal.m4
patching file libevent/autogen.sh
patching file libevent/buffer.c
patching file libevent/ChangeLog
patching file libevent/compat/sys/queue.h
patching file libevent/compat/sys/_time.h
patching file libevent/config.h.in
patching file libevent/configure
patching file libevent/configure.in
patching file libevent/depcomp
patching file libevent/devpoll.c
patching file libevent/epoll.c
patching file libevent/epoll_sub.c
patching file libevent/evbuffer.c
patching file libevent/event.3
patching file libevent/event.c
patching file libevent/event-config.h
patching file libevent/event-fpm.h
patching file libevent/event.h
patching file libevent/event-internal.h
patching file libevent/evhttp.h
patching file libevent/evport.c
patching file libevent/evsignal.h
patching file libevent/evutil.c
patching file libevent/evutil.h
patching file libevent/http.c
patching file libevent/http-internal.h
patching file libevent/install-sh
patching file libevent/kqueue.c
patching file libevent/log.c
patching file libevent/log.h
patching file libevent/Makefile.am
patching file libevent/Makefile.in
patching file libevent/min_heap.h
patching file libevent/missing
patching file libevent/poll.c
patching file libevent/README
patching file libevent/select.c
patching file libevent/signal.c
patching file libevent/strlcpy.c
patching file libevent/strlcpy-internal.h
patching file main/php_config.h.in
patching file sapi/cgi/cgi_main.c
patching file sapi/cgi/config9.m4
patching file sapi/cgi/fastcgi.c
patching file sapi/cgi/fastcgi.h
patching file sapi/cgi/fpm/acinclude.m4
patching file sapi/cgi/fpm/conf/php-fpm.conf.in
patching file sapi/cgi/fpm/config.m4
patching file sapi/cgi/fpm/fpm_arrays.h
patching file sapi/cgi/fpm/fpm_atomic.h
patching file sapi/cgi/fpm/fpm_autoconf.h.in
patching file sapi/cgi/fpm/fpm.c
patching file sapi/cgi/fpm/fpm_children.c
patching file sapi/cgi/fpm/fpm_children.h
patching file sapi/cgi/fpm/fpm_cleanup.c
patching file sapi/cgi/fpm/fpm_cleanup.h
patching file sapi/cgi/fpm/fpm_clock.c
patching file sapi/cgi/fpm/fpm_clock.h
patching file sapi/cgi/fpm/fpm_conf.c
patching file sapi/cgi/fpm/fpm_conf.h
patching file sapi/cgi/fpm/fpm_config.h
patching file sapi/cgi/fpm/fpm_env.c
patching file sapi/cgi/fpm/fpm_env.h
patching file sapi/cgi/fpm/fpm_events.c
patching file sapi/cgi/fpm/fpm_events.h
patching file sapi/cgi/fpm/fpm.h
patching file sapi/cgi/fpm/fpm_php.c
patching file sapi/cgi/fpm/fpm_php.h
patching file sapi/cgi/fpm/fpm_php_trace.c
patching file sapi/cgi/fpm/fpm_php_trace.h
patching file sapi/cgi/fpm/fpm_process_ctl.c
patching file sapi/cgi/fpm/fpm_process_ctl.h
patching file sapi/cgi/fpm/fpm_request.c
patching file sapi/cgi/fpm/fpm_request.h
patching file sapi/cgi/fpm/fpm_shm.c
patching file sapi/cgi/fpm/fpm_shm.h
patching file sapi/cgi/fpm/fpm_shm_slots.c
patching file sapi/cgi/fpm/fpm_shm_slots.h
patching file sapi/cgi/fpm/fpm_signals.c
patching file sapi/cgi/fpm/fpm_signals.h
patching file sapi/cgi/fpm/fpm_sockets.c
patching file sapi/cgi/fpm/fpm_sockets.h
patching file sapi/cgi/fpm/fpm_stdio.c
patching file sapi/cgi/fpm/fpm_stdio.h
patching file sapi/cgi/fpm/fpm_str.h
patching file sapi/cgi/fpm/fpm_trace.c
patching file sapi/cgi/fpm/fpm_trace.h
patching file sapi/cgi/fpm/fpm_trace_mach.c
patching file sapi/cgi/fpm/fpm_trace_pread.c
patching file sapi/cgi/fpm/fpm_trace_ptrace.c
patching file sapi/cgi/fpm/fpm_unix.c
patching file sapi/cgi/fpm/fpm_unix.h
patching file sapi/cgi/fpm/fpm_worker_pool.c
patching file sapi/cgi/fpm/fpm_worker_pool.h
patching file sapi/cgi/fpm/init.d/php-fpm.in
patching file sapi/cgi/fpm/Makefile.frag
patching file sapi/cgi/fpm/xml_config.c
patching file sapi/cgi/fpm/xml_config.h
patching file sapi/cgi/fpm/zlog.c
patching file sapi/cgi/fpm/zlog.h
patching file sapi/cgi/Makefile.frag



パッチを当てた後、ビルドとインストールを行ないます。


###PHPのビルドに必要なモジュールをインストール
#yum install libxml2-devel
#yum install openssl-devel
#yum install libjpeg-devel
#yum install libpng-devel
#yum install libmcrypt-devel
#yum install mysql-devel



###パッチ適用後のPHPのソースフォルダに移動
#cd php-5.2.6

###configure、make実行
#./configure --enable-fastcgi --enable-fpm --with-mcrypt --with-zlib --enable-mbstring --with-openssl --with-mysql --with-mysql-sock --with-gd --with-jpeg-dir=/usr/lib --enable-gd-native-ttf --without-sqlite --disable-pdo --disable-reflection
#make

+--------------------------------------------------------------------+
| *** WARNING *** |
| |
| You will be compiling the CGI version of PHP without any |
| redirection checking. By putting this cgi binary somewhere in |
| your web space, users may be able to circumvent existing .htaccess |
| security by loading files directly through the parser. See |
| http://www.php.net/manual/security.php for more details. |
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.


###インストール
#make install


Installing PHP SAPI module: cgi
Installing PHP CGI binary: /usr/local/bin/
Installing FPM config: /usr/local/etc/php-fpm.conf
Installing init.d script: /usr/local/sbin/php-fpm
Installing PHP CLI binary: /usr/local/bin/
Installing PHP CLI man page: /usr/local/man/man1/
Installing build environment: /usr/local/lib/php/build/
Installing header files: /usr/local/include/php/
Installing helper programs: /usr/local/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/lib/php/
[PEAR] Console_Getopt - installed: 1.2.3
[PEAR] Archive_Tar - installed: 1.3.2
[PEAR] Structures_Graph- installed: 1.0.2
pear/PEAR can optionally use package "pear/XML_RPC" (version >= 1.4.0)
[PEAR] PEAR - installed: 1.7.2
Wrote PEAR system config file at: /usr/local/etc/pear.conf
You may want to add: /usr/local/lib/php to your php.ini include_path


###実行ファイルからリロケート情報とシンボルテーブルを削除
###strip
strip /usr/local/bin/php-cgi





これでPHP、php-fpmのインストールは完了です。

次に、xcacheのインストールを行ないます。



###xcacheのインストールに必要なモジュールをインストール
#yum install autoconf

#cd /usr/local/src
#wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
#tar xzvf xcache-1.2.2.tar.gz
#cd xcache-1.2.2
#phpize
#./configure --with-php-config=/usr/local/bin/php-config --enable-xcache
#make
#make install

Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20060613/




これで必要なモジュールはそろいました。

php-fpmの設定ファイルを編集します。


#vi /usr/local/etc/php-fpm.conf

###設定ファイルはXML形式で記載されています。
###62行目付近にcgiの実行ユーザ、グループの記載があります。
###コメントアウトされているので、コメントアウトを削除し、
###有効にします。

Unix user of processes
<value name="user">nobody</value>

Unix group of processes
<value name="group">nobody</value>



nginxの設定ファイルを編集します。


#cd /usr/local/nginx
#vi nginx.conf

worker_processes 2;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    tcp_nodelay on;
    keepalive_timeout 10 10;
    gzip on;
    gzip_comp_level 1; gzip_proxied any;
    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    include /usr/local/nginx/sites-enabled/*;
}


include /usr/local/nginx/sites-enabled/*;
として、設定ファイルを別に記載できるんですね。
知りませんでした。

/usr/local/nginx/sites-enabled/ディレクトリを作成し、
設定ファイルを作成します。



###confフォルダを作成し、fastcgi_paramsをコピー
#cd /usr/local/nginx
#mkdir conf
#cp fastcgi_params conf/

###設定ファイルを作成
#mkdir sites-enabled
#vi sites-enabled/localhost

server {
    listen 80;
    server_name localhost;
    index index.php;
    root /var/www/local;
    location / {
        error_page 404 = //index.php?q=$uri;
    }
    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /usr/local/nginx/conf/fastcgi_params;
    }
}



これで、設定完了です。
phpが動くか確認します。


###document rootを作成
#mkdir /var/www/local
#cd /var/www/local

###動作確認用のphpプログラムを作成
#vi index.php

<?php> phpinfo(); <?>




php-fpmとnginxを起動して・・・


#php-fpm start
Starting php_fpm . done

#/sbin/nginx



http://www.example.com/index.phpにアクセスすると
こんな画面が表示されます。





引き続き、xcacheの設定を行ないます。
php.iniを作成し、xcacheのパラメータを設定します。


#vi /usr/local/lib/php.ini

magic_quotes_gpc=0
[xcache-common]
zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20060613/xcache.so
[xcache]
xcache.shm_scheme = "mmap"
xcache.size = 64M
default_charset = "utf-8"

[xcache.admin]
xcache.admin.auth = On
xcache.admin.user = "admin"
#md5でパスワードを記載
xcache.admin.pass = "5f4dcc3b5aa765d61d8327deb882cf99"



xcacheの管理画面を公開します。


#cd /usr/local/src/xcache-1.2.2
#cp -r admin/ /var/www/local/

#cd /var/www/local/
#mv admin/ xchace/



php.iniの設定を反映させるため、php-fpmを再起動。


#php-fpm restart
Shutting down php_fpm . done
Starting php_fpm done



http://www.example.com/xcache/
にアクセスすると、IDとパスワードの入力を求められるので、
php.iniで設定したIDとパスワードを入力します。

すると、こんな画面が表示されます。






php-fpmを自動起動させる場合は、


#cd /etc/init.d/
#ln -s /usr/local/sbin/php-fpm php-fpm

###php-fpmは、chkconfigの書式にしたがっていないので修正
#vi php-fpm

※ファイルの行頭に追加
#! /bin/sh
# chkconfig: 2345 98 20
# description: php-fpm
# processname: php-fpm



これでchkconfigで管理できるようになったので、追加します。


#chkconfig --add php-fpm
#chkconfig php-fpm on
#chkconfig --list php-fpm
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off



nginxの起動スクリプトは、コチラ。

CentOS nginx の起動スクリプト





もどる