PHP is a scripting language, which requires a PHP interpreter to analyze and run PHP files. When using PHP as a CGI to serve web requests, it needs to be embedded into some kind of web server, most commonly integrated into Apache or IIS. This means that before using PHP, you need to install Apache or IIS, and Correctly configure them and PHP integration parameters. Although this configuration has been standardized and the documentation is very rich, we still often encounter problems when installing Apache and PHP integration. Moreover, sometimes we just want to test a simple PHP feature and do not want to install and start the Apache service for this purpose. .
But according to the official documentation, this built-in web server is only for development and testing, and is not recommended for use in production environments. Because this server accepts and processes requests sequentially and cannot handle them concurrently.
This built-in web server is very convenient to use. You only need to execute the following command:
<ol><li><span><span>$ php -S localhost:8000 </span></span></li></ol>
Then you can access it. After starting in this way, the default web service directory is the current directory where the command is executed. If you do not want to use the current directory, you need to use the -t parameter to specify it.
Example #1 Start the Web server
<ol> <li><span><span>$ cd ~/public_html </span></span></li> <li><span>$ php -S localhost:8000 </span></li> </ol>
Terminal output information:
<ol> <li><span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011 </span></span></li> <li><span>Listening on localhost:8000 </span></li> <li><span>Document root is /home/me/public_html </span></li> <li><span>Press Ctrl-C to quit </span></li> </ol>
After requesting the http://localhost:8000/ and http://localhost:8000/myscript.html addresses, the terminal outputs information similar to the following :
<ol> <li><span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011 </span></span></li> <li><span>Listening on localhost:8000 </span></li> <li><span>Document root is /home/me/public_html </span></li> <li><span>Press Ctrl-C to quit. </span></li> <li><span>[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read </span></li> <li><span>[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read </span></li> <li><span>[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read </span></li> <li><span>[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read </span></li> <li><span>[Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read </span></li> </ol>
Example #2 Specify the root directory of the document when starting the web server
<ol> <li><span><span>$ cd ~/public_html </span></span></li> <li><span>$ php -S localhost:8000 -t foo/ </span></li> </ol>
Terminal display message:
<ol> <li><span><span>PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011 </span></span></li> <li><span>Listening on localhost:8000 </span></li> <li><span>Document root is /home/me/public_html/foo </span></li> <li><span>Press Ctrl-C to quit </span></li> </ol>
If you append a php script file after the startup command line, then this file will be treated as a "router" script. This script will be responsible for all HTTP requests. If this script returns FALSE when executed, the requested resource will be returned normally. If it is not FALSE, the content generated by this script will be displayed in the browser.
Example #3 Using router script
In this example, a request for an image will return the corresponding image, but a request for an HTML file will display "Welcome to PHP":
<ol> <li><span><span><?php </span></span></li><li><span>// router.php </span><span> </span></li><li><span>if</span><span> (preg_match(</span><span>'/\.(?:png|jpg|jpeg|gif)$/'</span><span>, </span><span>$_SERVER</span><span>[</span><span>"REQUEST_URI"</span><span>])) { </span></li><li><span>return</span><span> false; </span><span>// serve the requested resource as-is. </span><span> </span></li><li><span>} </span><span>else</span><span> { </span></li><li><span>echo</span><span> </span><span>"<p>Welcome to PHP</p>"</span><span>; </span></span></li> <li><span>} </span></li> <li><span>?> </span></li> </ol>
<ol><li><span><span>$ php -S localhost:8000 router.php </span></span></li></ol>
Example #4 Determine whether it is being used The built-in web server
adjusts the different behaviors of the same PHP router script in the built-in web server and the production server through program judgment:
<ol><li><span><span><?php </span></span></li><li><span>// router.php </span><span> </span></li><li><span>if</span><span> (php_sapi_name() == </span><span>'cli-server'</span><span>) { </span></li><li><span>/* route static assets and return false */</span><span> </span></li><li><span>} </span></li><li><span>/* go on with normal index.php operations */</span><span> </span></li><li><span>?> </span></span></li></ol>
<ol><li><span><span>$ php -S localhost:8000 router.php </span></span></li></ol>
This built-in web server can recognize some standard MIME type resources, and their extensions are : .css, .gif, .htm, .html, .jpe, .jpeg, .jpg, .js, .png, .svg, and .txt. Support for .htm and .svg extensions is only supported after PHP 5.4.4.
Example #5 Handling unsupported file types
If you want the web server to correctly handle unsupported MIME file types, do this:
<ol><li><span><span><?php </span></span></li><li><span>// router.php </span><span> </span></li><li><span>$path</span><span> = </span><span>pathinfo</span><span>(</span><span>$_SERVER</span><span>[</span><span>"SCRIPT_FILENAME"</span><span>]); </span></li><li><span>if</span><span> (</span><span>$path</span><span>[</span><span>"extension"</span><span>] == </span><span>"ogg"</span><span>) { </span></li><li><span>header(</span><span>"Content-Type: video/ogg"</span><span>); </span></li><li><span>readfile(</span><span>$_SERVER</span><span>[</span><span>"SCRIPT_FILENAME"</span><span>]); </span></li><li><span>} </span></li><li><span>else</span><span> { </span></li><li><span>return</span><span> FALSE; </span></li><li><span>} </span></li><li><span>?> </span></span></li></ol>
<ol><li><span><span>$ php -S localhost:8000 router.php </span></span></li></ol>
If you want to access the built-in web server remotely, Your startup command needs to be changed to the following:
Example #6 Remote access to this built-in web server
<ol><li><span><span>$ php -S 0.0.0.0:8000 </span></span></li></ol>
So you can remotely access this built-in web server through port 8000
The above has introduced the PHP 54 built-in Web server, including the content of the Web server. I hope it will be helpful to friends who are interested in PHP tutorials.