简体   繁体   English

在PHP流程中使用PHP-FPM

[英]Using PHP-FPM inside a PHP process

The setup is two separate PHP projects running on the same server using PHP-FPM. 该设置是使用PHP-FPM在同一服务器上运行的两个独立的PHP项目。 Currently they "speak" to each other over JSON by making HTTP requests with cURL. 当前,他们通过使用cURL发出HTTP请求,通过JSON彼此“讲话”。

The issue here is that cURL and web server overhead is a waste of time. 这里的问题是cURL和Web服务器的开销是浪费时间。 After all the other program is right there, in a folder just above the current one. 毕竟所有其他程序都在该文件夹中,位于当前文件夹上方。 So why go the long way around with cURL and HTTP? 那么,为什么要使用cURL和HTTP呢? The trick is I can't just include a file form the other project because autoloaders clash and make a big mess. 诀窍是,我不能仅在其他项目中包含文件,因为自动加载器会发生冲突并造成很大的混乱。 For that reason they need separate processes and not share too much. 因此,他们需要单独的流程并且不要共享太多。

What I've proposed to solve the issue is to create a Node.js server that listens to a socket that my PHP process can connect to and is able to make requests to PHP-FPM directly using the node-phpfpm module. 我提出的解决此问题的建议是创建一个Node.js服务器,该服务器侦听我的PHP进程可以连接的套接字,并能够使用node-phpfpm模块直接向PHP-FPM发出请求。 While this solves the issue, I'm asking myself why is that Node.js proxy needed? 虽然这可以解决问题,但我想问自己为什么需要Node.js代理?

There must be a way to make a new FPM request directly from PHP but I haven't found it. 必须有一种直接从PHP发出新的FPM请求的方法,但我没有找到它。

I know I could also use the CLI executable with exec() but that's not pretty at all. 我知道我也可以将CLI可执行文件与exec()一起使用,但这一点都不漂亮。 To be more specific, passing request data with exec() is nearly impossible. 更具体地说,用exec()传递请求数据几乎是不可能的。

You can make a request from PHP script to PHP-FPM instance directly through UNIX or TCP/IP socket using, for example, this library: https://github.com/ebernhardson/fastcgi 您可以使用以下库,例如,通过UNIX或TCP / IP套接字直接从PHP脚本向PHP-FPM实例发出请求: https : //github.com/ebernhardson/fastcgi

Example code, based on readme: 示例代码,基于自述文件:

<?php

use EBernhardson\FastCGI\Client as FastCGIClient;
use EBernhardson\FastCGI\CommunicationException;

$client = new FastCGIClient('/var/run/php5-fpm.sock');

try {
    $client->request([
        'REQUEST_METHOD'  => 'POST',
        'SCRIPT_FILENAME' => '/full/path/to/script.php',
    ], $postBody);
    $response = $client->response();
} catch (CommunicationException $e) {
    // Handle exception
}

There are other libraries you can consider: https://packagist.org/search/?q=fastcgi 您还可以考虑其他库: https : //packagist.org/search/?q=fastcgi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM