2

So in short I'm trying to get a PHP script to listen for requests over unix sockets and send it a request from another PHP script. I have configured PHP-FPM as such:

[a]

; Unix user/group of processes
user = www
group = www

listen = /var/run/php-fpm-a.sock
;listen.backlog = -1

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

; Choose how the process manager will control the number of child processes.
pm = dynamic
pm.max_children = 75
pm.start_servers = 3
pm.min_spare_servers = 1
pm.max_spare_servers = 5
pm.max_requests = 500

; host-specific php ini settings here
php_admin_value[open_basedir] = /usr/local/www/a
php_flag[display_errors] = on
/usr/local/www/a contains the following index.php:

<?php
echo 'test\ntest\ntest\n';

There is another PHP-FPM config file that effectively listens at /var/run/php-fpm-b.sock and Nginx points to it (this bit works fine), this contains the following code in /usr/local/www/b/index.php:

echo 'TEST B';
$fp = fsockopen('unix:///var/run/php-fpm-a.sock', -1, $errno, $errstr, 30);
if (!$fp) {
  echo "$errstr ($errno)<br />\n";
} else {
  $out = "GET /index.php HTTP/1.1\r\n";
  $out .= "Host: localhost\r\n";
  $out .= "Connection: Close\r\n\r\n";
  fwrite($fp, $out);
  while (!feof($fp)) {
    echo fgets($fp, 128);
  }

  fclose($fp);
}

Clearly I have something wrong in /usr/local/www/b/index.php as all I get is "TEST B" as the output. I don't think it's a socket permission issue as it would state so with an error, my guess is $out is wrong for this to work but have no idea what PHP excepts to receive. Any help would be appreciated.

Note: using PHP7 on FreeBSD11

FireLeopard
  • 314
  • 4
  • 18
  • Nginx uses `FastCGI` protocol to communicate with PHP-FPM process. Why you're trying to do it via `HTTP`? – Ivan Velichko Nov 22 '16 at 13:47
  • Ignore Nginx, that part works fine, I basically want one PHP script to connect to another with unix sockets as if one is a client and the other a server without going via Nginx if possible. – FireLeopard Nov 22 '16 at 13:52
  • I mean, that you have to use `FastCGI` proto as well while communicating with the second script. – Ivan Velichko Nov 22 '16 at 14:27
  • @IvanVelichko told you everything - two different FPM's can speak only via FCGI, but you're using HTTP as the protocol - that **can't** work. Also, this looks like XY problem. Why do you need two FPM's communicating with each other without anything in the middle? Are you trying to ping FPM's to see if they're alive or? – Mjh Nov 22 '16 at 16:41
  • @Mjh There will actually be things in the middle, it was just to get the bare minimum working. – FireLeopard Nov 22 '16 at 16:52
  • Convert the messages to FastCGI protocol (it's not actually hard but it's tedious) or have nginx as endpoint between two fpm's which will act as translator from one protocol to another. – Mjh Nov 22 '16 at 16:57

1 Answers1

2

PHP-FPM is a FastCGI Process Manager. FastCGI and HTTP are two different protocols. So, PHP-FPM can't speak HTTP directly.

browser -> (HTTP) -> nginx -> (FastCGI) -> PHP-FPM + scriptB
scriptB -> (HTTP) -> PHP-FPM + scriptA

You have two options:

  • Put script A behind nginx and then open TCP socket instead of unix socket to communicate from B to A.
  • Modify script B to speak in FastCGI language with script A instead of HTTP.
Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
  • Thanks, I've opted for the first option, which thinking about it is far better than trying to over-engineer it. I did know FastCGI and HTTP were different but I guess I was just hoping PHP-FPM could understand both. – FireLeopard Nov 22 '16 at 16:57
  • You're welcome. However, if this problem wasn't an educational one, I suggest you to use something like `cURL` or even `file_get_contents()` instead of manually open sockets. – Ivan Velichko Nov 22 '16 at 18:03
  • @FireLeopard the problem is that `php-fpm` is not an `http` server, therefore it doesn't have to understand `http`. Having it speak `fastcgi` only lets you place one or multiple `php-fpm` servers behind **any** web server, be it `nginx`, `apache`, mix of both, or something completely different. HTTP servers can adjust to the protocol `http 1.1` or `http 2.0` without having to upgrade `php-fpm` servers. There are reasons for all of this, there are ways to translate http to fcgi, but as you said - it's the best not to overengineer it. – Mjh Nov 23 '16 at 14:40