PHP-FPM Failed to connect to FastCGI server

Solution 1:

In short

I would bet that you are not using Unix Sockets in your php-fpm configuration. This is because by default php-fpm is using TCP ports.

Closer look at your configuration

You've decided to use Unix Sockets:

FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization

Therefore, you need to make sure php-fpm is actually running on those unix sockets.

Case 1: You are really using Unix Sockets

$ sudo lsof -U | grep php
// will show you
php-fpm   17330          root    5u  unix 0xf4f64800      0t0 7045381 socket
php-fpm   17330          root    7u  unix 0xf545f080      0t0 7045382 socket
php-fpm   17330          root    8u  unix 0xf55f3a80      0t0 7045383 /var/run/php5-fpm.sock
php-fpm   17331        nobody    0u  unix 0xf55f3a80      0t0 7045383 /var/run/php5-fpm.sock
php-fpm   17331        nobody    0u  unix 0xf55f3a80      0t0 7045383 /var/run/php5-fpm.sock

$sudo lsof -i | grep php
// should show nothing

Case 2: You are not using Unix Sockets but TCP ports

$ sudo lsof -U | grep php
// will show you
php-fpm   17330          root    5u  unix 0xf4f64800      0t0 7045381 socket
php-fpm   17330          root    7u  unix 0xf545f080      0t0 7045382 socket

$sudo lsof -i | grep php
php-fpm   13387    root    7u  IPv4 202656      0t0  TCP localhost:cslistener (LISTEN)
php-fpm   13388  nobody    0u  IPv4 202656      0t0  TCP localhost:cslistener (LISTEN)
php-fpm   13389  nobody    0u  IPv4 202656      0t0  TCP localhost:cslistener (LISTEN)

It's very likely that you get this error message because you are indeed not using unix sockets with php-fpm. The reason is that, php-fpm is configured by default to use tcp ports.

Open php-fpm.conf, and change to unix sockets:

sudo nano /etc/php5/fpm/pool.d/www.conf

# We don't want to use TCP ports
#listen = 127.0.0.1:9000

# We want to use Unix Sockets
listen = /var/run/php5-fpm.sock

Now, you will run into permission issues, on the same file:

// modify user and group and use same user as apache (here 'nobody')
user = nobody
group = nobody

// Uncomment those lines and use same user as apache (here 'nobody')
listen.owner = nobody
listen.group = nobody
listen.mode = 0666

PHP-FPM configuration is now using Unix Sockets, and Apache will be able to connect to it.

Solution 2:

It is possible that your fastcgi process is not authorized to read your /usr/lib/cgi-bin/ directory because the user that running the fastcgi process (you) doesn't belong to the same group that the owner of your /usr/lib/cgi-bin/ directory.

So, make sure of this :

$ stat /usr/lib/cgi-bin/

response :

bla bla... Uid: ( 33/www-data) Gid: ( 33/www-data)

Note : www-data is the user of Apache

If not, do this :

$ sudo chown -R www-data:www-data /usr/lib/cgi-bin

now check with the previous stat command if the user is www-data. Then, you have to add yourself (the user of the fastcgi process) to the user group www-data so :

$ sudo gpasswd -a ${USER} www-data

Now open /etc/php5/fpm/pool.d/www.conf with your favorite editor.

$ sudo nano /etc/php5/fpm/pool.d/www.conf

And uncomment those lines :

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

Important : don't turn listen.mode into 666, it's not safe

sudo service apache2 restart
sudo service php5-fpm restart

And the show must go on :)