PHP-FPM + NGINX: PHP-CGI processes die after a while in idle, then 502 bad gateway and manual restart required

7,685

I am running a very similar set up (nginx 0.7.61, php+fpm 5.2.10) and my PHP processes never die, even after being idle.

The process control bits of my php-fpm.conf are as follows:

<value name="pm">
    <value name="style">static</value>
    <value name="max_children">3</value>
    <value name="apache_like">
        <value name="StartServers">20</value>
        <value name="MinSpareServers">5</value>
        <value name="MaxSpareServers">35</value>
    </value>
</value>
<value name="request_terminate_timeout">0s</value>
<value name="request_slowlog_timeout">0s</value>
<value name="slowlog">logs/slow.log</value>
<value name="rlimit_files">1024</value>
<value name="rlimit_core">0</value>
<value name="chroot"></value>
<value name="chdir"></value>
<value name="catch_workers_output">yes</value>
<value name="max_requests">500</value>

Note that while I have the apache-like bits defined (they were part of the default config), they aren't used because I have PM style static set.

You can turn up the logs to debug level using this in the global options section:

<value name="log_level">debug</value>

to see if there's a reported reason that it's shutting down workers.

A latch-ditch fix if this doesn't work would be to have a service like pingdom hit a php on your site every x minutes, but my experience with this software combo doesn't suggest that this should be required.

Share:
7,685

Related videos on Youtube

Vito Botta
Author by

Vito Botta

I am a passionate developer with 10+ years commercial experience and 360° skills in the design, development and maintenance of modern, user centred as well as enterprise-scale web applications on both *nix and Microsoft platforms, with a strong interest in performance, scalability, security and search engine optimisation. My blog with tips and walkthroughs on web development with free and open source tools (such as Linux, Ruby, Rails, MySQL), performance &amp; scalability, security, database, search engine optimisation.

Updated on September 17, 2022

Comments

  • Vito Botta
    Vito Botta over 1 year

    I am currently setting up a VPS (with VPS.NET) which I will be using to host a blog and some other stuff. I've installed nginx, and patched php (5.2.8) with php-fpm.

    All works great (and extremely fast!), apart from one annoying issue: because the website has no other traffic than mine for now, after a while it has been in idle all the php-cgi processes die. As soon as I then try to visit the website, nginx returns a nice "502 bad gateway", and to fix I have to manually restart php-fpm to get the website to work again. Why is that? I have seen this is somehow a common problem but after a few days looking for a solution in my spare time, I have found none that works for me.

    Any suggestions? Thanks in advance

  • Vito Botta
    Vito Botta almost 15 years
    Hi, thanks for replying. I have changed the log_level parameter, I will let you know what I see. For now in php-fpm.log I see "notices" like this: "[WARNING] fpm_children_bury(), line 215: child 26275 (pool default) exited on signal 15 SIGTERM after 803.236849 seconds from star" every time that problem happens.
  • Vito Botta
    Vito Botta almost 15 years
    ops.. I meant warnings. Soon before I see this notice: "fpm_got_signal(), line 48: received SIGCHLD"
  • Vito Botta
    Vito Botta almost 15 years
    Hi, thanks. I have tried to export those env variables in the init.d script, but I haven't seen any difference. I am not too sure I have 100% understood how this stuff works. So, each php process will serve a limited number of requests before terminating; but what happens when this limit is reached and a php process terminates? Is a new php process automatically started?
  • Vito Botta
    Vito Botta almost 15 years
    BTW in my case I don't even reach that limit, because I have no traffic on the blog I am working on other than mine. That issue happens also if I make a couple of requests/page views and then leave it for a while. Then, as usual, I'll see those 520s :(
  • Joyce
    Joyce almost 15 years
    SIGCHLD is sent to a parent process when a child dies, the SIGTERM line is the parent reaping the child and determining why it exited. The question is whether the child is dying or the parent is killing it due to it hitting max_requests. What PHP app are you hosting here, and is there any chance that part of it is misbehaved in such a way that it sends itself TERM under some condition?
  • Vito Botta
    Vito Botta almost 15 years
    As said in the other comment, I don't even reach the max_requests. You said you have max_children set to 3. What is the value of When emergency_restart_threshold in your configuration?
  • Vito Botta
    Vito Botta almost 15 years
    "When this amount of php processes exited with SIGSEGV or SIGBUS ... <value name="emergency_restart_threshold">10</value> " Could this be the problem - that is, this value being lower than max_children?