Troubleshooting PHP5-FPM Startup Failures: Debugging 504 Gateway Timeout and Missing Logs


2 views

When you encounter sudo service php5-fpm restart failing while separate stop/start commands work, this typically indicates:

  • The restart command may have timing constraints
  • Potential PID file handling issues during fast restarts
  • Child process cleanup problems

PHP-FPM often logs to these locations (Ubuntu/Debian):

/var/log/php5-fpm.log
/var/log/syslog
/var/log/daemon.log
journalctl -u php5-fpm

For custom configurations, check your pool's error_log directive:

grep -r "error_log" /etc/php5/fpm/

Edit your php-fpm.conf (usually in /etc/php5/fpm):

log_level = debug
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10s

Try starting php-fpm in foreground with debug mode:

sudo php5-fpm -F -d log_level=debug -d error_log=/var/log/php5-fpm.debug.log

Check running processes for clues:

ps auxf | grep php
sudo lsof -i :9000
sudo netstat -tulnp | grep php

Test your configuration before applying changes:

sudo php5-fpm -t

Common issues to verify:

; In /etc/php5/fpm/pool.d/www.conf
listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

Try direct initialization when service commands fail:

sudo /usr/sbin/php5-fpm --nodaemonize --fpm-config /etc/php5/fpm/php-fpm.conf

Check systemd's view of the service (for newer systems):

sudo systemctl status php5-fpm
sudo journalctl -xe
  1. Kill any orphaned processes: sudo pkill -9 php5-fpm
  2. Remove stale socket/PID files: sudo rm /var/run/php5-fpm.sock /var/run/php5-fpm.pid
  3. Verify permissions: sudo chown -R www-data:www-data /var/run/php5-fpm

When encountering PHP5-FPM service failures, the symptoms you described are classic indicators of configuration or resource issues:

$ sudo service php5-fpm restart
 * Restarting PHP5 FastCGI Process Manager php5-fpm
   ...fail!

Yet curiously, separate stop/start commands work:

$ sudo service php5-fpm stop
 * Stopping PHP5 FastCGI Process Manager php5-fpm
   ...done.

$ sudo service php5-fpm start
 * Starting PHP5 FastCGI Process Manager php5-fpm
   ...done.

First, verify all potential log locations:

$ sudo find /var/log -name "*php*" -type f
$ sudo tail -f /var/log/php5-fpm.log
$ journalctl -u php5-fpm -n 50 --no-pager
$ sudo grep -Ri "php5-fpm" /var/log/

If logs appear empty, check syslog configuration:

$ sudo nano /etc/php5/fpm/php-fpm.conf
; Ensure these settings exist:
error_log = /var/log/php5-fpm.log
log_level = notice

The restart vs stop/start discrepancy often indicates process handling differences. Let's examine the init script:

$ sudo nano /etc/init.d/php5-fpm
; Look for force-stop conditions or PID handling

; Alternative service commands:
$ sudo /etc/init.d/php5-fpm restart --debug
$ sudo strace -f service php5-fpm restart

Validate your PHP-FPM configuration with:

$ sudo php5-fpm -t
[06-Aug-2023] ERROR: unable to bind listening socket for address '/var/run/php5-fpm.sock': No such file or directory (2)
[06-Aug-2023] ERROR: FPM initialization failed

Check pool configurations:

$ ls -la /etc/php5/fpm/pool.d/
$ sudo php5-fpm -y /etc/php5/fpm/php-fpm.conf -t

Critical directory permissions:

$ sudo mkdir -p /var/run/php5-fpm
$ sudo chown -R www-data:www-data /var/run/php5-fpm
$ sudo chmod 755 /var/run/php5-fpm

; Verify socket configuration:
$ sudo grep -r "listen" /etc/php5/fpm/
listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data

For stubborn cases, use these diagnostic commands:

$ sudo lsof -Pi | grep php
$ sudo netstat -tulnp | grep php
$ sudo strace -s 80 -ff -o /tmp/phpdebug -p $(pgrep php5-fpm)
$ sudo gdb -p $(pgrep php5-fpm)

Bypass service manager to isolate issues:

$ sudo pkill php5-fpm
$ sudo /usr/sbin/php5-fpm --nodaemonize --fpm-config /etc/php5/fpm/php-fpm.conf

This will display real-time output and any startup errors directly in your terminal.