Connect to php-fpm.sock failed (Permissions denied)

How to fix the Connect to php-fpm.sock failed (Permissions denied) error on Nginx web server.

The error will appear in Nginx error logs as:

*1 connect() to unix:/var/run/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: x.x.x.x, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm.sock:", host: "x.x.x.x"

Fix Connect to php-fpm sock failed

Check the username who runs Nginx:

ps aux | grep nginx

Edit the PHP-FPM configuration file. On Ubuntu systems, the path is:

vi /etc/php5/fpm/php.ini # PHP 5
vi /etc/php/7.0/fpm/php.ini # PHP 7

Set the username and group that will run Nginx and PHP-FPM:

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

Replace www-data with the username that runs Nginx.

If the socket file ( php-fpm.sock) is already created, change the ownership of the file:

chown www-data:www-data /var/run/php-fpm.sock

If you use the php-fpm.sock file, make sure that the cgi.fix_pathinfo parameter is set to 0 in php.ini:

vi /etc/php5/php.ini

Edit the cgi.fix_pathinfo to be uncommented and set to 0:

cgi.fix_pathinfo = 0

Reload PHP-FPM

sudo systemctl php-fpm restart

Restart Nginx:

sudo systemctl restart nginx