Starting from PHP 5.4 , PHP incorporates a feature of a built-in web server. You can take the advantage of PHP built-in web server for development and testing of your PHP applications. It means you don’t need to configure Apache to run your PHP applications.
If you have already installed LAMP , WAMP, XAMPP on your system, still you can try and use this feature.
To use PHP built-in web server you need PHP 5.4 or higher version. If you are not sure which PHP version you have installed on your machine, just open the terminal and type a simple command (php -v).
PHP 5.5.9-1+sury.org~precise+1 (cli)(built: Feb 13 2014 15:53:53)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
I have already installed PHP version 5.5 .
To check some of the options by typing PHP -h (PHP help command) on a terminal.
-a Run as interactive shell
-c <path>|<file> Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse and execute <file>.
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-r <code> Run PHP <code> without using script tags <?..?>
-B <begin_code> Run PHP <begin_code> before processing input lines
-R <code> Run PHP <code> for every input line
-F <file> Parse and execute <file> for every input line
-E <end_code> Run PHP <end_code> after processing all input lines
-H Hide any passed arguments from external tools.
<strong>-S <addr>:<port> Run with built-in web server.</strong>
<strong>-t <docroot> Specify document root <docroot> for built-in web server.</strong>
-s Output HTML syntax highlighted source.
-v Version number
-w Output source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.
Look at the line marked in bold (-S, -t) this is the option for running PHP built-in web server.
How to Start with PHP Built-in Web Server
To use a PHP built-in web server, go to the root directory of your project and run this simple command on a terminal.
php -S localhost:8000
Using this command a simple Web Server will run and listen to a port 8000. You can specify any port. If specified port is used by some other process, it will throw an error.
// If port is not free, then error is thrown.
[Sat Oct 25 10:43:00 2014] Failed to listen on localhost:8080 (reason: Address already in use)
When a web server is running successfully then the following output will be displayed on a terminal.
PHP 5.5.9-1+sury.org~precise+1 Development Server started at Fri Oct 24 23:37:53 2014
Listening on http://localhost:8000
Document root is /var/www
When a web server is started, by default your current directory is your document root . If you want to change the document root, you can specify them with -t flag.
// Specify Document Root /var/www/laravel
php -S localhost:8000 -t /var/www/laravel
Here, /var/www/laravel is my document root and I can request all the files present in a laravel directory.
As per PHP Manual :
Warning : This web server was designed to aid application development. It may also be useful for testing purposes or for application demonstrations that are run in controlled environments. It is not intended to be a full-featured web server. It should not be used on a public network.
Remember this is a great feature for testing your PHP applications but it’s not a full-featured web server.
I have written this post based on my experience with PHP built-in web server. If you are using this feature and you want to add something, you can let us know through your comment.