luzrain/phpstreamserver

High performance PHP application server

v0.2.2 2024-05-15 15:52 UTC

This package is auto-updated.

Last update: 2024-11-05 19:08:28 UTC


README

PHPStreamServer - PHP Application Server

PHP >=8.2 Version Tests Status

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.

Key features:

  • Process manager;
  • Scheduler;
  • Workers lifecycle management (reload by TTL, max memory, max requests, on exception, on each request);
  • HTTP/2

Requirements and limitations:

  • Unix based OS (no windows support);
  • php-posix and php-pcntl extensions;
  • php-uv extension is not required, but recommended for better performance.

Getting started

Install composer packages

$ composer require luzrain/phpstreamserver

Configure server

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\BundledPlugin\HttpServer\HttpServerPlugin;
use Luzrain\PHPStreamServer\BundledPlugin\HttpServer\HttpServerProcess;
use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\PeriodicProcess;
use Luzrain\PHPStreamServer\BundledPlugin\Scheduler\SchedulerPlugin;
use Luzrain\PHPStreamServer\BundledPlugin\Supervisor\WorkerProcess;
use Luzrain\PHPStreamServer\Server;

$server = new Server();

$server->addPlugin(
    new HttpServerPlugin(),
    new SchedulerPlugin(),
);

$server->addWorker(
    new HttpServerProcess(
        name: 'Web Server',
        count: 1,
        listen: '0.0.0.0:8088',
        onStart: function (HttpServerProcess $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),
            };
        }
    ),
    new WorkerProcess(
        name: 'Supervised Program',
        count: 1,
        onStart: function (WorkerProcess $worker): void {
            // custom long running process
        },
    ),
    new PeriodicProcess(
        name: 'Scheduled program',
        schedule: '*/1 * * * *',
        onStart: function (PeriodicProcess $worker): void {
            // runs every 1 minute
        },
    ),
);

exit($server->run());

Run

$ php server.php start