What the message looks like
2026/10/29 10:03:17 [crit] 1234#1234: *321 connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 203.0.113.5, server: example.com, request: "GET /index.php HTTP/2.0", upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock:"Common causes
- The nginx worker user (
userinnginx.conf) differs fromlisten.ownerandlisten.groupof the PHP-FPM pool. listen.modein the pool is too strict, so the group cannot write to the socket.- A separate pool for a site runs as its own user and its socket is owned by that user only.
- A parent directory of the socket cannot be entered by the nginx user.
- The socket path in
fastcgi_passpoints to a socket owned by another service.
Check
- Owner, group and mode of the socket
ls -l /run/php/*.sock - Which user does nginx run as?
grep -E '^\s*user' /etc/nginx/nginx.conf - Pool settings for the socket
grep -rEn '^(listen|listen\.owner|listen\.group|listen\.mode|user|group) *=' /etc/php/*/fpm/pool.d/ - Can every directory on the path be entered?
namei -l /run/php/php-fpm.sock
Fix
- In the pool file under
/etc/php/<version>/fpm/pool.d/setlisten.owner = www-data,listen.group = www-dataandlisten.mode = 0660, matching the nginx user. - Alternatively add the nginx user to the group that owns the socket.
- Make sure
fastcgi_passuses the exact socket path of this pool. - Restart the PHP-FPM service of your version (its name contains the version), then check the socket again with
ls -l. - Test nginx:
nginx -t && systemctl reload nginx.
Typical scenario
You use a script to set up a separate PHP-FPM pool for each customer site, running as user site1. The socket /run/php/site1.sock then belongs to site1:site1 with mode 0660, but nginx runs as www-data. Result: 502 and in the log "connect() … failed (13: Permission denied)". With ls -l /run/php/*.sock and ps -o user= -C nginx check whether owner and group of the socket fit the nginx user.
FAQ
Should I set the socket to 0666?
No. That lets every local user talk to PHP-FPM. Use the matching owner and group with mode 0660.
How is this different from Primary script unknown?
Here nginx cannot reach PHP-FPM at all. With Primary script unknown the connection works but PHP cannot find the script file.
Why is the permission wrong again after a restart although I changed it?
/run is a volatile file system, and PHP-FPM recreates the socket on every start with the values from the pool file. A manual chmod therefore lasts only until the next restart. listen.owner, listen.group and listen.mode in the pool fix it permanently.
Can I use TCP instead of a socket?
Yes, with listen = 127.0.0.1:9000 in the pool and fastcgi_pass 127.0.0.1:9000; in nginx. File permissions then no longer apply. In exchange any local user can reach the port, and the connection is slightly slower. Never bind to all addresses, only to the local one.
How do I test as the nginx user whether access is possible?
sudo -u www-data test -w /run/php/site1.sock && echo ok checks write access to the socket. If it fails, a parent directory can also be the cause: the user needs execute permission on every directory of the path, not only on the socket.