CentOS5.2+nginxでphpを動かす



いろいろ調べている、nginxでphpを動かすにはspawn-fcgiを
使うのがよさそうです。

しかし
「spawn-fcgiはlighttpdのオマケ」
とのことで、せっかくnginx入れたのに使わないとはいえ、
lighttpdも入れるのはちょっと・・・と思ってました。


さらに調べてみると、lighttpdのソースをダウンロードし
spawn-fcgiだけ入れる方法がある模様。
slicehostでnginxとphpを動かす


早速試してみます。
※nginxはインストール済みの状態からスタートしてます。

最終目標はサンプルアプリケーションとしてmantisを
動かすことなので、mysqlをインストールします。


yum install mysql-server-5*



その後、phpをインストール
※php-cgiを忘れずに。


yum install libmcrypt
yum install php-common php-cgi php-cli php-gd php-mysql



※lighttpdのビルドに必用なのでインストール

yum install bzip2-devel



lighttpdのソースを取得


wget http://www.lighttpd.net/download/lighttpd-1.4.20.tar.gz
tar xvzf lighttpd-1.4.20.tar.gz
cd lighttpd-1.4.20
./configure
make
cp src/spawn-fcgi /usr/bin/spawn-fcgi


make installを実行しないのがミソですね。



vi /usr/bin/php5-fastcgi
#!/bin/sh
#debian
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -C 2 -f /usr/bin/php5-cgi
#centos
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nobody -C 2 -f /usr/bin/php-cgi
chmod +x /usr/bin/php5-fastcgi
ln -s /usr/bin/php5-fastcgi /usr/bin/php-fastcgi



spawn-fcgiの起動ファイルです。


vi /etc/init.d/fastcgi

#!/bin/bash
#
# Startup script for the spawn-fcgi
#
# chkconfig: 345 80 15
# description: spawn-fcgi server.
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
    start)
        echo "Starting fastcgi"
        $PHP_SCRIPT
        RETVAL=$?
  ;;
    stop)
        echo "Stopping fastcgi"
        killall -9 php-cgi
        RETVAL=$?
  ;;
    restart)
        echo "Restarting fastcgi"
        killall -9 php-cgi
        $PHP_SCRIPT
        RETVAL=$?
  ;;
    *)
        echo "Usage: php-fastcgi {start|stop|restart}"
        exit 1
  ;;
esac
exit $RETVAL



chmod +x /etc/init.d/fastcgi
chkconfig --add fastcgi
chkconfig fastcgi on
chkconfig --list fastcgi

/etc/init.d/fastcgi start




拡張子phpファイルの要求がきたらspawn-fcgiに処理を
渡すようnginx.confを修正


vi /usr/local/nginx/nginx.conf


# 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 /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}



php-mysql接続テストスクリプト


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MySQL 接続テスト</title>
</head>
<body>
<p align="center">MySQL 接続テストL</p>
<hr>
<div>
<?php
  $con = mysql_connect("localhost", "root", "password");
  mysql_select_db("test", $con);
  mysql_close($con);
  print "<p>正常終了</p>";
?>
</div>
<hr>
</body>
</html>



mantisのインストール


#データベース作成
#CREATE DATABASE bugtracker CHARACTER SET utf8 COLLATE utf8_general_ci;

#ソース取得
#wget http://downloads.sourceforge.net/mantisbt/mantisbt-1.1.6.tar.gz

#/usr/local/nginx/htmlへ展開
#tar zxvf mantisbt-1.1.6.tar.gz

#phpセッション保存ディレクトリを作成
#mkdir /var/lib/php/session
#chmod -R 777 /var/lib/php/session/



結構、あっさり動いてくれました。



もどる