<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Built-in web server</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.commandline.interactive.html">Interactive shell</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.commandline.ini.html">INI settings</a></div> <div class="up"><a href="features.commandline.html">Command line usage</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="features.commandline.webserver" class="section"> <h2 class="title">Built-in web server</h2> <p class="para"> As of PHP 5.4.0, the <acronym title="Command Line Interpreter/Interface">CLI</acronym> <acronym title="Server Application Programming Interface">SAPI</acronym> provides a built-in web server. </p> <p class="para"> PHP applications will stall if a request is blocked. </p> <p class="para"> 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. </p> <p class="para"> URI requests are served from the current working directory where PHP was started, unless the -t option is used to specify an explicit document root. If a URI request does not specify a file, then either index.php or index.html in the given directory are returned. If neither file exists, then a 404 response code is returned. </p> <p class="para"> If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request. If this script returns <strong><code>FALSE</code></strong>, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser. </p> <p class="para"> Standard MIME types are returned for files with extensions: .3gp, .apk, .avi, .bmp, .css, .csv, .doc, .docx, .flac, .gif, .gz, .gzip, .htm, .html, .ics, .jpe, .jpeg, .jpg, .js, .kml, .kmz, .m4a, .mov, .mp3, .mp4, .mpeg, .mpg, .odp, .ods, .odt, .oga, .ogg, .ogv, .pdf, .pdf, .png, .pps, .pptx, .qt, .svg, .swf, .tar, .text, .tif, .txt, .wav, .webm, .wmv, .xls, .xlsx and .zip. </p> <p class="para"> MIME type support for .htm and .svg extensions was added in PHP 5.4.4. The .ogg, .ogv, and .webm extensions were added in PHP 5.4.11. The .pdf extension is recognized from PHP 5.5.5. The .3gp, .apk, .avi, .bmp, .csv, .doc, .docx, .flac, .gz, .gzip, .ics, .kml, .kmz, .m4a, .mp3, .mp4, .mpg, .mpeg, .mov, .odp, .ods, .odt, .oga, .pdf, .pptx, .pps, .qt, .swf, .tar, .text, .tif, .wav, .wmv, .xls, .xlsx and .zip extensions were added in PHP 5.5.7. </p> <div class="example" id="example-381"> <p><strong>Example #1 Starting the web server</strong></p> <div class="example-contents"> <div class="shellcode"><pre class="shellcode">$ cd ~/public_html $ php -S localhost:8000</pre> </div> </div> <div class="example-contents"><p> The terminal will show: </p></div> <div class="example-contents screen"> <div class="cdata"><pre> PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011 Listening on localhost:8000 Document root is /home/me/public_html Press Ctrl-C to quit </pre></div> </div> <div class="example-contents"><p> After URI requests for http://localhost:8000/ and http://localhost:8000/myscript.html the terminal will show something similar to: </p></div> <div class="example-contents screen"> <div class="cdata"><pre> PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011 Listening on localhost:8000 Document root is /home/me/public_html Press Ctrl-C to quit. [Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read [Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read [Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read [Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read [Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read </pre></div> </div> </div> <div class="example" id="example-382"> <p><strong>Example #2 Starting with a specific document root directory</strong></p> <div class="example-contents"> <div class="shellcode"><pre class="shellcode">$ cd ~/public_html $ php -S localhost:8000 -t foo/</pre> </div> </div> <div class="example-contents"><p> The terminal will show: </p></div> <div class="example-contents screen"> <div class="cdata"><pre> PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011 Listening on localhost:8000 Document root is /home/me/public_html/foo Press Ctrl-C to quit </pre></div> </div> </div> <div class="example" id="example-383"> <p><strong>Example #3 Using a Router Script</strong></p> <div class="example-contents"><p> In this example, requests for images will display them, but requests for HTML files will display "Welcome to PHP": </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// router.php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">'/\.(?:png|jpg|jpeg|gif)$/'</span><span style="color: #007700">, </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"REQUEST_URI"</span><span style="color: #007700">])) {<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">; </span><span style="color: #FF8000">// serve the requested resource as-is.<br /></span><span style="color: #007700">} else { <br /> echo </span><span style="color: #DD0000">"<p>Welcome to PHP</p>"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"> <div class="shellcode"><pre class="shellcode">$ php -S localhost:8000 router.php</pre> </div> </div> </div> <div class="example" id="example-384"> <p><strong>Example #4 Checking for CLI Web Server Use</strong></p> <div class="example-contents"><p> To reuse a framework router script during development with the CLI web server and later also with a production web server: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// router.php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">php_sapi_name</span><span style="color: #007700">() == </span><span style="color: #DD0000">'cli-server'</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* route static assets and return false */<br /></span><span style="color: #007700">}<br /></span><span style="color: #FF8000">/* go on with normal index.php operations */<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"> <div class="shellcode"><pre class="shellcode">$ php -S localhost:8000 router.php</pre> </div> </div> </div> <div class="example" id="example-385"> <p><strong>Example #5 Handling Unsupported File Types</strong></p> <div class="example-contents"><p> If you need to serve a static resource whose MIME type is not handled by the CLI web server, use: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// router.php<br /></span><span style="color: #0000BB">$path </span><span style="color: #007700">= </span><span style="color: #0000BB">pathinfo</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"SCRIPT_FILENAME"</span><span style="color: #007700">]);<br />if (</span><span style="color: #0000BB">$path</span><span style="color: #007700">[</span><span style="color: #DD0000">"extension"</span><span style="color: #007700">] == </span><span style="color: #DD0000">"el"</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Content-Type: text/x-script.elisp"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"SCRIPT_FILENAME"</span><span style="color: #007700">]);<br />}<br />else {<br /> return </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"> <div class="shellcode"><pre class="shellcode">$ php -S localhost:8000 router.php</pre> </div> </div> </div> <div class="example" id="example-386"> <p><strong>Example #6 Accessing the CLI Web Server From Remote Machines</strong></p> <div class="example-contents"><p> You can make the web server accessible on port 8000 to any interface with: </p></div> <div class="example-contents"> <div class="shellcode"><pre class="shellcode">$ php -S 0.0.0.0:8000</pre> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.commandline.interactive.html">Interactive shell</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.commandline.ini.html">INI settings</a></div> <div class="up"><a href="features.commandline.html">Command line usage</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>