Ffile2fix
Sign in Get started

How to fix Nginx "php-fpm.sock failed (2: No such file or directory)"

connect() to unix:/run/php/php-fpm.sock failed (2: No such file or directory) while connecting to upstream

Nginx is configured to pass PHP requests to a Unix socket file, but that file does not exist, so visitors get a 502 Bad Gateway. Either PHP-FPM is not running or it creates its socket under a different name or path.

Also appears as: connect() to unix:/var/run/php/php8.1-fpm.sock failed (2: No such file or directory) while connecting to upstream, upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:" · connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied) while connecting to upstream · connect() to unix:/run/php-fpm/www.sock failed (2: No such file or directory) · 502 Bad Gateway (with the socket error in /var/log/nginx/error.log)

Common causes

  • PHP-FPM is stopped or failed to start (check its own config errors)
  • PHP was upgraded (for example 8.1 to 8.3) and the socket name changed, but fastcgi_pass still points to the old one
  • Nginx uses a generic name like php-fpm.sock while the pool listens on php8.3-fpm.sock, or vice versa
  • On RHEL/Alma/Rocky the pool listens on /run/php-fpm/www.sock or 127.0.0.1:9000, not /run/php/
  • The pool listens on TCP 127.0.0.1:9000 while Nginx expects a socket
  • A related variant (13: Permission denied) means the socket exists but listen.owner/listen.group do not allow the Nginx user

How to fix it

  1. Check PHP-FPM is running. Run systemctl status php8.3-fpm (or php-fpm on RHEL). If it failed, run sudo php-fpm8.3 -t to find config errors, then start it.
  2. Find the real socket path. Run grep -R "^listen" /etc/php/*/fpm/pool.d/ (or /etc/php-fpm.d/) and ls -l /run/php/ to see which sockets actually exist.
  3. Update fastcgi_pass. Edit the server block so fastcgi_pass matches the listen value exactly, for example unix:/run/php/php8.3-fpm.sock. Search all files with grep -R fastcgi_pass /etc/nginx/.
  4. Fix socket permissions if you see error 13. In the pool config set listen.owner and listen.group to the Nginx user (www-data on Debian/Ubuntu, nginx on RHEL) and listen.mode = 0660, then restart PHP-FPM.
  5. Test and reload. Run sudo nginx -t && sudo systemctl reload nginx, then load a PHP page and check the error log is clear.

Nginx + PHP-FPM pool

# /etc/nginx/sites-available/example.com
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}

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

How to stop it happening again

  • Update fastcgi_pass in the same step as any PHP version upgrade
  • Enable PHP-FPM with systemctl enable so it starts on boot
  • Keep the socket path in one shared snippet included by all sites

Frequently asked questions

Should I use a Unix socket or 127.0.0.1:9000?

Both work. Sockets are slightly faster on the same machine; TCP is needed when PHP-FPM runs in a separate container or server. Nginx and the pool just have to agree.

Why did this start after a reboot?

/run is cleared on every boot, and the socket is recreated only when PHP-FPM starts. If the service is not enabled, the socket never appears.

Is changing the socket to 0666 a good fix for permission errors?

No. It lets any local user talk to PHP-FPM. Set listen.owner and listen.group to the web server user with mode 0660 instead.