PHP 5.4+ comes with a built-in development server. It can be used to run applications without having to install a production HTTP server such as nginx or Apache. The built-in server is only designed to be used for development and testing purposes.
It can be started by using the -S
flag:
php -S <host/ip>:<port>
index.php
file containing:<?php
echo "Hello World from built-in PHP server";
Run the command php -S localhost:8080
from the command line. Do not include http://
. This will start a web server listening on port 8080 using the current directory that you are in as the document root.
Open the browser and navigate to http://localhost:8080
. You should see your "Hello World" page.
To override the default document root (i.e. the current directory), use the -t
flag:
php -S <host/ip>:<port> -t <directory>
E.g. if you have a public/
directory in your project you can serve your project from that directory using php -S localhost:8080 -t public/
.
Every time a request is made from the development server, a log entry like the one below is written to the command line.
[Mon Aug 15 18:20:19 2016] ::1:52455 [200]: /