Quick Fix: 502 Bad Gateway on Nginx on Fresh Ubuntu 18.04 installation! (PHP-FPM)

Here is a quick fix for an issue I’ve seen come up over and over again, you’ve got a fresh install of php-fpm on Ubuntu 18.04, you’ve enabled it through /etc/nginx/sites-enabled/default and now on any .php page you receive: 502 Bad Gateway. Here is a quick and easy way to fix it.

Step 1: Make sure PHP-FPM is installed at all.

When you install php-fpm you have to specify it, installing php7.2 will not do it. Use the following command:

sudo apt-get install php7.2-fpm

Step 2: Make sure php-fpm is enabled

Open the sites configuration problem with:

sudo nano /etc/nginx/sites-enabled/default

Ensure you have edited this file and make sure that php-fpm is enabled, simply uncomment a few lines.

# pass PHP scripts to FastCGI server
         #
         location ~ .php$ {
                 include snippets/fastcgi-php.conf;
         #
         #       # With php-fpm (or other unix sockets):
                 fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         #       # With php-cgi (or other tcp sockets):
         #       fastcgi_pass 127.0.0.1:9000;
         }

Notice the lines that got uncommented including the last line with the ending bracket, Nginx will not start if you don’t close that bracket. Also notice the “fastcgi_pass” line where I changed php7.0-fpm.sock to php7.2-fpm.sock. Ubuntu uses php7.0 and Ubuntu 18.04 uses php7.2. For an unknown reason the file has never been changed for php7.2 however a bug report has been issued by us here: https://bugs.launchpad.net/ubuntu/+source/nginx/+bug/1831748

Step 3: Make sure Nginx got restarted

Any changes to the site configuration files requires a Nginx restart, use this command:

sudo service nginx restart

An error will present itself if something is wrong, if nothing is displayed all should be well! Test a .php file like this quick code:

<?php echo "Test"; ?> 

And see if it works now!

Still having issues? Post below and we will help!

Have a great day!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.