Skip to main content

Configuration

PHPStreamServer allows you to define settings at various levels of the application.
Configuration parameters can be passed to Server, Plugin, or Worker constructors.
Use named parameters when specifying configuration parameters.

<?php

require __DIR__ . '/vendor/autoload.php';

use PHPStreamServer\Core\Server;
use PHPStreamServer\Core\Worker\SupervisedWorker;
use PHPStreamServer\Core\Worker\WorkerFactory;
use PHPStreamServer\Plugin\HttpServer\HttpServerPlugin;

$server = new Server(
// Server configuration
);

$server->addPlugin(
new HttpServerPlugin(
// Plugin configuration
),
);

$server->addWorker(
new SupervisedWorker(
// Worker configuration
),
);

$server->addWorkerFactory(
new WorkerFactory(
// WorkerFactory configuration
),
);

exit($server->run());

Server Configuration

Server-level configuration controls application-wide runtime behavior.

ParameterTypeDefaultDescription
pidFilestring|nullnullOptional. By default, PHPStreamServer derives the path in /run when writable, or in the system temporary directory otherwise.
socketFilestring|nullnullOptional. By default, PHPStreamServer derives the path in /run when writable, or in the system temporary directory otherwise.
stopTimeoutint|nullnullOptional. Seconds to wait before forcefully terminating workers. Defaults to 10.
restartDelayfloat|nullnullOptional. Delay, in seconds, between process restarts. Defaults to 0.25.

Plugin Configuration

Plugin-level configuration controls the capabilities provided by each plugin. See the plugins documentation for detailed options and examples.
Read more →

Worker Configuration

Worker-level configuration controls how individual workers run. PHPStreamServer provides two built-in worker types, and plugins can provide additional types.
Read more →

Workers use the server process's effective user and group by default. If user is set without group, the server process's effective group is retained.

⚙️ SupervisedWorker

Worker class: SupervisedWorker
This worker type is designed to run long-running PHP code.

ParameterTypeDefaultDescription
namestring|nullnullOptional. Worker name. Defaults to worker <id>.
countint1Optional. Number of processes. Must be between 1 and 1024.
reloadablebooltrueOptional. Whether the worker can be reloaded.
userstring|nullnullOptional. Unix user name or user ID (UID).
groupstring|nullnullOptional. Unix group name or group ID (GID).
onStartClosure|nullnullOptional. Closure(SupervisedWorker): void callback executed when the worker starts.
onStopClosure|nullnullOptional. Closure(SupervisedWorker): void callback executed when the worker stops.
onReloadClosure|nullnullOptional. Closure(SupervisedWorker): void callback executed when the worker reloads.
reloadStrategiesReloadStrategy[][]Optional. Reload strategies assigned to the worker.

⚙️ ExecutableWorker

Worker class: ExecutableWorker
This worker type is designed to run external processes, allowing PHPStreamServer to manage and supervise programs or scripts outside of PHP.

ParameterTypeDefaultDescription
commandstringrequiredExternal command or script.
namestring|nullnullOptional. Worker name. Defaults to worker <id>.
countint1Optional. Number of processes. Must be between 1 and 1024.
reloadablebooltrueOptional. Whether the worker can be reloaded.
userstring|nullnullOptional. Unix user name or user ID (UID).
groupstring|nullnullOptional. Unix group name or group ID (GID).

WorkerFactory Configuration

Worker factory class: WorkerFactory
A worker factory defines workers that can be created and started on demand. Unlike a worker passed to Server::addWorker(), registering a factory does not create or start a worker. The factory is invoked when the master process receives a StartWorkerCommand with a matching factoryId.

ParameterTypeDefaultDescription
idstringrequiredUnique factory identifier. Pass this value as StartWorkerCommand::$factoryId.
factoryClosurerequiredCreates a new worker from the runtime parameters supplied by StartWorkerCommand::$parameters.

The factory closure has the type Closure(array<string, mixed>=): WorkerInterface. It must declare a return type that implements WorkerInterface. It may accept no parameters or one array parameter.

Reload Strategies

Reload strategies determine when reloadable workers should restart. PHPStreamServer provides three built-in strategies, and plugins can provide additional strategies.
Read more →

🔄️ TTLReloadStrategy

Reload strategy class: TTLReloadStrategy
Reloads a worker after a specified time-to-live (TTL) interval. This periodic restart helps prevent potential issues caused by long-running processes.

ParameterTypeDefaultDescription
ttlintrequiredTime-to-live interval in seconds.

🔄️ MaxMemoryReloadStrategy

Reload strategy class: MaxMemoryReloadStrategy
Checks memory usage and reloads the worker when it exceeds the configured threshold.

ParameterTypeDefaultDescription
maxMemoryintrequiredMemory consumption threshold in bytes.

🔄️ ExceptionReloadStrategy

Reload strategy class: ExceptionReloadStrategy
Reloads a worker after an unexpected Throwable. By default, compile errors and HTTP errors do not trigger a reload.

ParameterTypeDefaultDescription
allowedExceptionsstring[][]Optional. Throwable classes that should not trigger a reload.

Commands

Commands allow you to communicate with the master process and manage the server. Additional command implementations are available through plugins.
Read more →

▶️ GetWorkersCommand

Command class: GetWorkersCommand
Retrieves metadata for workers registered with the supervisor.

▶️ GetProcessesCommand

Command class: GetProcessesCommand
Retrieves metadata for processes managed by the supervisor.

▶️ GetNetworkInfoCommand

Command class: GetNetworkInfoCommand
Retrieves network traffic and active connection information for worker processes.

▶️ GetServerStatusCommand

Command class: GetServerStatusCommand
Retrieves runtime information about the running server.

▶️ StartWorkerCommand

Command class: StartWorkerCommand
Starts a worker using a registered WorkerFactory and returns its assigned ID, or 0 if startup fails.

ParameterTypeDefaultDescription
factoryIdstringrequiredID of the registered worker factory.
parametersarray<string, mixed>[]Runtime parameters passed to the factory.

▶️ StopWorkerCommand

Command class: StopWorkerCommand
Stops all processes of a factory-created worker.

ParameterTypeDefaultDescription
workerIdintrequiredAssigned ID of the worker to stop.

▶️ ReloadServerCommand

Command class: ReloadServerCommand
Requests a server reload.

ParameterTypeDefaultDescription
opcacheResetboolfalseWhether to invalidate scripts cached by OPCache when OPCache is enabled before reload.

▶️ StopServerCommand

Command class: StopServerCommand
Requests a graceful server shutdown.

ParameterTypeDefaultDescription
codeint0Server process exit code.