php-server is a utility that simplifies the process of starting PHP's built-in development web server directly from Node.js applications. It's designed for development environments, not production, and provides an API to control the server, including specifying port, hostname, base directory, and custom PHP configurations like INI directives or a router script. The package is currently at version 3.0.0 and maintains an active release cadence, primarily driven by Node.js version updates. Key differentiators include its simple API for programmatic control, automatic process management, and seamless integration with Node.js development workflows, making it easy to spin up a quick PHP backend for testing or local development without managing separate PHP processes manually. It is a pure ESM package.
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
The package is pure ESM since v1.0.0. CommonJS `require` is not supported.
import phpServer from 'php-server';
Use `Options` for type hinting in TypeScript. The primary export is the default function.
import phpServer, { Options as PhpServerOptions } from 'php-server';
The `Server` interface represents the return type of `phpServer()` for type hinting in TypeScript, providing `url`, `stdout`, `stderr`, and `stop()` method.
import phpServer, { Server as ServerInstance } from 'php-server';
Demonstrates how to start a basic PHP server, a customized server with specific port, hostname, base directory, and browser opening, along with type imports.
import phpServer from 'php-server';
async function startServers() {
// Basic usage
const server1 = await phpServer();
console.log(`PHP server running at ${server1.url}`);
// With custom configuration
const server2 = await phpServer({
port: 8080,
hostname: 'localhost',
base: './public',
open: true, // Opens browser automatically
router: './router.php' // Assuming router.php exists in the project root
});
console.log(`Custom PHP server running at ${server2.url}`);
// Example of a simple router.php file (create this in your project root)
// router.php:
// <?php
// if (preg_match('/\.(?:png|jpg|jpeg|gif|css|js)$/', $_SERVER["REQUEST_URI"])) {
// return false; // Serve the requested resource as-is
// } else {
// echo "<h1>Hello from PHP!</h1>";
// echo "<p>URI: " . $_SERVER["REQUEST_URI"] . "</p>";
// }
// ?>
// Clean up when done (e.g., on process exit or after tests)
// server1.stop();
// server2.stop();
}
startServers().catch(console.error);
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.