nginx and php-fpm socket owner

nginx and php-fpm socket owner

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

When an update to your system results in a sudden 502 Bad Gateway error, the culprit is often not the application code or the web server configuration itself, but the underlying socket permissions managed by PHP-FPM. Consider this specific scenario: Nginx logs a connection failure to /var/run/php-fcgi-vhostname-php-fcgi-0.sock with the message failed (13: Permission denied). While the error points directly to access rights, a common misdiagnosis is assuming that updating the main pool configuration file will automatically propagate those changes.

In many standard setups, you might adjust /etc/php-fpm.d/www.conf with settings like listen.owner = www-data, listen.group = www-data, and listen.mode = 0660. The logic seems sound: define the socket ownership explicitly. However, if your PHP-FPM instance is configured to run as a pool using a parent configuration file (such as /etc/php-fpm.conf), simply editing the included files may not take effect. This happens because PHP-FPM reads its final configuration based on the order of inclusion and specific directives within the active pool block.

The root cause in this case stems from how php-fpm.conf structures its pools. If you define a [vhostname-php-fcgi-0] section directly inside /etc/php-fpm.conf, that file takes precedence over any settings defined in files included via include. Even if your www.conf contains the correct directives, PHP-FPM ignores them for that specific pool because the configuration is already resolved at the time the daemon starts. Consequently, the socket is created with the default user and group of the master process or defaults to root:root if not explicitly overridden in the active context.

The immediate workaround suggested in the source material involves manually changing the owner of the socket after a restart using commands like chown www-data:www-data /var/run/php-fcgi-vhostname-php-fcgi-0.sock. While this resolves the permission issue temporarily, it is operationally inefficient and prone to error during maintenance windows. Every time you restart Nginx or PHP-FPM, the socket reverts to its default ownership, forcing you to repeat the manual fix.

To achieve a permanent solution without relying on post-restart scripts, the configuration must be moved into the active pool block within /etc/php-fpm.conf. The critical detail is ensuring that the listen.owner, listen.group, and listen.mode directives are placed inside the specific pool definition rather than in a separate included file.

For example, updating the [vhostname-php-fcgi-0] section in /etc/php-fpm.conf to include these parameters ensures they are read when that specific pool initializes:

[vhostname-php-fcgi-0]
user = www-data
group = www-data

listen = /var/run/php-fcgi-vhostname-php-fcgi-0.sock

pm = dynamic
pm.max_children = 5
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 5

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

Regarding the listen.mode, there is a trade-off to consider. Setting it to 0666 (as mentioned in the edit) allows any user on the system to connect, which solves permission issues but introduces a security risk by exposing the FastCGI socket globally. A more secure approach, as shown above, is 0660, which restricts access to the owning group. This requires that your Nginx worker process and PHP-FPM pool share the same group ID (or are both in a dedicated group), ensuring they can communicate without opening the door for other system users.

Once the configuration is corrected in /etc/php-fpm.conf, you must restart the PHP-FPM service to apply the changes:

sudo systemctl restart php-fpm
# or on CentOS 6 where systemd might not be present
sudo service php-fpm restart

After the restart, verify that the socket permissions have taken effect by running ls -l /var/run/php-fcgi-vhostname-php-fcgi-0.sock. You should see www-data as both the owner and group with the intended mode. With the configuration properly scoped to the pool definition, the Bad Gateway error will be resolved persistently across reboots and service restarts.

Our network