luzrain/phprunner

This package is abandoned and no longer maintained. The author suggests using the luzrain/phpstreamserver package instead.

High performance PHP application server

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

This package is auto-updated.

Last update: 2024-05-15 15:56:41 UTC


README

PHPStreamServer logo

PHPStreamServer - PHP Application Server

PHP >=8.2 Version Tests Status

PHPStreamServer is a high performance event-loop based process manager, TCP, and UDP server written in PHP.
With a built-in PSR-7 HTTP server you can easily integrate any PSR-7 compatible framework with it in no time.
The built-in HTTP server is memory efficient no matter how large your HTTP requests and responses you operate are.
PHPStreamServer is supports TLS encryption and the ability to implement custom protocols.

Key features:

  • Supervisor;
  • Workers lifecycle management (reload by TTL, max memory, max requests, on exception, on each request);
  • PSR-7 HTTP server;

Requirements and limitations:

  • Unix based OS (no windows support);
  • php-posix and php-pcntl extensions;
  • php-uv extension is not required, but highly 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 Luzrain\PHPStreamServer\Exception\HttpException;
use Luzrain\PHPStreamServer\Listener;
use Luzrain\PHPStreamServer\Server;
use Luzrain\PHPStreamServer\Server\Connection\ConnectionInterface;
use Luzrain\PHPStreamServer\Server\Http\Psr7\Response;
use Luzrain\PHPStreamServer\Server\Protocols\Http;
use Luzrain\PHPStreamServer\WorkerProcess;
use Psr\Http\Message\ServerRequestInterface;

$server = new Server();
$server->addWorkers(new WorkerProcess(
    name: 'HTTP Server',
    onStart: function (WorkerProcess $worker) {
        $worker->startListener(new Listener(
            listen: 'tcp://0.0.0.0:80',
            protocol: new Http(),
            onMessage: function (ConnectionInterface $connection, ServerRequestInterface $data): void {
                $response = match ($data->getUri()->getPath()) {
                    '/' => new Response(body: 'Hello world'),
                    '/ping' => new Response(body: 'pong'),
                    default => throw HttpException::createNotFoundException(),
                };
                $connection->send($response);
            },
        ));
    },
));

exit($server->run());

Run

$ php server.php start