Last Updated: September 09, 2019
· rmitchellnet

Have PHP-FPM Listen On Unix Socket

Here is how you can switch PHP-FPM to listen on a unix socket rather than a TCP socket. For this example I'm also going to show how to make the switch in Nginx.

First, edit the file at /etc/php5/fpm/pool.d/www.conf and find this block:

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   ''    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen =
listen = /var/run/php5-fpm.sock

And make sure the line to listen on the unix socket is not commented out:

listen = /var/run/php5-fpm.sock

Let's make some changes in our Nginx configs to tell it to pass requests through the unix socket rather than TCP.

First let's create a file at /etc/nginx/conf.d/ named php5-fpm.conf:

upstream php5-fpm-sock {
    server unix:/var/run/php5-fpm.sock;

In our Nginx server config we'll also have to change where to pass PHP requests:

fastcgi_pass php5-fpm-sock;