phpnginxbad-gateway

nginx php-fpm 502 Bad Gateway


I was running Ubuntu server 20.04 quite successfully with Ired mail and 2 websites, one of them with WordPress.

I wanted to install Nextcloud, to do that I had to reinstall php-fpm to generate php7.4-fpm.sock. After this Nextcloud worked, but my other websites stopped working with error '502 Bad Gateway'.

So to say the least, I'm very confused!

I followed this article to install Nextcloud and set up the sites-enabled .conf file as per instructions: https://www.linuxbabe.com/ubuntu/install-nextcloud-ubuntu-20-04-nginx-lemp-stack/amp

I think I understand that the .conf file used to listen on 127.0.0.1:XXXX and now listens on php7.4-fpm.sock?

Here is the .conf file that I have put together for my website after re-installing php-fpm:

#
# Note: This file must be loaded before other virtual host config files,
#
# HTTPS
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name SOMEWEBSITE www.SOMEWEBSITE;

    error_log /var/log/nginx/localhost.error_log info;

    root /var/www/SOMEWEBSITE/html;
    index index.php index.html;

    include /etc/nginx/templates/misc.tmpl;
    include /etc/nginx/templates/ssl.tmpl;
    include /etc/nginx/templates/iredadmin.tmpl;
    include /etc/nginx/templates/roundcube.tmpl;
    include /etc/nginx/templates/sogo.tmpl;
    include /etc/nginx/templates/netdata.tmpl;
    include /etc/nginx/templates/php-catchall.tmpl;
    include /etc/nginx/templates/stub_status.tmpl;
        
    location / {
        try_files $uri $uri/ /index.php?q=$uri$args;
    }
        
    # PHP handling
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    ssl_certificate /etc/letsencrypt/live/SOMEWEBSITE/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/SOMEWEBSITE/privkey.pem; # managed by Certbot

}
# Redirect http to https
server {
    listen 80;
    listen [::]:80;
    server_name SOMEWEBSITE www.SOMEWEBSITE;

    return 301 https://$host$request_uri;
}

I have checked the file permissions for php7.4-fpm.sock

ll /var/run/php/ | grep php

-rw-r--r--  1 root     root        3 May 22 21:13 php7.4-fpm.pid
srw-rw----  1 www-data www-data    0 May 22 21:13 php7.4-fpm.sock=
lrwxrwxrwx  1 root     root       30 May 22 21:13 php-fpm.sock -> /etc/alternatives/php-fpm.sock=

and I think it looks ok.

here is the log file:

2021/05/23 20:32:52 [error] 43596#43596: *305 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xxx.xxx, server: SOMEWEBSITE, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9999", host: "SOMEWEBSITE"
2021/05/23 20:32:53 [info] 43596#43596: *305 client xx.xx.xxx.xxx closed keepalive connection

Any Ideas? Need any more information? Thank you in advance for looking.


Solution

  • PHP-FPM can listen using two method for accepting fastcgi request. using TCP Socket or with Unix Socket.

    You can sepecify it in php-fpm configuration, In Ubuntu the configuration is in /etc/php/7.4/fpm/pool.d/www.conf and check listen configuration.

    If you want to use unix socket, use configuration below.

    listen = /run/php/php7.4-fpm.sock
    

    For TCP Socket.

    listen = 127.0.0.1:9000
    

    Next in nginx you can specify fastcgi_pass based on fpm configuration. If you using Unix socket, all your website, include Nextcloud must be using Unix Socket.

    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    

    If you using TCP Socket, you must change the nginx configuration for Nextcloud to pass from TCP Socket.

    fastcgi_pass 127.0.0.1:9000;