PHP is a scripting language that requires the PHP interpreter to parse and run PHP files. When PHP is used as a CGI service Web request, it needs to be embedded in some kind of Web server, most commonly integrated into Apache or IIS, that is, before using PHP, you need to install Apache or IIS, and correctly configure them and PHP integrated parameters. Although this configuration is already very prescriptive and the documentation is very rich, we often have problems installing Apache and PHP integration, and sometimes we just want to test a simple PHP feature and don't want to install and start the Apache service for this.
But according to official documentation, this built-in Web server is only available for development testing and is not recommended for use in production environments. Because this server accepts processing requests in sequence, it cannot be processed concurrently.
This built-in Web server is very handy to use, and you just need to execute the following command:
Then you can access it. When this is started, the default Web service directory is the current directory where the command is executed, and if you do not want to use the current directory, you need to specify it with the-t parameter.
Example #1 starting a Web server
- $ CD ~/public_html
Terminal Output Information:
- 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
When the http://localhost:8000/and http://localhost:8000/myscript.html addresses are requested, the terminal outputs information similar to the following:
- 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 10:48:48]:: 1:39144 get/favicon.ico-request Read
- [Thu Jul 10:48:50]:: 1:39146 GET/-Request Read
- [Thu Jul 10:48:50]:: 1:39147 get/favicon.ico-request Read
- [Thu Jul 10:48:52]:: 1:39148 get/myscript.html-request Read
Example #2 the root directory of the specified document when starting the Web server
- $ CD ~/public_html
Terminal Display information:
- 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
If you attach a PHP script file after the startup command line, the file will be treated as a "router" script. This script will be responsible for all HTTP requests, and if the script returns False when it executes, the requested resource will return normally. If it is not false, the content displayed in the browser will be the result of this script.
Example #3 using router scripts
In this example, the request for the picture returns the corresponding image, but the request to the HTML file displays "Welcome to PHP":
-
- if (Preg_match('/\.:p ng|jpg|jpeg|gif) $/'$_server[" Request_uri "])) {
- return false;
- Else {
- echo"
Welcome to PHP
";
- }
Example #4 determine if you are using the built-in Web server
The different behavior of the same PHP router script in the built-in Web server and in the production server is adjusted by program judgment:
-
- If' cli-server ') {
- /* route static assets and return false */
- }
- /* Go on with normal index.php operations */
This built-in Web server recognizes a number of standard MIME-type resources, and their extensions are:. css,. gif,. htm,. html,. JPE,. jpeg,. jpg,. js,. png,. svg,. Extension of. htm and. svg to support is supported after PHP 5.4.4.
Example #5 handling unsupported file types
If you want this Web server to handle the MIME file types that are not supported correctly, do this:
- !--? php
- // router.php
- $path = pathinfo ( $_server [ "Script_filename" ]);
- if ( $path [ "extension" ] = = "ogg" ) {
- header ( "Content-type:video/ogg" );
- ReadFile ( $_server [ "Script_filename" ] );
- }
- else {
- return FAL SE;
- }
- ?>
If you want to be able to access this built-in Web server remotely, your boot command needs to be changed to the following:
Example #6 remote access to this built-in Web server
This allows you to access the built-in Web server remotely via port 8000.
The above describes the PHP 54 built-in Web server, including Web server aspects of the content, I hope the PHP tutorial interested in a friend helpful.