preload preload

Get High Performance PHP-FPM with socket connections

In my earlier post (The Perfect LAMP Stack), I have explained about how to install the PHP-FPM and configure Apache + FastCGI to connect to PHP over TCP/IP connection at port 9000. PHP-FPM not only supports TCP/IP connections but also the socket based connections.

The advantage of running PHP-FPM on socket connections instead of TCP/IP is that the socket connections are much more faster than TCP/IP connections (around 10-15%) because it saves the passing the data over the different layers of TCP/IP stack.

Therefore, it is recommended to run the PHP-FPM on socket connections over TCP/IP when you are using the same server for Apache and PHP-FPM. If you are using the different servers for Apache and PHP-FPM then the socket connections for PHP-FPM will not work.

Now, to enable socket connections for your LAMP setup, following are steps involved:

1. Open you php-fpm.conf file and look for

<value name="listen_address"></value>

Change the above line to the socket file path where you want to store the PHP-FPM socket file. In my case, I am storing it in Apache log folder

<value name="listen_address">/usr/local/apache2/logs/php-fpm.sock</value>

After the above changes, restart your PHP-FPM server with following command:

php-fpm restart

Now you will be having a socket file created in the above mentioned path.

2. The next setting is to change the Apache FastCgiExternalServer configuration in VirtualHost, to let Apache know about sending the PHP requests to socket connections. Open your http-vhosts.conf file and change the below settings from

FastCGIExternalServer /usr/local/apache2/fcgi-bin/php-cgi -host –pass-header Authorization


FastCGIExternalServer /usr/local/apache2/fcgi-bin/php-cgi -socket /usr/local/apache2/logs/php-fpm.sock –pass-header Authorization

After the above configuration changes, restart your Apache HTTPD server and voila, you have enabled the socket connections for PHP-FPM. Enjoy.

Liked the content? Then why not share with your pals

  • 5 responses to "Get High Performance PHP-FPM with socket connections"

  • badmash
    15:57 on October 23rd, 2010

    I just signed up to your blogs rss feed. Will you post more on this subject?

  • Deepesh
    10:28 on October 24th, 2010

    @badmash, sure I will

  • Dimas
    11:58 on March 6th, 2011

    Very straight forward to set it up, thanks for the post.

  • Dave
    0:14 on October 29th, 2011

    If you want to have multiple PHP users within one web hiearchy use the following for a guideline:

    AddHandler php-fastcgi .php .php5

    FastCGIExternalServer /var/vpath/base_www -socket /var/run/php-sockets/base_www.sock -pass-header Authorization

    Action php-fastcgi /base_www

    Alias /base_www /var/vpath/base_www

    # Some subdir you want to run php stuff as a different user
    FastCGIExternalServer /var/vpath/www_otheruser -socket /var/run/php-sockets/www_otheruser.sock -pass-header Authorization

    Action php-fastcgi /www_otheruser

    Alias /www_otheruser /var/vpath/www_otheruser

    /var/vpath MUST EXIST. the “www” and “www_otheruser” files references in the Alias and FastCGIExternalServer lines DO NOT need to, they are virtual paths.

    php-fpm should be configured in this case in Unix domain socket mode with the sockets in /var/run/php-sockets

    For my test, I had /var/www’s PHP pool configured to run as “userA”, and /var/www/some_subdir to run under a DIFFERENT pool as user B.

    Thus I could have separate PHP .ini configs per user, and if user B had a bad script it could only potentially damage files in his dir, but non of the others (assuming you setup permissions correctly).
    . This also allows the pools to be configured as needed (i.e. leaned/tighten approrpriately, and each vhost could have multiple pools.

    The basics are:
    The AddHandler says, all files with .php or .php5’s should be handled by the “php-fastcgi” handler (this could be named anything actually)

    The FastCGIExternalServer lines define a VIRTUAL path which refers to a socket to send the request to.

    The blocks are used to assign the ACTION associated with the Handler to an aliased path

    Action php-fastcgi /base_www

    Which takes us to the most important piece that ties it together
    Alias /base_www /var/vpath/base_www

    This means any files with the .php/.php5 extension under the directory listed in the Directory Block are passed off the the FastCGIExternalServer associated with the alias referenced within that block.

    This can’t be done with blocks as apache only allows the “Action” directive in the core config, vhost or directory contexts.

    Drop a test.php with a phpinfo() call in it in the the appropriate place and you can see that your PHP executes under the appropriate user.

    I’m sure this can probably be done slightly differently, but the end result is similar
    No need to use ugly suEXEC stuff either.

  • Dylan
    2:05 on April 26th, 2012

    Was having the same problems with nginx. I made the change to php-fpm and in the nginx config I changed the pass through address to:

    fastcgi_pass unix:/tmp/php-fpm.socket;

  • Trackbacks

  • Trackback fromCómo instalar LEMP en Ubuntu — Linux nginx MySQL PHP | @lobo_tuerto
    Friday, 5 October, 2012

    [...] Comunicación por medio de un socket UNIX Normalmente, PHP-FPM escucha en el puerto 9000 en Dado que php-fpm y nginx están en la misma máquina prefiero que se comuniquen a través de un socket UNIX en vez de usar TCP. Get High Performance PHP-FPM with socket connections [...]

  • Leave a Reply

    * Required
    ** Your Email is never shared