DebianのPHP+FastCGI+nginx

DebianのPHP+FastCGI+nginx

私はDebianを初めて使用します。 DebianでPHPを使用したいです。私は以下が欲しい:

apt-get install php5-cli php5-cgi spawn-fcgi

ファイルの作成/usr/bin/php-fastcgi:

#! /bin/sh
PHP_FCGI_CHILDREN=3
PHP_FCGI_MAX_REQUESTS=1000
exec /usr/bin/php5-cgi

ファイルの作成/etc/init.d/init-fastcgi:

#!/bin/bash
PHP_SCRIPT="/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f     /usr/bin/php-fastcgi"
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php5-cgi
RETVAL=$?
;;
restart)
killall -9 php5-cgi
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: sudo /etc/init.d/init-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL

Atelは次のことを行います。

chmod 755 /usr/bin/php-fastcgi
chmod 755 /etc/init.d/init-fastcgi 

入り、/etc/nginx/sites-enabled/default次を追加します。

location ~\.php$ {
 root /srv/www/ekb.mydomain.com/public_html;
 include /etc/nginx/fastcgi_params;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param QUERY_STRING $query_string;
 fastcgi_param SCRIPT_FILENAME /srv/www/ekb.mydomain.com/public_html$fastcgi_script_name;
}

ディレクトリを作成します。

/srv/www/ekb.mydomain.com/public_html
/srv/www/ekb.mydomain.com/logs

ファイルの作成/srv/www/ekb.mydomain.com/public_html/test.php

<?phpinfo();?>

サービスを開始します。

/etc/init.d/init-fastcgi start
/etc/init.d/nginx start

ブラウザで:

 www.ekb.maydomain.com/test.php 

ところで、404エラーが発生します。

私は何が間違っていましたか?

ベストアンサー1

nginxを設定するにはphp-fpmを使用する必要があります。インストール、構成、管理がより簡単で迅速です。

$ apt-get install nginx php5-fpm
$ nano /etc/nginx/nginx.conf

[...]
worker_processes  4;
[...]
keepalive_timeout   2;
[...]


$ nano /etc/nginx/sites-available/default

[...]
server {
    listen   80; ## listen for ipv4; this line is default and implied
    listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            allow ::1;
            deny all;
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #       proxy_pass http://127.0.0.1:8080;
    #}

    #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 /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            # With php5-cgi alone:
            #fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }
}

nginxとphp-fpmを起動します。

$ service nginx start
$ service php5-fpm start

おすすめ記事