Fixing 504 Gateway Time Out (Nginx & PHP5-FPM)

Sometimes things just get overloaded

Since upgrading the server from Ubuntu 12.04 to 14.04 a couple of months ago, we ran into this problem: ‘lfd on High 5 minute load average alert‘.

Because, during the upgrade, some files were replaced with newer versions such as PHP5-FPM’s php.ini file, not all of our server configurations were exactly as they had been on the older version of Ubuntu. To quickly rectify the dreaded 504 Gateway Timeout, I needed to modify both the php.ini and www.conf files located in /etc/php5/fpm and /etc/php5/fpm/pool.d respectively.

In php.ini, I upped the max_execution_time from 30 to 300.

; Resource Limits ;

; Maximum execution time of each script, in seconds
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 300

In www.conf, the ;request_terminate_timeout needed to be uncommented (remove the semi-colon) and upped.

request_terminate_timeout = 300

And then restart the service.

$ sudo service php5-fpm restart