Debian

Debian 上的 PHP+FastCGI+nginx

  • March 24, 2014

我是 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

做:

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 錯誤。

我能做錯什麼?

你真的應該用 php-fpm 配置 nginx。它更易於安裝、配置和管理,而且速度也一樣快。

$ 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

引用自:https://unix.stackexchange.com/questions/121136