nginx error connect to php5-fpm.sock failed (13: Permission denied)
Updating your web stack is a routine maintenance task, but it often exposes latent configuration issues that were previously masked by older software versions. A classic example of this occurred after upgrading nginx to 1.4.7 and PHP to 5.5.12, which resulted in a 502 Bad Gateway error. The logs immediately point to the culprit: a connection failure to the Unix socket /var/run/php5-fpm.sock with the message "Permission denied" (error code 13).
This error indicates that the nginx master or worker process does not have the necessary read/write permissions to access the socket file created by the PHP-FPM daemon. While older versions of nginx might have been more lenient or ignored these permission discrepancies, version 1.4.x enforces stricter security checks on Unix domain sockets. If you were running everything fine before the update, the underlying binaries likely shared a user context that is no longer valid after the upgrade.
Diagnosing the User Context Mismatch
The root cause almost always lies in a mismatch between the user running nginx and the user running php-fpm. In your configuration file, you explicitly defined:
user www www;
This directive tells nginx to spawn its worker processes as the www user (often mapped to www-data or nginx depending on the distro) and group. However, PHP-FPM creates the socket file owned by the user defined in its own configuration (php-fpm.conf). If that file is owned by a different user, or if the group permissions don't align with the www group, the connection will be rejected immediately.
Verifying File Ownership and Permissions
To resolve this, you must ensure the socket file belongs to the same user and group as the nginx workers. First, check the current ownership of the socket:
ls -l /var/run/php5-fpm.sock
You will likely see output similar to srwxrwxr-x 1 root root .... Even though the group is root, the critical factor here is that the owner is root. If your nginx workers are running as www, they cannot write to a socket owned by root unless specific group permissions allow it, and even then, the setup is brittle.
The standard fix involves adjusting the ownership of the socket file to match your web server user. Execute the following command:
sudo chown www:www /var/run/php5-fpm.sock
Note: Replace www with your actual system username (e.g., www-data, nginx, or apache) if www is not defined on your specific Linux distribution.
After changing the ownership, restart both services to ensure they pick up the new socket state:
sudo service nginx restart
sudo service php5-fpm restart
Configuration Hardening and Best Practices
While fixing the ownership solves the immediate crash, it is worth considering how this interacts with directory permissions. Your nginx.conf correctly sets SCRIPT_FILENAME to /usr/home/user/public_html. Ensure that this directory itself is accessible by the www user:
ls -l /usr/home/user/public_html
If the parent directories restrict access, nginx might fail to serve static files even if the PHP socket connection works. Always verify that the public_html directory has permissions like 755 or 775 (depending on your security policy) and is owned by the web server user.
Modern Alternatives for Large Applications
If you are managing a production environment with complex application requirements, relying solely on Unix sockets can sometimes be limiting, particularly when scaling across multiple servers or dealing with specific file path resolution issues. While sockets are generally faster over the local loopback interface because they avoid network overhead, their strict permission model requires careful orchestration during upgrades.
For high-scale deployments where process isolation is critical, many teams eventually migrate to TCP-based FastCGI connections (using 127.0.0.1:9000 instead of the socket) or containerized solutions like Docker, which handle networking and user contexts more gracefully. For modern PHP applications built on frameworks like Laravel, using a dedicated deployment tool or container orchestration often abstracts away these socket permission headaches entirely.
If you are maintaining legacy systems where upgrading to newer nginx versions or PHP major releases is difficult, understanding this permission model is essential. However, for new projects, utilizing managed hosting or containerized stacks ensures that the "permission denied" errors remain a thing of the past rather than a recurring maintenance task.