Posted by rp8 on Sunday, October 26, 2008

Running PHP scripts in Nginx is easy.

You need to install the php5 with fastcgi support. If you like to compile from source, you need to include the configure options

—enable-fastcgi
and
—enable-force-cgi-redirect
.

Here’re the steps:

1. Run “/usr/local/bin/php-cgi -b 127.0.0.1:9000”.

2. Configure Nginx to pass any calls to the php scripts to this port 9000.

A common error in configuration is mistakenly setting “fastcgi_param SCRIPT_FILENAME /vaw/www/phpsite$fastcgi_script_name;”, which results the incorrect file path “/var/www/phpsite/phpsite/xyz.php” and the dreadful “404 File Not Found” error for a request like “http://www.example.com/phpsite/xyz.php”.

Here's a server section that works for URLs like http://www.example.com/phpsite/index.php and the site is located in /var/www/phpsite:

location /phpsite {
  root /var/www;
  index index.php;
}

location ~ \.php$ {
  fastcgi_pass  127.0.0.1:9000;
  fastcgi_index index.php;
  include /usr/local/nginx/conf/fastcgi_params;
  fastcgi_param SCRIPT_FILENAME /vaw/www$fastcgi_script_name;
}