Jason Bamford's blog

PHP Built-in Server

PHP has a really useful feature, which is a built in web-server, great for local testing of a php website. It's all installed and available by default on MacOS and some versions of Linux. To use, cd to the html root directory and run:

$php -S localhost:8888 testserver.php

This will run the file testserver.php for each request received from the local machine at port 8888.

Example testserver.php file:


/* remove get parameters */
$file_uri = explode('?',$_SERVER['REQUEST_URI'],2)[0];

/* remove leading / to make relative unix path */
if ($file_uri[0] == "/")  $file_uri = substr($file_uri,1);

/* run controller to handle non-existent uri */
if (! is_file($file_uri)) {
  include 'controller.php';
} else {
  /* all other files served automatically by php server */
  return False;

PHP Manual - Built-in web server