How to make PHP-FPM to use a Unix Socket

By default PHP-FPM is listening on port 9000 on 127.0.0.1. It is also possible to make PHP-FPM use a Unix socket which avoids the TCP overhead. To do this, open /etc/php-fpm.d/www.conf…

vi /etc/php-fpm.d/www.conf

and make the listen line look as follows:

[...]
;listen = 127.0.0.1:9000
listen = /tmp/php5-fpm.sock
[...]

restart PHP-FPM  with /etc/init.d/php-fpm restart

Next go through your nginx configuration and all your vhosts and change the line fastcgi_pass 127.0.0.1:9000; to fastcgi_pass unix:/tmp/php5-fpm.sock;, e.g. like this:

[...]
        location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_pass   unix:/tmp/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
[...]

Finally restart nginx: /etc/init.d/nginx restart

1 thought on “How to make PHP-FPM to use a Unix Socket”

  1. I do this, but I have changed the root directory for each of my vhosts.
    root /var/www/host1
    root /var/www/host2

    Yet, nginx still serves all responses from the /usr/share/nginx/html

    Why might that be? I have grep’d all of /etc/nginx for “/usr/share/nginx/html” and can find it in no file (except for 50x and 404 pages). I have restarte php-fpm and nginx

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: