Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx cannot open the PHP-FPM socket: Permission denied

Errorconnect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied) while connecting to upstream

Short answer

nginx runs as one user and PHP-FPM creates its socket for another. On Linux the socket needs read and write permission for the connecting user. Compare the socket's owner, group and mode with the nginx worker user, then set listen.owner, listen.group and listen.mode in the FPM pool, or change the nginx user, and reload both.

  • Applies to nginx 1.24, PHP 8.3 FPM, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the socket's owner, group and mode, then the user nginx workers run as. If the worker user is neither the owner nor in the group, you have found it.

    ls -l /run/php/
    ps -o user= -C nginx | sort -u
    grep -n "^user" /etc/nginx/nginx.conf
  2. 2Match what you see, then jump to its fix

    1. 1(13: Permission denied) and ls -l shows an owner and group that differ from the nginx worker userThe socket belongs to a user or group nginx does not run asFix
    2. 2The pool file has listen.owner and listen.group, yet the socket still refuses nginxlisten.acl_users is set, so listen.owner and listen.group are ignoredFix
    3. 3The pool is set for www-data, but ps shows workers as nobody or another accountnginx workers run as a different user than you thinkFix

What does the error look like?

  • connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied) while connecting to upstream
  • 502 Bad Gateway

Cause 1 of 3

The socket belongs to a user or group nginx does not run as

1Check

Without listen.owner, listen.group and listen.mode, the socket gets the FPM pool's running user and group with mode 0660. On Linux the connecting user needs read and write on it.

ls -l /run/php/php8.3-fpm.sock
grep -n -E "^(user|group|listen)" /etc/php/8.3/fpm/pool.d/www.conf

2Fix

Give the socket to the user and group nginx uses, with read and write for both. Then restart FPM so it creates the socket again.

; /etc/php/8.3/fpm/pool.d/www.conf
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

sudo systemctl restart php8.3-fpm

3Verify

The socket now shows the new owner and group, and a request through nginx works.

ls -l /run/php/php8.3-fpm.sock
curl -I http://localhost/index.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

listen.acl_users is set, so listen.owner and listen.group are ignored

1Check

When listen.acl_users or listen.acl_groups is set, FPM ignores listen.owner and listen.group and uses the POSIX access control list instead. A user missing from the list is refused.

grep -n "listen.acl" /etc/php/8.3/fpm/pool.d/*.conf
getfacl /run/php/php8.3-fpm.sock

2Fix

Either add the nginx user to listen.acl_users (comma separated list), or remove the acl lines and use owner, group and mode.

; /etc/php/8.3/fpm/pool.d/www.conf
listen.acl_users = www-data

sudo systemctl restart php8.3-fpm

3Verify

getfacl lists the nginx user with rw on the socket.

getfacl /run/php/php8.3-fpm.sock

Confirmed by php.net

Did this fix it?

Cause 3 of 3

nginx workers run as a different user than you think

1Check

With no user directive, nginx workers run as nobody with group nobody. If nginx.conf has no user line, that default applies.

ps -o user=,args= -C nginx
grep -n "^user" /etc/nginx/nginx.conf

2Fix

Set the user directive in the main context to the account the socket allows, test, and reload.

# /etc/nginx/nginx.conf, top level
user www-data;

sudo nginx -t && sudo systemctl reload nginx

3Verify

Workers now run as that user and the 502 is gone.

ps -o user= -C nginx | sort -u
curl -I http://localhost/index.php

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Write listen.owner, listen.group and listen.mode explicitly in every pool file instead of relying on defaults.
  • Keep the nginx user directive and the pool's socket group in one place in your server notes, so a change to one updates the other.
  • After editing a pool, restart FPM and check ls -l on the socket before testing the site.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Servers

nginx gives up waiting for the app and returns 504

upstream timed out (110: Connection timed out) while reading response header from upstream

nginx returns 504 when the app behind it takes the request but sends nothing back within proxy_read_timeout (60 seconds by default), or when the connection cannot be opened in time.

5 causes, each with a fix

Servers

Cookies from a PHP backend vanish behind an nginx proxy

Login works when you call the backend directly but fails through nginx

nginx forwards Set-Cookie headers from the backend unchanged, so lost cookies usually come from the cookie itself: a domain or path that does not match the public URL, or Secure and SameSite flags the browser rejects.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.