info home general windows unix linux debian mac os apache nginx iphone

Recent Articles

How to rename multiple files in Unix or Linux

Convert docx to doc in Mac OS X Snow Leopard or Leopard

HISTORY shell variable management in Unix/Linux

How to eject CD/DVD from your MacBook or iMac

Play Prince of Persia on Ubuntu Linux using DOSBox

Tags

Configuring Nginx to include PHP FastCGI support

« Configuring Nginx | Nginx with mod_php »

Install these on the command line:

apt-get install php5-cli php5-cgi php5-xcache build-essential

Download and install spawn-fcgi 1.6.2:

wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.bz2
tar -xjvf spawn-fcgi-1.6.2.tar.bz2
cd spawn-fcgi-1.6.2
./configure
make
make install

Create a script to start PHP processes as FastCGI:

touch /usr/bin/php-fastcgi

FastCGI PHP will run on port 10005.

Edit it and add this:

#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 10005 -u www-data -f /usr/bin/php5-cgi

Edit /etc/nginx/sites-enabled/default and add this in the server block

# php requests
location ~ .*\.php?$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:10005;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /home/username/public_html/imacstercom$fastcgi_script_name;
}

Contents of /etc/nginx/fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

Run /usr/bin/php-fastcgi and restart nginx

sudo /usr/bin/php-fastcgi
sudo /etc/init.d/nginx restart

See if Nginx serves PHP pages. Create a simple PHP script called info.php with these contents:

<?php
phpinfo();
?>

And navigate to http://imacster.com/info.php to see if it works.

Alternative to PHP FastCGI

  1. You can also make Nginx forward PHP requests to Apache instead of running PHP through FastCGI
  2. Another alternative is making Nginx serve PHP pages by running PHP-FPM instead of spawn-fcgi. I have no tried it, but apparently PHP-FPM is more stable than spawn-fcgi.

« Configuring Nginx | Nginx with mod_php »

Last Update: July 2009