phpfastcgi/fastcgi-daemon

用 PHP 编写的 FastCGI 守护进程

v0.11.0 2018-08-14 08:56 UTC

README

Latest Stable Version Build Status Coverage Status Scrutinizer Code Quality Total Downloads

用 PHP 编写的 FastCGI 守护进程。访问 项目网站 获取更多文档和指南。

查看 项目性能基准,了解我们如何将 "Hello, World!" Slim 应用程序处理能力提升到 5,500 rq/s。

简介

使用此守护进程,应用程序可以在 HTTP 请求之间保持活动状态,同时位于启用 FastCGI 的 Web 服务器保护之下。

守护进程需要一个处理程序来定义,该处理程序接受请求对象并返回 PSR-7 或 HttpFoundation 响应。

Speedfony Bundle 将此守护进程与 Symfony2 框架集成。Slim Adapter 将此守护进程与 Slim v3 框架集成。Silex Adapter 将此守护进程与 Silex 框架集成。

还有一个非官方的 ZF2 Adapter,它将此守护进程与 Zend Framework 2 集成。

当前状态

此项目目前处于开发的早期阶段,尚不稳定。

欢迎贡献和建议。

使用方法

以下是一个简单的 "Hello, World!" FastCGI 应用程序的示例。有三个示例。一个是使用 PHPFastCGI 请求,一个是使用 Symfony HTTP Foundation 请求,还有一个是使用 PSR-7 请求。

PHPFastCGI 请求

使用纯 PHPFastCGI 是最快的方法,因为它不涉及任何请求转换。

我们需要 PSR-7 实现的响应对象。

composer require composer require zendframework/zend-diactoros
<?php // fastCGI_app.php

// Include the composer autoloader
require_once dirname(__FILE__) . '/../vendor/autoload.php';

use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
use Zend\Diactoros\Response\HtmlResponse;

// A simple kernel. This is the core of your application
$kernel = function (RequestInterface $request) {    
    return new HtmlResponse('<h1>Hello, World!</h1>');
};

// Create your Symfony console application using the factory
$application = (new ApplicationFactory)->createApplication($kernel);

// Run the Symfony console application
$application->run();

Symfony HTTP Foundation 请求

当您的应用程序正在利用 Symfony 生态系统时使用此方法。

composer require symfony/http-foundation
<?php // fastCGI_app.php

require_once dirname(__FILE__) . '/../vendor/autoload.php';

use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
use Symfony\Component\HttpFoundation\Response;

$kernel = function (RequestInterface $request) {
    $sfRequest = $request->getHttpFoundationRequest(); // returns HTTP Foundation request object
    
    return new Response('<h1>Hello, World!</h1>' . $sfRequest->getUri());
};

$application = (new ApplicationFactory)->createApplication($kernel);
$application->run();

PSR-7 请求

这里是用 PSR-7 HTTP 对象的相同示例。首先,您需要安装任何 PSR-17(HTTP 工厂)实现,然后是一个 PSR-17 工具库(nyholm/psr7-server)。

composer require http-interop/http-factory-diactoros nyholm/psr7-server
<?php // fastCGI_app.php

require_once dirname(__FILE__) . '/../vendor/autoload.php';

use Http\Factory\Diactoros;
use Nyholm\Psr7Server\ServerRequestCreator;
use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
use PHPFastCGI\FastCGIDaemon\Http\Request;
use PHPFastCGI\FastCGIDaemon\Http\RequestInterface;
use Zend\Diactoros\Response\HtmlResponse;

// Give the Request an instance of ServerRequestCreatorInterface filled with PSR-17 factories. 
// This is how we are independent of any PSR-7 implementation. 
Request::setServerRequestCreator(new ServerRequestCreator(
    new Diactoros\ServerRequestFactory,
    new Diactoros\UriFactory,
    new Diactoros\UploadedFileFactory,
    new Diactoros\StreamFactory
));

$kernel = function (RequestInterface $request) {
    $psr7Request = $request->getServerRequest(); // returns PSR-7 ServerRequestInterface
    
    return new HtmlResponse('<h1>Hello, World!</h1>' . $psr7Request->getRequestTarget());
};

$application = (new ApplicationFactory)->createApplication($kernel);
$application->run();

服务器配置

NGINX

对于 NGINX,您需要使用进程管理器(如 supervisord)来管理应用程序的实例。请参阅 AstroSplash 的示例配置。

以下是您将对 Symfony NGINX 配置 进行修改的示例。核心原则是将 PHP-FPM 引用替换为指向工作进程集群的引用。

# This shows the modifications that you would make to the Symfony NGINX configuration
# https://www.nginx.com/resources/wiki/start/topics/recipes/symfony/

upstream workers {
    server localhost:5000;
    server localhost:5001;
    server localhost:5002;
    server localhost:5003;
}

server {
    # ...

    location ~ ^/app\.php(/|$) {
        # ...
        fastcgi_pass workers;
        # ...
    }

    # ...
}

Apache 2.4.10+

如果您正在使用 Apache 2.4.10 或更高版本,您需要使用 mod_proxy_fcgi。您需要使用进程管理器(如 supervisord)来管理应用程序实例。对于开发,您只需启动应用程序(见 运行服务器),然后将其添加到您的虚拟主机配置中。

<FilesMatch ^index\.php$>
  SetHandler "proxy:fcgi://127.0.0.1:5000"
</FilesMatch>

访问 http://127.0.0.1/index.php 以测试您的设置。请注意,index.php 必须存在,但可以是空文件。

Apache 2.0 - 2.2

如果您希望将 FastCGI 应用程序配置为与 Apache 网络服务器一起使用,您可以使用 Apache FastCGI 模块来管理应用程序的进程。

这可以通过创建一个启动您应用程序的 FCGI 脚本并将 FastCgiServer 指令插入虚拟主机配置中来实现。

以下是一个示例 script.fcgi

#!/bin/bash

# Run the server
php /path/to/application.php run

有关运行服务器的其他命令,请参见 此处

在您的配置中,您可以使用 FastCgiServer 指令来通知 Apache 您的应用程序。

FastCgiServer /path/to/script.fcgi

运行服务器

根据您的配置,您将有不同的运行服务器的方法。在一个普通的 PHP 应用程序中,您已经创建了您的 fastCGI_app.php见如何使用),您只需启动服务器即可

php /path/to/fastCGI_app.php run

在一个注册了 DaemonRunCommand 服务的 Symfony 应用程序中,您只需运行

# If installed with Symfony Flex
./bin/console fastcgi-daemon:run

# If you use https://github.com/PHPFastCGI/SpeedfonyBundle (deprecated)
./bin/console speedfony:run 

命令选项

当您运行命令时,您可以向它传递一些选项。

自动关闭

--auto-shutdown

在接收到 5XX HTTP 状态代码后执行优雅关闭。

驱动程序

--driver userland

要使用的 FastCGI 协议实现。

文件描述符

--fd 4711

要监听的文件描述符 - 默认为 FCGI_LISTENSOCK_FILENO。

主机

--host 127.0.0.1

要监听的 TCP 主机。

内存限制

--memory-limit 256m

在关闭前,在守护进程实例上的内存限制。

端口

--port 5000

要监听的 TCP 端口(如果不存在,守护进程将监听 FCGI_LISTENSOCK_FILENO)。

安静

--quiet

减少控制台中的日志输出数量。

请求限制

--request-limit 56

在关闭前可以处理的最大请求数量。

时间限制

--time-limit 120

在关闭前,在守护进程上的秒数限制。

详细

--verbose-v

增加控制台中的日志输出。

示例运行

./bin/console fastcgi-daemon:run --port=5000 --host=127.0.0.1 -v