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.
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.
This project is currently in early stages of development and not considered stable.
Contributions and suggestions are welcome.
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.
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();
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();
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();
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;
# ...
}
# ...
}
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.
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
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
When you run the command you have a few option you could pass to it.
--auto-shutdown
Perform a graceful shutdown after receiving a 5XX HTTP status code.
--driver userland
The implementation of the FastCGI protocol to use.
--fd 4711
File descriptor to listen on - defaults to FCGI_LISTENSOCK_FILENO.
--host 127.0.0.1
TCP host to listen on.
--memory-limit 256m
The memory limit on the daemon instance before shutting down.
--port 5000
TCP port to listen on (if not present, daemon will listen on FCGI_LISTENSOCK_FILENO).
--quiet
Reduces the number of log output in the console.
--request-limit 56
The maximum number of requests to handle before shutting down.
--time-limit 120
The time limit on the daemon in seconds before shutting down.
--verbose
or -v
Increases the log output in the console.
./bin/console fastcgi-daemon:run --port=5000 --host=127.0.0.1 -v