8

Error log:

[Sat Nov 22 05:24:41 2014] [error] [client xx] (2)No such file or directory: FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed 
[Sat Nov 22 05:24:41 2014] [error] [client xx] FastCGI: incomplete headers (0 bytes) received from server "/usr/lib/cgi-bin/php5-fcgi"

Well, this file /usr/lib/cgi-bin/php5-fcgi indeed is missing, and there's actually nothing in cgi-bin folder. And when I restarts apache2 it seems to work all right:

[Sat Nov 22 04:46:29 2014] [notice] FastCGI: process manager initialized (pid 10747)
[Sat Nov 22 04:46:29 2014] [notice] Apache/2.2.22 (Ubuntu) mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_ssl/2.2.22 OpenSSL/1.0.1 configured -- resuming normal operations

/etc/apache2/conf.d/php-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
 </IfModule>

So what do I do now?

Rob
  • 335
  • 3
  • 15
Shane
  • 231
  • 2
  • 3
  • 8

4 Answers4

9

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.

Jakuje
  • 9,145
  • 2
  • 40
  • 44
Mick
  • 203
  • 2
  • 7
  • I've been debugging this basic issue for a client, and the issue was caused by the FastCgi option of "FastCgiWrapper" being set to on by default. Also, it's not required that the fpm pool instances run as the apache user - the whole point is that they can run as a different user. – Stephen Nov 29 '15 at 17:29
  • My problem was that I needed to set the owner and group for the socket on ubuntu 16.04 running php7.2-fpm. – Cyclonecode Aug 14 '19 at 18:32
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 :)

Azodium
  • 121
  • 2
0

I had to ensure the Apache reference to FastCGI in /etc/apache2/conf-available/httpd.conf:

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

matched the listening PHP FastCGI process configured in /etc/php5/fpm/pool.d/www.conf:

listen = /var/run/apache2/php-fpm.sock

Once I had them matched I checked that FastCGI was actually running on the correct UNIX socket as described by @Mick (as usual prefix with sudo if not running as root):

lsof -U | grep php

which showed it was still incorrect:

php5-fpm  19438       root    7u  unix 0xffff880002242b40      0t0 79631 socket
php5-fpm  19438       root    8u  unix 0xffff880002242780      0t0 79632 socket
php5-fpm  19438       root    9u  unix 0xffff8800022423c0      0t0 79633 /var/run/php-fpm.sock
php5-fpm  19440   www-data    0u  unix 0xffff8800022423c0      0t0 79633 /var/run/php-fpm.sock
php5-fpm  19441   www-data    0u  unix 0xffff8800022423c0      0t0 79633 /var/run/php-fpm.sock
php5-fpm  19442   www-data    0u  unix 0xffff8800022423c0      0t0 79633 /var/run/php-fpm.sock
php5-fpm  19443   www-data    0u  unix 0xffff8800022423c0      0t0 79633 /var/run/php-fpm.sock

So a restart of the FastCGI process was required to pick up the config changes:

service php5-fpm restart
SharpC
  • 233
  • 2
  • 4
0

It sounds like you've started your web server without php5-fpm running and listening to a socket address. If you want to specify "/usr/lib/cgi-bin/php5-fcgi" as the listen address (a channel for fpm to process connections coming into it), you'll need to make sure that is in your fpm config. "/etc/php5/fpm/pool.d/www.conf" is a common path for it.

Then in your apache configuration, ensure .php files are using fastcgi_pass to send requests onto fpm. It sounds like it is already set up to do this, except it's expecting a unix socket in the wrong location.

Peter
  • 1,450
  • 2
  • 15
  • 26
  • This is a very strange problem, I believe everything is setup correctly in config files, but it did not work... I just added /etc/apache2/conf.d/php-fpm.conf in my question, and would you please be a bit more specific on where next should I look into? – Shane Nov 22 '14 at 08:33
  • Ensure php5-fpm process is running. Have you looked in /usr/lib/cgi-bin/php5-fcgi yet? – Peter Nov 22 '14 at 08:35
  • `/usr/lib/cgi-bin/php5-fcgi` does not exist... But still I'm able to restart php5-fpm: root@demodemo:~# service php5-fpm restart * Restarting PHP5 FastCGI Process Manager php5-fpm [ OK ] – Shane Nov 22 '14 at 08:47
  • Oops. I meant the file I originally mentioned. /etc/php5/fpm/pool.d/www.conf – Peter Nov 22 '14 at 08:52
  • I changed them into: `FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization` and `listen = 127.0.0.1:9000`, now it works! Still don't know why `FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization` and `listen = /var/run/php5-fpm.sock` do not work... – Shane Nov 22 '14 at 09:12
  • Did you ever figure out why the /var/run/php5-fpm.sock do not work??? I am struggling with the same issue now myself... – Alex.Barylski Mar 11 '15 at 19:21
  • Probably because his pool config wasn't set to listen to a socket. He never said whether he had checked it. – Phil May 10 '15 at 16:40