# Query regarding Nginx working with FastCGI

**URL:** <https://www.ruby-forum.com/t/query-regarding-nginx-working-with-fastcgi/209306>\
**Category:** NGINX\
**Created:** [July 11, 2011, 11:22am UTC](https://www.ruby-forum.com/t/query-regarding-nginx-working-with-fastcgi/209306 "2011-07-11T11:22:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 11, 2011, 11:22am UTC](https://www.ruby-forum.com/t/query-regarding-nginx-working-with-fastcgi/209306/1 "2011-07-11T11:22:48Z")

</div>

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 127.0.0.1 -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.

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.

regards,  
Rishab

---

<div class="post-metadata">

**Author:** ![Rishab\_Jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7cd45c/32.png) [@Rishab\_Jain](https://www.ruby-forum.com/u/Rishab_Jain)\
**Post date:** [July 11, 2011, 12:14pm UTC](https://www.ruby-forum.com/t/query-regarding-nginx-working-with-fastcgi/209306/2 "2011-07-11T12:14:11Z")

</div>

On 7/11/11 5:21 AM, Rishab Jain wrote:

> 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 127.0.0.1 -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 O.

---

<div class="post-metadata">

**Author:** ![Rishab\_Jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7cd45c/32.png) [@Rishab\_Jain](https://www.ruby-forum.com/u/Rishab_Jain)\
**Post date:** [July 11, 2011, 4:13pm UTC](https://www.ruby-forum.com/t/query-regarding-nginx-working-with-fastcgi/209306/3 "2011-07-11T16:13:30Z")

</div>

On 11 Jul 2011 10h21 WEST, removed\_email\_address@domain.invalid wrote:

As suggested above try php-fpm, it provides a native PHP process  
manager. Alternatively to php-fpm and supervisor, I suggest  
monit. This is a C program opposed to supervisor which is Python.

> **[M/Monit](https://mmonit.com/)**
>
> M/Monit

— appa

---

<div class="post-metadata">

**Author:** ![Rishab\_Jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7cd45c/32.png) [@Rishab\_Jain](https://www.ruby-forum.com/u/Rishab_Jain)\
**Post date:** [July 11, 2011, 4:51pm UTC](https://www.ruby-forum.com/t/query-regarding-nginx-working-with-fastcgi/209306/4 "2011-07-11T16:51:51Z")

</div>

On 7/11/11 10:12 AM, Antnio P. P. Almeida wrote:

> On 11 Jul 2011 10h21 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> As suggested above try php-fpm, it provides a native PHP process  
> manager. Alternatively to php-fpm and supervisor, I suggest  
> monit. This is a C program opposed to supervisor which is Python.
> 
> [http://mmonit.com](http://mmonit.com)

Didn’t I actually suggest that? I mentioned that we use Monit for nginx,  
php-fpm, memcached,etc. Where did I suggest Supervisor and php-fpm? I  
actually mentioned Supervisor along with “vanilla” php-cgi although  
Monit would work there too.

If one is really paranoid and wants additional redundancy, Supervisor  
can be used to manage critical daemons and Monit used to monitor  
Supervisor. But that’s beyond the scope of this list and this question.

> — appa
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Jim O.

---

<div class="post-metadata">

**Author:** ![Rishab\_Jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7cd45c/32.png) [@Rishab\_Jain](https://www.ruby-forum.com/u/Rishab_Jain)\
**Post date:** [July 11, 2011, 5:25pm UTC](https://www.ruby-forum.com/t/query-regarding-nginx-working-with-fastcgi/209306/5 "2011-07-11T17:25:48Z")

</div>

On 11 Jul 2011 15h51 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Didn’t I actually suggest that? I mentioned that we use Monit for  
> nginx, php-fpm, memcached,etc. Where did I suggest Supervisor and  
> php-fpm? I actually mentioned Supervisor along with “vanilla”  
> php-cgi although Monit would work there too.

I was unclear in my reply. I suggest monit to monitor php-cgi and do  
the process managing part. But you can always use it as an additional  
monitoring agent. You can monitor php-fpm with it also.

> If one is really paranoid and wants additional redundancy,  
> Supervisor can be used to manage critical daemons and Monit used to  
> monitor Supervisor. But that’s beyond the scope of this list and  
> this question.

Yes.

— appa
