Dealing PHP-FPM Crash & Nginx 502 Bad Gateway Error with Shell Scripts

Having PHP-FPM crash often seems quite inevitable when there’s a long debugging list of third-party modules, memory leaks, server configurations, resources and the list goes on. There’s just too much to look into.

Disclaimer: As I’m frequently updating the original guides and installers here on, I will not be responsible for any brick issues if you were to follow my obsolete guides copied elsewhere. Thus subscribe to this post to get latest updates. Modifying any part of a device may void its warranty.

Trying to upgrade and tweak PHP-FPM, MySQL and Nginx setup to match my NAS limited memory constrain (256MB) don’t seem to help much. PHP-FPM would just crash while doing some rather heavy jobs such as image processing. And sometimes it would just gave up out of no where even there’s still enough RAM to handle the request. You could just imagine waking up and your site is totally offline. This is a nightmare for most small site owners. They could be missing potential hits. Those sites with thousands hits would probably have gigabytes of RAM equipped, so lesser prone to this problem.

<center>Email notifications when PHP-FPM restarted.</center>
Email notifications when PHP-FPM restarted.

After hours of googling for solutions with no definite answer, I though of an approach to probe the PHP-FPM process in case it goes down. This check was done every fifteen minutes via root account cronjob calling a shell script. The shell script on the other hand triggers a Curl request to my site’s WordPress PHP cron script. If the result is not HTTP “200 OK”, I would expect the PHP-FPM process is no longer active, after which the shell script will try to restart the PHP-FPM service. This script will also check Nginx and MySQL services just in case they too needed an emergency jumpstart.

This works as intended but I wasn’t satisfied because there’s a fifteen minutes gap in between the checks which visitors will still get 502 errors when PHP-FPM goes offline. Another better workaround, “Dealing PHP-FPM Crash & Nginx 502 Bad Gateway Error with Perl FastCGI“…

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see