Query regarding Nginx working with FastCGI

Jim Ohlstein jim at ohlste.in
Mon Jul 11 14:13:32 MSD 2011

On 7/11/11 5:21 AM, Rishab Jain wrote:
> Hi guys,
> I'm facing an issue and was wondering if anybody could help me with
> this.
> I have an Ubuntu server, and I have installed all the softwares
> required using apt-get. Now, I have installed Nginx using the same
> mechanism as well. I want to use the Nginx for serving php scripts
> from the browser too. So therefore, after going through the web, I
> learn't that we need FastCGI to be installed.
> Now, as soon as the installation was complete, I restarted Nginx
> server and tried accessing the php file. Instead of executing the
> script, it was actually showing me  the php code on the browser. It
> wasn't untill I executed the following command: /usr/bin/spawn-fcgi
> -a -p 9090 -u www-data -g www-data -f /usr/bin/php5-cgi -P
> /var/run/fastcgi-php.pid
> As soon as I executed this command, and, restarted Nginx again, I
> was able to execute the PHP script.

You should be able to reload nginx configuration changes without restarting:

# sudo nginx -s reload

> Now, my issue is since fastcgi is being binded by a port, it means
> that there's a kind of a daemon running. So, going forward, if I make
> this server live, and, by any chance the fastcgi daemon crashes, the
> ubuntu will start throwing up my php code to the users (which is
> obviously never desired).
> I actually want to know a way we can prevent the php code from
> getting displayed to the user, irrespective of whether the fastcgi is
> running or not.

If you're PHP scripts are being passed to a fastcgi daemon using a
"fastcgi_pass" statement in your nginx.conf, then if the PHP daemon does
go down you should see a "502 Bad Gateway" page and not the source code.

On another note, you should use a process manager like Supervisor(d) if
you want to use the standard php-cgi daemon. This will restart the
processes should they die. You might also consider using the php-fpm
sapi and daemon. It has a built in process manager and is much more
stable, as it will spawn children to replace ones that die. Behind that
you can consider using Monit to monitor and automatically restart
daemons. We use it for nginx, php-fpm, memcached, sshd, MySQL, etc.

Jim Ohlstein

More information about the nginx mailing list