composer require weew/http-server
To start the server, simply pass in a hostname, desired port and the root directory for your server.
// all files within the given directory will be available
// at http://localhost:9999, if you've passed a file
// name instead of a directory the server will always serve this
// file, no matter how the URI looks like
$server = new HttpServer('localhost', 9999, __DIR__);
// incoming requests will be logged to this file
$server->setLogFile('/tmp/log');
$server->start();
$server->isRunning(); // true
$server->stop();
You can tell the server to block current process until the server has started by passing in a $waitForProcess
value. You can also disable the server output completely.
// starting the server will wait for the server to start
// for a maximum of 2 seconds, then it will throw an exception
// saying that the process took too long to start
// the default value is 5.0 seconds
$waitForProcess = 2.0;
// enables log messages like
// [HTTP SERVER] Wed, 12 Aug 2015 19:49:25 +0200 - HTTP server started on localhost:9999 with PID 99412
// [HTTP SERVER] Wed, 12 Aug 2015 19:56:18 +0200 - Server is already running at localhost:9999 with PID 99535
// [HTTP SERVER] Wed, 12 Aug 2015 19:49:25 +0200 - Killing process with PID 99412
$enableOutput = true;
$server = new HttpServer('localhost', 9999, __DIR__, $waitForProcess, $enableOutput);
$server->start();
- HTTP Blueprint: spin up a server, serve some content, shutdown the server.