Note
This package is now under development
PHPStreamServer is a high performance event-loop based process manager, scheduler and webserver written in PHP. This application server is designed to replace traditional setup for running php applications such as nginx, php-fpm, cron, supervisor.
- Process manager;
- Scheduler;
- Workers lifecycle management (reload by TTL, max memory, max requests, on exception, on each request);
- HTTP/2
- Unix based OS (no windows support);
- php-posix and php-pcntl extensions;
- php-uv extension is not required, but recommended for better performance.
$ composer require luzrain/phpstreamserver
Here is example of simple http server.
// server.php
use Amp\Http\Server\HttpErrorException;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use Luzrain\PHPStreamServer\PeriodicProcess;
use Luzrain\PHPStreamServer\Plugin\HttpServer\HttpServer;
use Luzrain\PHPStreamServer\Plugin\Scheduler\Scheduler;
use Luzrain\PHPStreamServer\Plugin\Supervisor\Supervisor;
use Luzrain\PHPStreamServer\Server;
use Luzrain\PHPStreamServer\WorkerProcess;
$server = new Server();
$server->addPlugin(
new HttpServer(
name: 'web server',
count: 1,
listen: '0.0.0.0:8088',
onStart: function (WorkerProcess $worker, mixed &$context): void {
// initialization
},
onRequest: function (Request $request, mixed &$context): Response {
return match ($request->getUri()->getPath()) {
'/' => new Response(body: 'Hello world'),
'/ping' => new Response(body: 'pong'),
default => throw new HttpErrorException(404),
};
}
),
);
$server->addPlugin(
new Scheduler(
name: 'scheduled program',
schedule: '*/1 * * * *',
command: function (PeriodicProcess $worker): void {
// runs every 1 minute
},
),
);
$server->addPlugin(
new Supervisor(
name: 'supervised program',
count: 1,
command: function (WorkerProcess $worker): void {
// custom long running process
},
),
);
exit($server->run());
$ php server.php start