11

So I've got a Ubuntu server that I'm trying to restore from a backup. And when a dummy PHP version page I get 502 Bad Gateway. What have I tried to fix it? Quite a few things including the answers from this related question - nginx 502 bad gateway

This is my default config with Ubuntu

server {
listen 80;
#   listen [::]:81 ipv6only=on default_server;

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

server_name localhost;

location ~* \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
        expires max;
}


location / {
    try_files $uri $uri.php $uri.php/$args /index.php?q=$uri&$args $uri/ =404;
    index index.php index.html index.htm;
    rewrite ^(.*)$ /$1.php;
}

location /phpmyadmin {
  index index.php;
    try_files $uri $uri/ =404;
    auth_basic "Admin Login";
    auth_basic_user_file /etc/nginx/pma_pass;
}

error_page 404 /404.html;

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

location ~ \.php$ {
    try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}


location ~ /\.ht {
    deny all;
}
}

EDIT: Here's what I think is the relevant part of the log

2017/08/11 13:18:13 [error] 27759#0: *270 connect() failed (111: Connection refused) while connecting to upstream, client: 66.61.18.112, server: domain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8080/favicon.ico", host: "[IP ADDRESS]", referrer: "http://[IP ADDRESS]/phpmyadmin/index.php"

EDIT 2:

Here's the result when I run ps aux | grep php-fpm:

mre      21685  0.0  0.0  11756   932 pts/0    S+   10:42   0:00 grep --color=auto php-fpm
root     32721  0.0  1.0 269300 11168 ?        Ss   Aug11   0:30 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)                    
www-data 32724  0.0  0.4 269300  4540 ?        S    Aug11   0:00 php-fpm: pool www                                                       
www-data 32725  0.0  0.4 269300  4540 ?        S    Aug11   0:00 php-fpm: pool www 

Edit 3:

Here's what's uncommented out of my php-fpm.conf

[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = /var/run/php5-fpm.pid

; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written
; in a local file.
; Note: the default prefix is /var
; Default Value: log/php-fpm.log
error_log = /var/log/php5-fpm.log


;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ; 
;;;;;;;;;;;;;;;;;;;;

; To configure the pools it is recommended to have one .conf file per
; pool in the following directory:
include=/etc/php5/fpm/pool.d/*.conf

Edit 4:

Here's what's uncommented from my www.conf:

listen = 127.0.0.1:9000
listen.owner = www-data  
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /

Edit 5:

My nginx log after I try to go to index.php:

[error] 29393#0: *23 connect() failed (111: Connection refused) while connecting to upstream, client: [Computer IP Address], server: [Subdomain.domain.com], request: "GET /favicon.ico HTTP/1.1", upstream:"http://127.0.0.1:8080/favicon.ico", host: [Server IP], referrer: "http://[Server IP]/index.php"
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
  • What URL path are you trying? Does `fastcgi_params` contain any directives that may override what you set above? (try placing `include fastcgi_params;` above other fastcgi directives and restart nginx) – Rob W Aug 11 '17 at 18:45
  • Same issue, 502 when I moved the line and restarted ngxinx – MrEngineer13 Aug 11 '17 at 19:01
  • 1
    You should be seeing an entry for that 502 in your nginx error logs. Strange that you're not. Check your PHP logs as well. Sorry I can't be of more help :( – Rob W Aug 11 '17 at 19:04
  • Is there a directory `/etc/php5/fpm/pool.d/` and if there is, what files does it contain? It seems that either you don't have a default pool settings, or there is an miss-configuration to the default pool inside that directory. – Christos Lytras Aug 14 '17 at 16:37
  • There is a directory that contains www.conf – MrEngineer13 Aug 14 '17 at 16:42
  • Pretty sure that Nginx conf file is not what is generating the error. The error log shows favicon being proxied which your conf file would not do. I reckon another instance of Nginx is active on your server. – Dayo Aug 14 '17 at 17:01
  • Could you please add `error_log /var/log/nginx/error.log;` to your nginx conf. Restart nginx. Perform request and check this error file. – cn007b Aug 14 '17 at 18:49
  • Please add the contents of that `www.conf` file. That is the default pool config file and it should set how php is running socket or port setting. – Christos Lytras Aug 14 '17 at 19:33
  • Also try to restart both nginx and php-fpm daemons. – Christos Lytras Aug 14 '17 at 19:43
  • I added the errror file and the www.conf – MrEngineer13 Aug 14 '17 at 19:59
  • Please try to replace inside the site/docroot conf `fastcgi_pass unix:/var/run/php5-fpm.sock;` with `fastcgi_pass 127.0.0.1:9000;`, restart nginx and php-fpm and try again. – Christos Lytras Aug 14 '17 at 20:26
  • It is not the only config that's being used. This one is for `server_name localhost;`. Run `nginx -t` to check which configs are loaded. The one that tries to connect to upstream 127.0.0.1:8080 is used to handle the request to static content. Also the log entry `GET /favicon.ico HTTP/1.1" .... referrer: "http://[IP ADDRESS]/phpmyadmin/index.php"` implies previous request to `http://[IP ADDRESS]/phpmyadmin/index.php` was successful and returned valid html header with favicon location. – Alex Blex Aug 15 '17 at 14:20
  • Please add the version of Ubuntu , Nginx and PHP you are using. and full contents of www.conf – Vamsi Krishna B Aug 15 '17 at 16:12
  • @MrEngineer13 can you please add the contents of `/etc/nginx/nginx.conf` main configuration file. Maybe this file does not include the site's vhost files. In my configuration for example, there is a line inside `nginx.conf` that includes everything inside `/etc/nginx/sites-enabled/` like this `include /etc/nginx/sites-enabled/*.vhost;`. – Christos Lytras Aug 16 '17 at 20:28

3 Answers3

5

So I checked out my nginx config and found these lines:

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

Then I went to /etc/nginx/conf.d/*.conf and saw that in the servers.conf file, there was a config for a sub domain that I planned on using but won't anymore so I deleted it. This was a big part of the problem because nginx was looking in this config first and that's why the errors included this subdomain.

As for the php problems, I switched my config to fastcgi_pass unix:/run/php/php7.0-fpm.sock; and that seems to have fixed the problem.

MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
  • 1
    I just wanted to tell you that I posted my last comment before I saw this answer! I was so close! I have 3 Ngnix production servers and I was facing many of these issues myself. Nice that you got that solved. – Christos Lytras Aug 16 '17 at 22:19
2

Try to replace

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

in your nginx config to

fastcgi_pass 127.0.0.1:9000;

Because if your PHP-FPM config you use TCP instead of socket (it's OK).

Alexey Shokov
  • 4,775
  • 1
  • 21
  • 22
2

You have to change this line

from:

listen = 127.0.0.1:9000

to:

listen = /var/run/php5-fpm.sock

in your www.conf file

Don't forget to restart php.

Explanation:

If you look at your nginx virtual host fastcgi_pass parameter

location ~ \.php$ {
    try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}

you'll see that you are pointing processing of php files to php5-fpm socket but in your www.conf file php is listening port 9000 which is why you should change it to /var/run/php5-fpm.sock

paravibe
  • 121
  • 6