# Dynamically Start/Stop FastCGI PHP Processes

**URL:** <https://www.ruby-forum.com/t/dynamically-start-stop-fastcgi-php-processes/217875>\
**Category:** NGINX\
**Created:** [March 15, 2012, 4:31am UTC](https://www.ruby-forum.com/t/dynamically-start-stop-fastcgi-php-processes/217875 "2012-03-15T04:31:02Z")\
**Posts on this page:** 7\
**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:** [March 15, 2012, 4:31am UTC](https://www.ruby-forum.com/t/dynamically-start-stop-fastcgi-php-processes/217875/1 "2012-03-15T04:31:02Z")

</div>

Ok, so I’ve got a crazy idea, but it might just work.

Basically, we have a bunch of PHP pools managed with PHP-FPM. Each site  
has its own single PHP-FPM worker for security reasons.

The majority of the sites don’t get much traffic, so the PHP processes  
don’t need to be running eating up RAM and resources. My thought was  
after N number of minutes of no requests to shut down that PHP worker  
for that site. Then when a request comes in, hold the request in sort-of  
a pause mode, start the worker for that site, and then forward off the  
request. Sure there would be a slight delay on that first request to  
spin up the worker, but should work beautifully.

Does nginx have any plugins to emulate this behavior? I.E. sort of  
hooks? All I would need is a hook from nginx when a site is idle for X  
number of second/minutes. For this first hook, we could write a simple  
bash script which kills that PHP-FPM worker. Then another hook, when a  
request comes in on a given site. Again, another bash script which first  
checks if the PHP-FPM worker is up, if not, holds the requests, starts  
the worker, and then forwards the request.

I was thinking at first, I can use node.js and write a middleware proxy  
that sits between nginx and the PHP-FPM workers, but then quickly  
realized the node.js app would have to speak cgi, and I didn’t want to  
have to deal with that rubbish. 🙂

Posted at Nginx Forum:

> **[Dynamically Start/Stop FastCGI PHP Processes](https://forum.nginx.org/read.php?2,223855,223855#msg-223855)**
>
> Ok, so I've got a crazy idea, but it might just work. Basically, we have a bunch of PHP pools managed with PHP-FPM. Each site has its own single PHP-FPM worker for security reasons. The majority of the sites don't get much traffic, so the PHP...

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 15, 2012, 4:38am UTC](https://www.ruby-forum.com/t/dynamically-start-stop-fastcgi-php-processes/217875/2 "2012-03-15T04:38:36Z")

</div>

Am 15.03.2012 10:30 schrieb “justin” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> a pause mode, start the worker for that site, and then forward off the
> 
> I was thinking at first, I can use node.js and write a middleware proxy  
> that sits between nginx and the PHP-FPM workers, but then quickly  
> realized the node.js app would have to speak cgi, and I didn’t want to  
> have to deal with that rubbish. 🙂

What you’re looking for is php-fpm with 0 initial worker. I’m not sure  
how  
far its current progress but you should try asking php folks. In the  
meantime you can config the pool to start with only one inital worker.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 15, 2012, 4:53am UTC](https://www.ruby-forum.com/t/dynamically-start-stop-fastcgi-php-processes/217875/3 "2012-03-15T04:53:32Z")

</div>

Awesome idea, but the minium appears to be 1. I tried 0 and get the  
following:

Starting php-fpm: [14-Mar-2012 20:52:28] WARNING: [pool justin]  
pm.start\_servers is not set. It’s been set to 1.  
[14-Mar-2012 20:52:28] WARNING: [pool simon] pm.start\_servers is not  
set. It’s been set to 1.

Posted at Nginx Forum:

> **[Re: Dynamically Start/Stop FastCGI PHP Processes](https://forum.nginx.org/read.php?2,223855,223857#msg-223857)**
>
> Ok, so I've got a crazy idea, but it might just work. Basically, we have a bunch of PHP pools managed with PHP-FPM. Each site has its own single PHP-FPM worker for security reasons. The majority of the sites don't get much traffic, so the PHP...

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 15, 2012, 4:56am UTC](https://www.ruby-forum.com/t/dynamically-start-stop-fastcgi-php-processes/217875/4 "2012-03-15T04:56:01Z")

</div>

Am 15.03.2012 10:53 schrieb “justin” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Awesome idea, but the minium appears to be 1. I tried 0 and get the  
> following:

Yes, it is work in progress.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 15, 2012, 5:39am UTC](https://www.ruby-forum.com/t/dynamically-start-stop-fastcgi-php-processes/217875/5 "2012-03-15T05:39:07Z")

</div>

Edho,

Appears to be working, commented out other directives such as  
max\_workers. Added the following:

pm = ondemand  
pm.process\_idle\_timeout = 60  
pm.max\_children = 1

Posted at Nginx Forum:

> **[Re: Dynamically Start/Stop FastCGI PHP Processes](https://forum.nginx.org/read.php?2,223855,223861#msg-223861)**
>
> Ok, so I've got a crazy idea, but it might just work. Basically, we have a bunch of PHP pools managed with PHP-FPM. Each site has its own single PHP-FPM worker for security reasons. The majority of the sites don't get much traffic, so the PHP...

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 15, 2012, 9:42am UTC](https://www.ruby-forum.com/t/dynamically-start-stop-fastcgi-php-processes/217875/6 "2012-03-15T09:42:01Z")

</div>

On 03/15/2012 04:30 AM, justin wrote:

> request. Sure there would be a slight delay on that first request to  
> I was thinking at first, I can use node.js and write a middleware proxy  
> that sits between nginx and the PHP-FPM workers, but then quickly  
> realized the node.js app would have to speak cgi, and I didn’t want to  
> have to deal with that rubbish. 🙂

php-fpm support ‘ondemand’ governor so it will spawn processes as-needed  
and kill them after specified period if not needed (keeping zero workers  
for pool). This is what you are looking for.

– Piotr.

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 15, 2012, 11:22am UTC](https://www.ruby-forum.com/t/dynamically-start-stop-fastcgi-php-processes/217875/7 "2012-03-15T11:22:21Z")

</div>

On 03/15/2012 09:39 AM, Piotr K. wrote:

> On 03/15/2012 04:30 AM, justin wrote:

> > that sits between nginx and the PHP-FPM workers, but then quickly  
> > realized the node.js app would have to speak cgi, and I didn’t want to  
> > have to deal with that rubbish. 🙂
> 
> php-fpm support ‘ondemand’ governor so it will spawn processes as-needed  
> and kill them after specified period if not needed (keeping zero workers  
> for pool). This is what you are looking for.

I’d be interested, whether there is something generic nginx specific,  
which would allow to start / stop any fast-cgi / uwsgi service.

I think it could be rather intersting for a lot of rarely used services  
where a startup penalty is not important.
