nginx cannot open the PHP-FPM socket: Permission denied
connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied) while connecting to upstreamShort 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.
Fix it fast
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.conf2Match what you see, then jump to its fix
- 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
The 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
The pool is set for www-data, but ps shows workers as nobody or another accountnginx workers run as a different user than you thinkFix
- 1
What does the error look like?
connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied) while connecting to upstream502 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.conf2Fix
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-fpm3Verify
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.phpConfirmed by php.net
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.sock2Fix
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-fpm3Verify
getfacl lists the nginx user with rw on the socket.
getfacl /run/php/php8.3-fpm.sockConfirmed by php.net
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.conf2Fix
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 nginx3Verify
Workers now run as that user and the 502 is gone.
ps -o user= -C nginx | sort -u
curl -I http://localhost/index.phpConfirmed by nginx.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
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 fixServers
nginx and PHP-FPM show File not found or Primary script unknown
File not found.
PHP-FPM received a script path it could not turn into a file.
4 causes, each with a fixServers
nginx still returns 504 although the PHP script has a long time limit
upstream timed out (110: Connection timed out) while reading response header from upstream
set_time_limit() only controls PHP.
4 causes, each with a fixServers
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 fixServers
display_errors has no effect on PHP behind nginx and PHP-FPM
500 Internal Server Error with no PHP message in the browser
With nginx there is no web-server PHP setting.
4 causes, each with a fixServers
nginx cannot find the PHP-FPM socket file
502 Bad Gateway
nginx and the PHP-FPM pool must name the same address.
2 causes, each with a fix