Nginx – Getting 502 Bad Gateway, php-fpm takes 100% cpu

cpu-usagenginxphp-fpmUbuntu

Okay, last night, around 2am on one of my servers the cpu usages went up to 100%. When I checked in this morning, I saw 5 php-fpm processes taking up around 100% cpu together. I've rebooted the server, but everytime the server comes back, the php-fpm processes start again and take 100% cpu.

It a Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-32-generic x86_64) server, I've updated all software and I just can't get any information what's causing this.

Obviously the problem causes the websites hosted on the server to be offline, all the time showing either a 502 Bad Gateway or a 504 Gateway Time-out error…

I'm hoping someone can offer some help in fixing this.

I've got 3 other servers, with the exact same setup who don't have any of these problems.

EDIT 1:
When I look at NewRelic, it shows me that around the same time the problems started, there's a huge increase in "Packets per second" in the network tab, That must have something to do with the problem…

Best Answer

I had similar problem with PHP-FPM and it used to take 100% CPU even during the night time when the traffic was less.

You will have to bit optimize the PHP-FPM configuration files. Make sure that you have this entry present :-

pm = ondemand pm.max_children = 2 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3

If it's not ondemand, then change to ondemand otherwise PHP-FPM will try and take lot of CPU resource. With ondemand , it will start more process when there will be a need for that.

The configuration files for PHP-FPM are located at :-

/etc/php5/fpm/php-fpm.conf

There is also a pool configured at pool.d/www.conf .

Check both of these files for entries and optimize those above settings accordingly and restart your PHP-FPM.