Configuring Nginx to include PHP FastCGI support

Published July 01, 2009
Online Library Management System in...
Online Library Management System in PHP Source Code 2021 | PHP Projects Free Download

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:


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

Alternative to PHP FastCGI

  • You can also make Nginx forward PHP requests to Apache instead of running PHP through FastCGI
  • 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

Table of Contents

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: July 01, 2009. Β Β Β  This post was originally written on July 02, 2009.