connect() to unix:/tmp/php-fpm.sock failed (2: No such file or directory)
When debugging a "No such file or directory" error for a Unix socket, the immediate instinct is often to verify that the socket file actually exists. In many cases, you will find the file sitting there, exactly as shown in your ll /tmp/php-fpm.sock output with correct permissions (srw-rw----). However, the presence of the file on disk does not guarantee that Nginx can establish a connection to it. The root cause of this specific failure is almost always a permission mismatch regarding the UID and GID of the socket versus the user account running the Nginx worker processes.
While PHP-FPM and Nginx are both configured to run as the www user in your environment, Linux socket permissions can be tricky. The socket file is created by the master process of PHP-FPM before the workers spawn. If the master process creates the socket with restrictive ownership (e.g., owned by root temporarily or if the umask isn't set correctly in php-fpm.conf), the subsequent worker processes might not be able to bind to it, or Nginx workers might be rejected upon connection attempts despite the file appearing accessible.
In your specific scenario, you are running PHP-FPM as a systemd service (php-fpm.service) and Nginx similarly via nginx.service. Even though both services are marked as active and running, the effective user context in which they operate can differ from what is explicitly defined in the configuration files if the unit files override them.
Check the actual process owners using ps to verify who is running the daemons:
ps aux | grep php-fpm
ps aux | grep nginx
If you see that PHP-FPM workers are running as www but Nginx workers are running as a different user (often nobody or nginx, depending on how the systemd unit is templated), the socket will be readable by the group but not executable (traversed) by the Nginx worker.
To resolve this, ensure that both the PHP-FPM configuration and the Nginx configuration explicitly define the user and group for their respective processes. In your www.conf, you have:
user = www
group = www
listen = /tmp/php-fpm.sock
listen.owner = www
listen.group = www
This tells PHP-FPM to create the socket owned by www:www. However, Nginx must also be running as www to access that socket without hitting a permission denial. If your Nginx systemd unit defaults to a different user, you need to adjust it:
# /etc/systemd/system/nginx.service
[Service]
User=www
Group=www
After making these changes, reload both services to apply the new user contexts without dropping connections:
systemctl daemon-reload
systemctl restart php-fpm
systemctl restart nginx
It is also worth noting that using a socket (unix:/tmp/php-fpm.sock) is generally preferred over TCP sockets (127.0.0.1:9000) in production environments because sockets are faster and allow for better process isolation. The performance trade-off between TCP and Unix sockets is usually negligible on a single server, but the security model of sockets allows you to restrict access strictly by file permissions rather than relying solely on network firewall rules or port binding restrictions.
If you encounter similar issues in the future with Laravel applications or other frameworks using PHP-FPM, remember that the "file exists" check is only half the battle. Always verify the UID/GID of the listening socket against the running processes of both the upstream (PHP-FPM) and the client (Nginx). For more robust deployment strategies involving these components, resources like https://laravelcompany.com often provide detailed guides on securing PHP environments with Nginx.
Finally, ensure that /tmp is mounted with appropriate options in your filesystem configuration if you are managing the server manually. By default, /tmp is mounted with noexec, which can sometimes interfere with socket operations depending on the kernel version and specific use case, though this is less common for simple socket communication.