e update nginx to 1.4.7 and php to 5.5.12, After that I got the 502 error. Before we update everything works fine.

nginx-error.log

[pastacode lang=”php” manual=”2014%2F05%2F03%2013%3A27%3A41%20%5Bcrit%5D%204202%230%3A%20*1%20connect()%20to%20unix%3A%2Fvar%2Frun%2Fphp5-fpm.sock%20failed%20(13%3A%20Permission%20denied)%20while%20connecting%20to%20upstream%2C%20client%3A%20xx.xxx.xx.xx%2C%20server%3A%20localhost%2C%20request%3A%20%22GET%20%2F%20HTTP%2F1.1%22%2C%20upstream%3A%20%22fastcgi%3A%2F%2Funix%3A%2Fvar%2Frun%2Fphp5-fpm.sock%3A%22%2C%20host%3A%20%22xx.xx.xx.xx%22%0Anginx.conf” message=”Php Code” highlight=”” provider=”manual”/] [pastacode lang=”php” manual=”user%20%20www%20www%3B%0Aworker_processes%20%201%3B%0A%0A%20%20%20%20%20%20%20%20location%20%2F%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20root%20%20%20%2Fusr%2Fhome%2Fuser%2Fpublic_html%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20index%20%20index.php%20index.html%20index.htm%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20location%20~%20%5B%5E%2F%5D%5C.php(%2F%7C%24)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20fastcgi_split_path_info%20%5E(.%2B%3F%5C.php)(%2F.*)%24%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20fastcgi_pass%20unix%3A%2Fvar%2Frun%2Fphp5-fpm.sock%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20fastcgi_index%20index.php%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20fastcgi_param%20%20SCRIPT_FILENAME%20%20%20%20%2Fusr%2Fhome%2Fuser%2Fpublic_html%24fastcgi_script_name%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20include%20fastcgi_params%3B%0A%20%20%20%20%20%20%20%20%7D” message=”Php Code” highlight=”” provider=”manual”/]

  • we had a similar error after php update. PHP fixed a security bug where o had rw permission to the socket file.
  • Open /etc/php5/fpm/pool.d/www.conf or /etc/php/7.0/fpm/pool.d/www.conf, depending on your version.

Uncomment all permission lines, like:

[pastacode lang=”php” manual=”listen.owner%20%3D%20www-data%0Alisten.group%20%3D%20www-data%0Alisten.mode%20%3D%200660″ message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Restart fpm – sudo service php5-fpm restart or sudo service php7.0-fpm restart

Note: if your webserver runs as user other than www-data, you will need to update the www.conf file accordingly

  • All the fixes currently mentioned here basically enable the security hole all over again.
  • What we ended up doing is adding the following lines to my PHP-FPM configuration file.
[pastacode lang=”php” manual=”listen.owner%20%3D%20www-data%0Alisten.group%20%3D%20www-data” message=”Php Code” highlight=”” provider=”manual”/]

Make sure that www-data is actually the user the nginx worker is running as. For debian it’s www-data by default.

Doing it this way does not enable the security problem that this change was supposed to fix.

  • Make sure you have these lines uncommented in /etc/php5/fpm/pool.d/www.conf:
[pastacode lang=”php” manual=”listen.owner%20%3D%20www-data%0Alisten.group%20%3D%20www-data%0Alisten.mode%20%3D%200660″ message=”Php Code” highlight=”” provider=”manual”/]

Make sure /etc/nginx/fastcgi_params looks like this:

[pastacode lang=”php” manual=”fastcgi_param%20%20QUERY_STRING%20%20%20%20%20%20%20%24query_string%3B%0Afastcgi_param%20%20REQUEST_METHOD%20%20%20%20%20%24request_method%3B%0Afastcgi_param%20%20CONTENT_TYPE%20%20%20%20%20%20%20%24content_type%3B%0Afastcgi_param%20%20CONTENT_LENGTH%20%20%20%20%20%24content_length%3B%0A%0Afastcgi_param%20%20SCRIPT_NAME%20%20%20%20%20%20%20%20%24fastcgi_script_name%3B%0Afastcgi_param%20%20REQUEST_URI%20%20%20%20%20%20%20%20%24request_uri%3B%0Afastcgi_param%20%20DOCUMENT_URI%20%20%20%20%20%20%20%24document_uri%3B%0Afastcgi_param%20%20DOCUMENT_ROOT%20%20%20%20%20%20%24document_root%3B%0Afastcgi_param%20%20SCRIPT_FILENAME%20%20%20%20%24document_root%24fastcgi_script_name%3B%0Afastcgi_param%20%20SERVER_PROTOCOL%20%20%20%20%24server_protocol%3B%0Afastcgi_param%20%20PATH_INFO%20%20%20%20%20%20%20%20%20%20%24fastcgi_script_name%3B%0Afastcgi_param%20%20HTTPS%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24https%20if_not_empty%3B” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”] [pastacode lang=”php” manual=”fastcgi_param%20%20GATEWAY_INTERFACE%20%20CGI%2F1.1%3B%0Afastcgi_param%20%20SERVER_SOFTWARE%20%20%20%20nginx%2F%24nginx_version%3B%0A%0Afastcgi_param%20%20REMOTE_ADDR%20%20%20%20%20%20%20%20%24remote_addr%3B%0Afastcgi_param%20%20REMOTE_PORT%20%20%20%20%20%20%20%20%24remote_port%3B%0Afastcgi_param%20%20SERVER_ADDR%20%20%20%20%20%20%20%20%24server_addr%3B%0Afastcgi_param%20%20SERVER_PORT%20%20%20%20%20%20%20%20%24server_port%3B%0Afastcgi_param%20%20SERVER_NAME%20%20%20%20%20%20%20%20%24server_name%3B%0A%0A%23%20PHP%20only%2C%20required%20if%20PHP%20was%20built%20with%20–enable-force-cgi-redirect%0Afastcgi_param%20%20REDIRECT_STATUS%20%20%20%20200%3B” message=”Php Code” highlight=”” provider=”manual”/]

These two lines were missing from my /etc/nginx/fastcgi_params, make sure they are there!

[pastacode lang=”php” manual=”fastcgi_param%20%20SCRIPT_FILENAME%20%20%20%20%24document_root%24fastcgi_script_name%3B%0Afastcgi_param%20%20PATH_INFO%20%20%20%20%20%20%20%20%20%20%24fastcgi_script_name%3B” message=”Php Code” highlight=”” provider=”manual”/]

Then, restart php5-fpm and nginx. Should do the trick.

  • In fact, “listen.mode” should be: “0660” and not “0666” as Other Writable or Other Readable is never a good choice here.
  • So try to find out as which user/group your webserver runs.

I use CentOs and it runs as user “nginx” So add to your php-fpm.conf:

[pastacode lang=”php” manual=”listen.owner%20%3D%20nginx%0Alisten.group%20%3D%20nginx%0Alisten.mode%20%3D%200660″ message=”Php Code” highlight=”” provider=”manual”/]

  • Check which user runs nginx. As of Ubuntu 12.04 nginx runs by nginx user which is not a member of www-data group.
[pastacode lang=”php” manual=”usermod%20-a%20-G%20www-data%20nginx” message=”Php Code” highlight=”” provider=”manual”/]

and restarting nginx and php5-fpm daemons solves the problem of nginx error.

  • Alternative to broadening permissions in your php config, you could change the user specified in your nginx config.
  • On the first line of your nginx.conf excerpt above, the user and group are specified as www and www, respectively.
[pastacode lang=”php” manual=”user%20%20www%20www%3B” message=”Php Code” highlight=”” provider=”manual”/]

Meanwhile, your php config probably specifies a user and group of www-data:

[pastacode lang=”php” manual=”listen.owner%20%3D%20www-data%0Alisten.group%20%3D%20www-data” message=”Php Code” highlight=”” provider=”manual”/]

You might change the line in your nginx.conf, to any of the following, then:

[pastacode lang=”php” manual=”user%20www-data%20www%3B%0Auser%20www-data%20www-data%3B%20%23%20or%20any%20group%2C%20really%2C%20since%20you%20have%20the%20user%20matching%0Auser%20www%20www-data%3B%20%23%20requires%20that%20your%20php%20listen.mode%20gives%20rw%20access%20to%20the%20group” message=”Php code” highlight=”” provider=”manual”/] [ad type=”banner”]

  • Consideration must also be given to your individual FPM pools, if any.
  • The listen.user and listen.group were duplicated on a per-pool basis.
  • If you used pools for different user accounts like, where each user account owns their FPM processes and sockets, setting only the default listen.owner and listen.group configuration options to ‘nginx’ will simply not work.
  • And obviously, letting ‘nginx‘ own them all is not acceptable either.

For each pool, make sure that

[pastacode lang=”php” manual=”listen.group%20%3D%20nginx” message=”Php Code” highlight=”” provider=”manual”/]

Otherwise, you can leave the pool’s ownership and such alone.

  • Simple but works..
[pastacode lang=”php” manual=”listen.owner%20%3D%20nginx%0Alisten.group%20%3D%20nginx%0Achown%20nginx%3Anginx%20%2Fvar%2Frun%2Fphp-fpm%2Fphp-fpm.sock” message=”Php Code” highlight=”” provider=”manual”/]

Categorized in: