定制 phpfastcgi/fastcgi-daemon 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phpfastcgi/fastcgi-daemon

Composer 安装命令:

composer require phpfastcgi/fastcgi-daemon

包简介

A FastCGI daemon written in PHP

README 文档

README

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

A FastCGI daemon written in PHP. Visit the project website for more documentation and guides.

Check out the project performance benchmarks to see how we got a "Hello, World!" Slim application to handle 5,500 rq/s.

Introduction

Using this daemon, applications can stay alive between HTTP requests whilst operating behind the protection of a FastCGI enabled web server.

The daemon requires a handler to be defined that accepts request objects and returns PSR-7 or HttpFoundation responses.

The Speedfony Bundle integrates this daemon with the Symfony2 framework. The Slim Adapter integrates this daemon with the Slim v3 framework. The Silex Adapter integrates this daemon with the Silex framework.

There is also an un-official ZF2 Adapter that integrates this daemon with Zend Framework 2.

Current Status

This project is currently in early stages of development and not considered stable.

Contributions and suggestions are welcome.

Usage

Below is an example of a simple 'Hello, World!' FastCGI application. There are 3 examples. One with the PHPFastCGI request, one with Symfony HTTP Foundation request and one with PSR-7 Request.

PHPFastCGI request

Using the pure PHPFastCGI is the fastest method since it does not involve any converting of requests.

We need a PSR-7 implementation's response object.

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 Request

Use this when your application is taking advantage of the Symfony echosystem.

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 Request

Here is the same example but with PSR-7 HTTP objects. First you need to install any PSR-17 (HTTP Factory) implementation and then a PSR-17 utility library (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();

Server Configuration

NGINX

With NGINX, you need to use a process manager such as supervisord to manage instances of your application. Have a look at AstroSplash for an example supervisord configuration.

Below is an example of the modification that you would make to the Symfony NGINX configuration. The core principle is to replace the PHP-FPM reference with one to a cluster of workers.

# 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 +

If you are using Apache 2.4.10 or later you need to use mod_proxy_fcgi. You need to use a process manager such as supervisord to manage instances of your application. For development you only need to start you application (see Running the server) then add the following to your VirtualHost config:

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

Go to http://127.0.0.1/index.php to test your setup. Note that index.php needs to exist but could be an empty file.

Apache 2.0 - 2.2

If you wish to configure your FastCGI application to work with the apache web server, you can use the apache FastCGI module to process manage your application.

This can be done by creating a FCGI script that launches your application and inserting a FastCgiServer directive into your virtual host configuration.

Here is an example script.fcgi:

#!/bin/bash

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

See other commands to run the server here.

In your configuration, you can use the FastCgiServer directive to inform Apache of your application.

FastCgiServer /path/to/script.fcgi

Running the server

Depending on your setup, you will have different ways of running the server. In a normal PHP application where you have created your own fastCGI_app.php (see how), you may start the server simply by:

php /path/to/fastCGI_app.php run

In a Symfony application where you have registered DaemonRunCommand as a service, you may just run:

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

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

Command options

When you run the command you have a few option you could pass to it.

Auto shutdown

--auto-shutdown

Perform a graceful shutdown after receiving a 5XX HTTP status code.

Driver

--driver userland

The implementation of the FastCGI protocol to use.

File descriptor

--fd 4711

File descriptor to listen on - defaults to FCGI_LISTENSOCK_FILENO.

Host

--host 127.0.0.1

TCP host to listen on.

Memory Limit

--memory-limit 256m

The memory limit on the daemon instance before shutting down.

Port

--port 5000

TCP port to listen on (if not present, daemon will listen on FCGI_LISTENSOCK_FILENO).

Quiet

--quiet

Reduces the number of log output in the console.

Request limit

--request-limit 56

The maximum number of requests to handle before shutting down.

Time limit

--time-limit 120

The time limit on the daemon in seconds before shutting down.

Verbose

--verbose or -v

Increases the log output in the console.

Example run

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

phpfastcgi/fastcgi-daemon 适用场景与选型建议

phpfastcgi/fastcgi-daemon 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.42k 次下载、GitHub Stars 达 329, 最近一次更新时间为 2015 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「server」 「daemon」 「fastcgi」 「fast cgi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phpfastcgi/fastcgi-daemon 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phpfastcgi/fastcgi-daemon 我们能提供哪些服务?
定制开发 / 二次开发

基于 phpfastcgi/fastcgi-daemon 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 11.42k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 330
  • 点击次数: 21
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 329
  • Watchers: 17
  • Forks: 16
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-18