Restart PHP-FPM

Using SysV Init scripts directly:

  • /etc/init.d/php-fpm restart   # typical
  • /etc/init.d/php5-fpm restart   # debian-style

Using service wrapper script

  • service php-fpm restart    # typical
  • service php5-fpm restart   # debian-style

Using systemd (newer servers):

  • systemctl restart php-fpm.service   # typical
  • systemctl restart php5-fpm.service   # uncommon

Nginx error 502 bad gateway

When you will find 502 bad gateway error:

  • Nginx running as proxy for Apache web server.
  • Nginx running with PHP-FPM daemon.
  • Nginx running with other services as gateway.
  • Bad buffering/timeout configuration.

In simple words, a gateway, is like an access point, a bridge that communicate one service with another, in this case the gateway can be a service/application that is working and recieving requests from Nginx web server.

Now, let’s explore each one of this reasons to understand the 502 bad gateway message:

Nginx as Proxy for Apache: in this case, the gateway is Apache. When you use Nginx as proxy for Apache, if apache dies or it’s not well configured, it can cause this 502 error. Solution: Most of the times, restarting apache web server will get rid of this, but you must check the log files to know why exactly this was caused.

Nginx with PHP-FPM: same as Apache example, let’s imagine your php daemon stop working, or get’s overloaded by requests, it will not work properly and the all the php requests from nginx would not be served, so, an error will appear. Solution: Restart php-fpm daemon and check the logs.

Source 1: serverfault.com
Source 2: nginxtips.com