2017 © Pedro Peláez
 

library fastcgi-daemon

A FastCGI daemon written in PHP

image

phpfastcgi/fastcgi-daemon

A FastCGI daemon written in PHP

  • Wednesday, August 1, 2018
  • by AndrewCarterUK
  • Repository
  • 23 Watchers
  • 306 Stars
  • 3,680 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 9 Forks
  • 6 Open issues
  • 21 Versions
  • 6 % Grown

The README.md

FastCGI Daemon

Latest Stable Version Build Status Coverage Status Scrutinizer Code Quality Total Downloads, (*1)

A FastCGI daemon written in PHP. Visit the project website for more documentation and guides., (*2)

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

Introduction

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

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

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., (*6)

There is also an un-official ZF2 Adapter that integrates this daemon with Zend Framework 2., (*7)

Current Status

This project is currently in early stages of development and not considered stable., (*8)

Contributions and suggestions are welcome., (*9)

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., (*10)

PHPFastCGI request

Using the pure PHPFastCGI is the fastest method since it does not involve any converting of requests., (*11)

We need a PSR-7 implementation's response object., (*12)

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('

Hello, World!

'); }; // 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., (*13)

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)., (*14)

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., (*15)

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., (*16)

# 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:, (*17)

<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., (*18)

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., (*19)

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

Here is an example script.fcgi:, (*21)

#!/bin/bash

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

See other commands to run the server here., (*22)

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

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:, (*24)

php /path/to/fastCGI_app.php run

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

# 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., (*26)

Auto shutdown

--auto-shutdown, (*27)

Perform a graceful shutdown after receiving a 5XX HTTP status code., (*28)

Driver

--driver userland, (*29)

The implementation of the FastCGI protocol to use., (*30)

File descriptor

--fd 4711, (*31)

File descriptor to listen on - defaults to FCGI_LISTENSOCK_FILENO., (*32)

Host

--host 127.0.0.1, (*33)

TCP host to listen on., (*34)

Memory Limit

--memory-limit 256m, (*35)

The memory limit on the daemon instance before shutting down., (*36)

Port

--port 5000, (*37)

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

Quiet

--quiet, (*39)

Reduces the number of log output in the console., (*40)

Request limit

--request-limit 56, (*41)

The maximum number of requests to handle before shutting down., (*42)

Time limit

--time-limit 120, (*43)

The time limit on the daemon in seconds before shutting down., (*44)

Verbose

--verbose or -v, (*45)

Increases the log output in the console., (*46)

Example run

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

The Versions

01/08 2018

dev-master

9999999-dev

A FastCGI daemon written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

01/08 2018

dev-bug/test-fail-from-tempnam-notice

dev-bug/test-fail-from-tempnam-notice

A FastCGI daemon written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

01/03 2018
01/03 2018

dev-ci-dependency-fix

dev-ci-dependency-fix

A FastCGI daemon written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

01/12 2016

v0.10.0

0.10.0.0

A FastCGI daemon written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

01/12 2016

dev-shutdown

dev-shutdown

A FastCGI daemon written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

01/12 2016

v0.9.0

0.9.0.0

A FastCGI daemon written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

22/02 2016

v0.8.0

0.8.0.0

A FastCGI daemon written in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

16/10 2015

dev-php5-fastcgi-ext

dev-php5-fastcgi-ext

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

23/09 2015

v0.7.0

0.7.0.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

29/08 2015

v0.6.2

0.6.2.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

27/08 2015

v0.6.1

0.6.1.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

26/08 2015

v0.6.0

0.6.0.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

20/08 2015

v0.5.3

0.5.3.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

20/08 2015

v0.5.2

0.5.2.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

19/08 2015

v0.5.1

0.5.1.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

19/08 2015

v0.5.0

0.5.0.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

14/08 2015

v0.4.0

0.4.0.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

04/07 2015

v0.3.0

0.3.0.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

30/05 2015

v0.2.0

0.2.0.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

 

The Development Requires

server daemon fastcgi fast cgi

19/05 2015

v0.1.0

0.1.0.0

A FastCGI daemon written in PHP

  Sources   Download

GPL v2

The Requires

  • php >=5.4.0

 

The Development Requires

server daemon fastcgi fast cgi