<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/features.commandline.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'features.commandline.webserver.php',
    1 => 'Built-in web server',
    2 => 'Built-in web server',
  ),
  'up' => 
  array (
    0 => 'features.commandline.php',
    1 => 'Command line usage',
  ),
  'prev' => 
  array (
    0 => 'features.commandline.interactive.php',
    1 => 'Interactive shell',
  ),
  'next' => 
  array (
    0 => 'features.commandline.ini.php',
    1 => 'INI settings',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'features/commandline.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="features.commandline.webserver" class="section">
  <h2 class="title">Built-in web server</h2>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    This web server is 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>
  </div>

  <p class="para">
   The <abbr title="Command Line Interpreter/Interface">CLI</abbr> <abbr title="Server Application Programming Interface">SAPI</abbr> provides a built-in web server.
  </p>

  <p class="para">
   The web server runs only one single-threaded process, so
   PHP applications will stall if a request is blocked.
  </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, the lookup for index.php and index.html
    will be continued in the parent directory and so on until one is found or
    the document root has been reached. If an index.php or index.html is found,
    it is returned and $_SERVER[&#039;PATH_INFO&#039;] is set to the trailing part of
    the URI. Otherwise 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 &quot;router&quot; script.  The script is run at
    the start of each HTTP request.  If this script returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>,
    then the requested resource is returned as-is.  Otherwise the
    script&#039;s output is returned to the browser.
  </p>

  <p class="para">
   Standard MIME types are returned for files with extensions:
   <span class="simplelist"><code class="literal">.3gp</code>, <code class="literal">.apk</code>, <code class="literal">.avi</code>, <code class="literal">.bmp</code>, <code class="literal">.css</code>, <code class="literal">.csv</code>, <code class="literal">.doc</code>, <code class="literal">.docx</code>, <code class="literal">.flac</code>, <code class="literal">.gif</code>, <code class="literal">.gz</code>, <code class="literal">.gzip</code>, <code class="literal">.htm</code>, <code class="literal">.html</code>, <code class="literal">.ics</code>, <code class="literal">.jpe</code>, <code class="literal">.jpeg</code>, <code class="literal">.jpg</code>, <code class="literal">.js</code>, <code class="literal">.kml</code>, <code class="literal">.kmz</code>, <code class="literal">.m4a</code>, <code class="literal">.mov</code>, <code class="literal">.mp3</code>, <code class="literal">.mp4</code>, <code class="literal">.mpeg</code>, <code class="literal">.mpg</code>, <code class="literal">.odp</code>, <code class="literal">.ods</code>, <code class="literal">.odt</code>, <code class="literal">.oga</code>, <code class="literal">.ogg</code>, <code class="literal">.ogv</code>, <code class="literal">.pdf</code>, <code class="literal">.png</code>, <code class="literal">.pps</code>, <code class="literal">.pptx</code>, <code class="literal">.qt</code>, <code class="literal">.svg</code>, <code class="literal">.swf</code>, <code class="literal">.tar</code>, <code class="literal">.text</code>, <code class="literal">.tif</code>, <code class="literal">.txt</code>, <code class="literal">.wav</code>, <code class="literal">.webm</code>, <code class="literal">.wmv</code>, <code class="literal">.xls</code>, <code class="literal">.xlsx</code>, <code class="literal">.xml</code>, <code class="literal">.xsl</code>, <code class="literal">.xsd</code>, <code class="literal">.zip</code></span>
   .
  </p>

  <p class="simpara">
   As of PHP 7.4.0, the built-in webserver can be configured to fork multiple
   workers in order to test code that requires multiple concurrent requests
   to the built-in webserver.
   Set the <var class="envar">PHP_CLI_SERVER_WORKERS</var> environment variable to the
   number of desired workers before starting the server.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">This feature is not supported on Windows.</span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    For information on PHP commandline usage and options, run
    <strong class="command">php --help</strong> or <strong class="command">man php</strong>. Not all
    options will apply when running the web server.
   </span>
  </p></blockquote>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    This <em>experimental</em> feature is <em>not</em>
    intended for production usage. Generally, the built-in Web Server is
    <em>not</em> intended for production usage.
   </p>
  </div>

  <div class="example" id="example-1">
   <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 class="example-contents"><p>
    Note that prior to PHP 7.4.0, symlinked statical resources have not been
    accessible on Windows, unless the router script would handle these.
   </p></div>
  </div>

  <div class="example" id="example-2">
   <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-3">
   <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 &quot;Welcome to PHP&quot;:
</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">"&lt;p&gt;Welcome to PHP&lt;/p&gt;"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</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-4">
   <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">&lt;?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">?&gt;</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-5">
   <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">&lt;?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">?&gt;</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-6">
   <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 class="warning"><strong class="warning">Warning</strong>
    <p class="para">
      The built-in Web Server should not be used on a public network.
    </p>
   </div>
  </div>

 </div><?php manual_footer($setup); ?>