Maaz Zindani

Loading the index…

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

nginx cannot find the PHP-FPM socket file

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

Short answer

nginx and the PHP-FPM pool must name the same address. The pool's listen line decides where FPM creates its socket, and fastcgi_pass tells nginx where to connect. When the two differ, for example after a PHP upgrade changed the socket name, the file nginx asks for does not exist. Make both lines match.

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

Fix it fast

  1. 1Find out which cause you have

    Put the two sides next to each other: the socket nginx wants, and the address the pool really listens on.

    grep -R "fastcgi_pass" /etc/nginx/
    grep -R "^listen" /etc/php/*/fpm/pool.d/
  2. 2Match what you see, then jump to its fix

    1. 1The path in the error is not the path in the pool's listen linefastcgi_pass names a socket path the pool does not createFix
    2. 2ls shows no .sock file at all, and the listen line holds an address like 127.0.0.1:9000The pool listens on TCP, nginx connects to a socketFix

What does the error look like?

  • connect() to unix:/var/run/php-fpm/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstream
  • 502 Bad Gateway

Cause 1 of 2

fastcgi_pass names a socket path the pool does not create

1Check

The listen directive is the address on which FPM accepts requests; a Unix socket is written as a path. Compare it with the path after unix: in fastcgi_pass. A versioned name such as php7.4-fpm.sock left in an old site file is the usual mismatch.

grep -R "fastcgi_pass" /etc/nginx/sites-enabled/
grep -R "^listen " /etc/php/*/fpm/pool.d/
ls -l /run/php/

2Fix

Copy the path from the pool's listen line into fastcgi_pass, character for character.

# nginx site config
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}

3Verify

Test the nginx config, reload it and request a PHP page.

sudo nginx -t && sudo systemctl reload nginx
curl -I http://localhost/index.php

Confirmed by nginx.org

Did this fix it?

Cause 2 of 2

The pool listens on TCP, nginx connects to a socket

1Check

The listen directive accepts ip:port, a bare port or a socket path. If it holds a host and port, FPM never creates a socket file.

grep -R "^listen" /etc/php/*/fpm/pool.d/

2Fix

Either point nginx at the TCP address, or change the pool to a socket path. The first needs one line in nginx and nothing else.

# nginx site config
fastcgi_pass 127.0.0.1:9000;

3Verify

Reload nginx and request a PHP page.

sudo nginx -t && sudo systemctl reload nginx
curl -I http://localhost/index.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • After a PHP version change, grep the nginx sites for the old socket name.
  • Keep the socket path in one place, such as a snippet file that every site includes.
  • Use a path without a version number if you plan to upgrade PHP often, and set it in both files.

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.