Nginx – PHP7.0-FPM and Nginx don’t work using unix sockets

nginxphp7socket

I have a trouble that is not described in the web. I am using VPS Debian 8 Nginx + PHP7.0-FPM. My server gives php-files in a source code and does not compile them when I configure Nginx and PHP7.0-FPM to use unix sockets (I do it cause I've read it works faster then tcp connections).

So what I really do and it does not work:

  1. edit /etc/nginx/fastcgi_params to comment fastcgi_pass parameter to use unix socket file:
#fastcgi_pass    127.0.0.1:9000;

all my hosts include this file, so I need to configure own socket in each host file. I do it like this:

  1. edit /etc/nginx/sites-available/example.com I type fastcgi_pass parameter there
location ~ '\.php$|^/update.php' {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
fastcgi_pass      unix:/var/run/php/example.com.php7.0-fpm.conf;
        include             /etc/nginx/fastcgi_params;

        fastcgi_param       SCRIPT_FILENAME     /var/www/example.com/www$fastcgi_script_name;
        fastcgi_param       DOCUMENT_ROOT       /var/www/example.com/www;

        fastcgi_param       PHP_ADMIN_VALUE     upload_tmp_dir=/var/www/example.com/tmp/upload;
        fastcgi_param       PHP_ADMIN_VALUE     session.save_path=/var/www/example.com/tmp/sessions;
    }
  1. edit main php7.0-fpm pool file /etc/php/7.0/fpm/pool.d/www.conf to listen socket (but now I cannot understand why i need it here):

    listen = /var/run/php/php7.0-fpm.sock

  2. edit my site's pool file /etc/php/7.0/fpm/pool.d/example.com.conf to set up it own socket (so here it is necessary I think):

    listen = /var/run/php/example.com.php7.0-fpm.sock

  3. restart services:

    service nginx restart && service php7.0-fpm restart

so this get my index.php downloading as it is when I visit any site page.

But if I use tcp socket fastcgi_pass 127.0.0.1:9000; it works fine.

Best Answer

It looks like you have the wrong filename for fastcgi_pass it should be:

fastcgi_pass      unix:/var/run/php/example.com.php7.0-fpm.sock;

Make sure that php-fpm is running and that the socket file exists and has the correct permissions for nginx to access the file. If you have selinux installed and enforcing, you might also need to check its logs to see if it is blocking nginx from accessing the file.

On php-fpm's side, each of those configuration files in pool.d is a separate pool of PHP executables and each needs its own socket. www.conf is not a "main" configuration file, it's a completely separate pool of processes and should be disabled/removed if you are not using it. With both pools configured to use the same socket, there is certainly a conflict here.