502 Bad Gateway - Linux, php7, Nginx. How to resolve this error?

Asked

Viewed 9,659 times

4

I am having a 502 error that represents some configuration on the server, when I try to open a site using linux, Nginx and php7. Researching some other POSTS, link about the same error I see that php needs fpm to be recognized in Nginx. However fpm is installed and updated. See photo below:

inserir a descrição da imagem aqui

According to the error 505 something’s going on on the server. It shouldn’t be php-7 (I think). Does anyone have any idea what it might be?

Updating

And finally it seems that it is an error due to PHP. I tested with an html file and I had no problems. So it seems that there is something with php itself. What can it be?

inserir a descrição da imagem aqui

Latest logs

2016/07/06 06:14:56 [crit] 834#834: *15 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: ig.app, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ig.app"

2016/07/06 06:15:04 [crit] 834#834: *15 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: ig.app, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ig.app"

2016/07/06 06:15:04 [crit] 834#834: *15 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: ig.app, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ig.app"

2016/07/06 06:16:29 [crit] 834#834: *15 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: ig.app, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "ig.app"
  • 1

    You can paste the Nginx log?

  • 2

    Yeah, it looks like he’s looking for a php5-fpm file and isn’t finding it. But I’m using it with php-7 that already has fpm. Any idea?

  • 1

    In the configuration file /etc/nginx/sites-available/default you update from fastcgi_pass unix:/var/run/php5-fpm.sock; for fastcgi_pass unix:/var/run/php7-fpm.sock;

  • 2

    that’s right. You almost got it right. instead of changing in the folder default I changed in the project folder, in case ig.app.conf. A note: in the installation of linux php7 16.04 LTS through the apt install php-dev or apt install php (that in linux 16.04 will install php7.0 as default), note that after the folder /var/run/ there is still one more directory /php. I mean, the whole way is /var/run/php/php7-fpm.sock and not /var/run/php7-fpm.sock. Put your answer to receive the credits.

  • Very good! : ) Put this as an answer because someone looking for this problem on the main page will see that no answer and may not even click

1 answer

1


Nginx communicates with PHP-FPM using a Unix domain socket. Sockets map to a path in the air system.

Version 5.x of PHP used the path /var/run/php5-fpm.sock

Should stay like this:

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

For PHP7.x the way is this /var/run/php/php7.4-fpm.sock (example, change the .4 for another if your version is different) and should look like this:

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

Exemplo da configuração para PHP7.x

The site configuration can be found in different folders depending on the system you use. So search if you can’t find it. In this answer we will use one of the standard paths.

To edit the configuration file you can use nano (or another editor), it is necessary to use sudo/su (depending on whether it is based on Debian or Ubuntu), for example:

> sudo nano /etc/nginx/sites-available/default

server {
    listen 80;
    root /usr/share/nginx/www;
    index index.php index.html index.htm;
    server_name example.com;

    location / {
        try_files $uri $uri/ /index.html;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    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;
    }
}

SOCKET settings may vary due to the NGINX version, as well as the OS version. PHP version may also interfere. If your server does not have any references in standard files. Always search by citing the operating system. Note that the "Socket version" is always changed, along with the PHP-FPM version. But not necessarily the number as in the example should be present.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.