Troubleshooting PHP-FPM and Nginx: 502 Bad Gateway Issue on Ubuntu / Debian / Mint

September 10th, 2016 by thelinuxevangelist

Solving PHP-FPM and Nginx: 502 Bad Gateway Error on Linux

The Step-by-step Troubleshooting Shows You How-to Solve the PHP-FPM and Nginx: 502 Bad Gateway Issue on Linux Ubuntu/Debian Based Systems.

Usually this Misbehavior is due to an Improper Ownership of the Nginx Server

Troubleshooting PHP-FPM and Nginx: 502 Bad Gateway Issue on Ubuntu / Debian / Mint - Featured
  1. Give the Right Permissions to Nginx

    sudo usermod -a -G www-data nginx
  2. Set to Listen FastCgi on 127.0.0.1:9000

    cat /etc/nginx/conf.d/default.conf

    Confronts with:

    server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.php index.html index.htm;
    server_name YOURIPADDRESS;
    # pass the PHP scripts to FastCGI server at /var/run/php5-fpm.sock
    location ~ ^/index\.php(/|$) {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS              off;
    }
    }
    

    And then:

    cat /etc/apache2/conf-available/php5-fpm.conf

    Setting the Host parameter as below:

    
    AddHandler php5.fcgi .php
    Action php5.fcgi /php5.fcgi
    Alias /php5.fcgi /usr/lib/cgi-bin/php5.fcgi
    FastCgiExternalServer /usr/lib/cgi-bin/php5.fcgi -host 127.0.0.1:9000 -pass-header Authorization -idle-timeout 3600
    
    Require all granted
    
      
    
  3. Finally Restart php5-fpm & nginx Services

    sudo service php5-fpm restart && sudo service nginx restart

Tags: , , , , , , , , ,