How to fix connect() to php5-fpm.sock failed (13: Permission denied) while connecting to upstream Nginx error

I encountered this problem after updating PHP to 5.5.12. I use Nginx with PHP5 FPM and after the updating PHP I was seeing 502 Gateway Error pages. Nginx’s error log file (/var/log/nginx/error.log) had the following in it:

It was clear that Nginx couldn’t access PHP FPM’s socket file due to insufficient permissions. But everything was working fine till I updated PHP so something had changed in version 5.5.12. The changelog for this version has the following information under the FPM section:

Notice the first column of the output srw-rw----, it means users/groups other than root do not have any permissions on this file.

We have two options now:

Explicitly set the “listen.mode” to 0666 which make it insecure, or

Change the owner and group of the socket file so that Nginx can read/write to it.

Option 2 is highly recommended, find out username used by the Nginx worker processes:

grep 'user' /etc/nginx/nginx.conf

The most common ones are either www-data or nginx. Edit PHP FPM pool configuration file:

/etc/php5/fpm/pool.d/www.conf

Find the following lines:

;listen.owner = www-data
;listen.group = www-data

Remove the semicolon “;” before these lines. It is highly unlikely that these lines aren’t present in which case you can add them WITHOUT a semicolon at the beginning.

If you have multiple pools with different listen.owners and listen.groups make sure the Nginx user (for example www-data) is a member of the listen.group secondary group. So if a FPM pool is owned by bob: