phpfastcgi/fastcgi-daemon

A FastCGI daemon written in PHP

v0.10.0 2016-12-01 21:05 UTC

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. Importantly, this library currently lacks support for uploaded files.

Contributions and suggestions are welcome.

Usage

Below is an example of a simple 'Hello, World!' FastCGI application in PHP.

<?php // command.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) {
    // $request->getServerRequest()         returns PSR-7 server request object
    // $request->getHttpFoundationRequest() returns HTTP foundation request object
    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();

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

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
php /path/to/application.php run

Or with Symfony:

#!/bin/bash
php /path/to/bin/console speedfony:run --env=prod

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

FastCgiServer /path/to/script.fcgi

By default, the daemon will listen on FCGI_LISTENSOCK_FILENO, but it can also be configured to listen on a TCP address. For example:

#!/bin/bash
php /path/to/application.php run --port=5000 --host=localhost

Or with Symfony:

#!/bin/bash
php /path/to/bin/console speedfony:run --env=prod --port=5000 --host=localhost