This article describes PHP5.4 built-in web servers in detail. the built-in Web servers are only used for development and testing and are not recommended in the production environment, for more information, see the following section. PHP is a scripting language that requires a PHP interpreter to analyze and run PHP files. When PHP is used as a CGI service Web request, it needs to be embedded into a Web server. The most common thing is to integrate it into Apache or IIS. that is to say, before using PHP, you need to install Apache or IIS and correctly configure the parameters they integrate with PHP. Although this configuration is already very standard and has rich documentation, we often encounter problems when installing Apache and PHP integration. In addition, sometimes we only want to test a simple PHP feature, you do not want to install or start the Apache service.
However, according to the official documentation, this built-in Web server is only available for development and testing and is not recommended in the production environment. Because this server is executed sequentially when processing requests, it cannot be processed concurrently.
This built-in web server is very convenient to use. you only need to execute the following command:
$ Php-S localhost: 8000
Then you can access it. In this way, the default web Service Directory is the current directory for executing commands. 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
$ Cd ~ /Public_html
$ Php-S localhost: 8000
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
Press Ctrl-C to quit
After the http: // localhost: 8000/and http: // localhost: 8000/myscript.html addresses are requested, the terminal outputs the following information:
PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011Listening on localhost:8000Document root is /home/me/public_htmlPress 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
Example #2Specify the root directory of the document when starting the web server
$ Cd ~ /Public_html
$ Php-S localhost: 8000-t foo/
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
Press Ctrl-C to quit
If you append a php script file to the startup command line, the file will be treated as a "router" script. This script will be responsible for all HTTP requests. if FALSE is returned when the script is executed, the requested resource will be returned normally. If it is not FALSE, the content displayed in the browser will be generated by this script.
Example #3Use vro script
In this example, the corresponding image will be returned for the image request, but the request to the HTML file will display "Welcome to PHP ":
<?php// router.phpif (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {return false; // serve the requested resource as-is.} else {echo "Welcome to PHP
";}?>
$ Php-S localhost: 8000 router. php
Example #4 check whether a built-in web server is used
Adjust the different behaviors of the same PHP router script on the built-in Web server and on the production server through program judgment:
<?php// router.phpif (php_sapi_name() == 'cli-server') {/* route static assets and return false */}/* go on with normal index.php operations */?>
$ Php-S localhost: 8000 router. php
This built-in web server can recognize some standard MIME-type resources. Their extensions include :. css ,. gif ,. htm ,. html ,. jpe ,. jpeg ,. jpg ,. js ,. png ,. svg, and. txt. Extension to .htm and. svg is supported only after PHP 5.4.4.
Example #5Process unsupported file types
If you want the Web server to correctly process unsupported MIME file types, do the following:
<?php// router.php$path = pathinfo($_SERVER["SCRIPT_FILENAME"]);if ($path["extension"] == "ogg") {header("Content-Type: video/ogg");readfile($_SERVER["SCRIPT_FILENAME"]);}else {return FALSE;}?>$ php -S localhost:8000 router.php
If you want to remotely access this built-in web server, change your startup command to the following:
Example #6Remote access to this built-in Web server
$ Php-S 0.0.0.0: 8000
In this way, you can remotely access this built-in web server through port 8000.
The above is all the content of this article. I hope it will be helpful to everyone's learning, and I hope you can support your own home.