502 Bad Gateway - Nginx and PHP-FPM

Nginx 502 Bad Gateway cost me more time than the actual Ubuntu + PHP-FPM + MySQL installation I was working on. In my case the 502 Bad Gateway error on my installation was the result of a parameter mismatch between /etc/ningx/sites-available/default and /etc/php5/fpm/pool.d/www.conf. In order to solve the 502 Bad Gateway problem edit the Nginx file /etc/ningx/sites-available/default.

1
nano /etc/ningx/sites-available/default

And look for the commented line # fastcgi_pass unix:/var/run/php5-fpm.sock; and uncomment the line.

1
fastcgi\_pass unix:/var/run/php5-fpm.sock;

You location ~ .php$ { section should look similar to mine.

1
2
3
4
5
6
7
8
9
10
11
location ~ .php$ {
fastcgi\_split\_path\_info ^(.+.php)(/.+)$;
# # NOTE: You should have "cgi.fix\_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi\_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi\_pass unix:/var/run/php5-fpm.sock;
fastcgi\_index index.php;
include fastcgi\_params;
}

The final two steps are to restart Nginx and PHP-FPM.

1
sudo service nginx restart
1
sudo service php5-fpm restart

Now you have a working installation like it should have been from the beginning.