PHP-FPM occasional “FastCGI: failed to connect to server” error

apache-2.4PHPphp-fpmubuntu-14.04

Ubuntu 14.04.3 LTS
Apache 2.4.7
PHP 5.5.9

I switched from mod_php to PHP-FPM about two weeks ago. Everything, for the most part is running smoothly. Except twice now I've had a situation where apache/php would become unresponsive. A restart would fix the issue, however I'd like to know why this is happening. Here are the error logs, its filled with hundreds of the similar type of error.

# /var/log/apache2/error.log
.
.
[Wed Dec 16 23:19:21.476641 2015] [fastcgi:error] [pid 32523] (104)Connection reset by peer: [client xx.xx.xx.xx:43676] FastCGI: comm with server "/usr/lib/cgi-bin/php5-fcgi" aborted: read failed
[Wed Dec 16 23:19:21.476866 2015] [fastcgi:error] [pid 32411] (2)No such file or directory: [client xx.xx.xx.xx:63082] FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed
[Wed Dec 16 23:19:21.477489 2015] [fastcgi:error] [pid 32527] (104)Connection reset by peer: [client xx.xx.xx.xx:49675] FastCGI: comm with server "/usr/lib/cgi-bin/php5-fcgi" aborted: read failed
[Wed Dec 16 23:19:21.478270 2015] [fastcgi:error] [pid 32548] (2)No such file or directory: [client xx.xx.xx.xx:59140] FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed
.
.

Apache config

# /etc/apache2/conf-available/php5-fpm.conf
<IfModule mod_fastcgi.c>
    AddHandler php5-fcgi .php
    Action php5-fcgi /php5-fcgi
    Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization

    <Directory /usr/lib/cgi-bin>
        Require all granted
    </Directory> 
</IfModule> 

PHP5-FPM Config ( included what I thought may be helpful )

# /etc/php5/fpm/pool.d/www.conf
listen = /var/run/php5-fpm.sock
.
.
user = www-data
group = www-data
.
.
pm.max_children = 10
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 1
pm.max_requests = 500
.
.
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
.
.

Connecting to sockets

# lsof -U | grep php
php5-fpm  14373   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock
php5-fpm  17084   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock
php5-fpm  18544   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock
php5-fpm  18544   www-data    4u  unix 0xffff8800da1e7700      0t0 701371 /var/run/php5-fpm.sock
php5-fpm  18649   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock
php5-fpm  19672   www-data    0u  unix 0xffff8801ff42bb80      0t0   1721 /var/run/php5-fpm.sock

Please let me know if there is any other information needed to help figure this out. As I've stated this has only happened twice now. Thanks

Best Answer

Don't use a socket for this purpose. They block too much to support even a medium-load site with lots of requests coming in. Use a TCP socket instead to connect httpd to php-fpm.

Refer to this for instructions: https://wiki.apache.org/httpd/PHP-FPM (look at the section entitled "TCP socket (IP and port) approach").

You also don't have very many workers set up. I usually recommend around two per available CPU core (assuming that most web requests will take 500ms). You can do your own math based on your average response time. If you need more workers to support your load, get more servers.