nginx cannot find the PHP-FPM socket file
connect() to unix:/var/run/php-fpm/php7.4-fpm.sock failed (2: No such file or directory) while connecting to upstreamShort 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.
Fix it fast
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/2Match what you see, then jump to its fix
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 upstream502 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.phpConfirmed by nginx.org
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.phpConfirmed by php.net
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:
- 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 cannot open the PHP-FPM socket: Permission denied
502 Bad Gateway
nginx runs as one user and PHP-FPM creates its socket for another.
3 causes, each with a fixServers
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 fix