PHP 54 built-in Web Server Web server ranking Web server principle Simple Web server

Source: Internet
Author: User
Tags mime file
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

 
  
  
  1. $ CD ~/public_html

Terminal Output Information:

 
  
  
  1. PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
  2. Listening on localhost:8000
  3. 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:

 
  
  
  1. PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
  2. Listening on localhost:8000
  3. Document Root is/home/me/public_html
  4. Press Ctrl-c to quit.
  5. [Thu Jul 10:48:48]:: 1:39144 get/favicon.ico-request Read
  6. [Thu Jul 10:48:50]:: 1:39146 GET/-Request Read
  7. [Thu Jul 10:48:50]:: 1:39147 get/favicon.ico-request Read
  8. [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

 
  
  
  1. $ CD ~/public_html

Terminal Display information:

 
  
  
  1. PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011
  2. Listening on localhost:8000
  3. 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":

 
  
  
  1. if (Preg_match('/\.:p ng|jpg|jpeg|gif) $/'$_server[" Request_uri "])) {
  2. return false;
  3. Else {
  4. echo"

    Welcome to PHP

    ";
  5. }
 
  
  

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:

 
  
  
  1. If' cli-server ') {
  2. /* route static assets and return false */
  3. }
  4. /* 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:

 
  
  
  1. !--? php
  2. // router.php
  3. $path = pathinfo ( $_server [ "Script_filename" ]);
  4. if ( $path [ "extension" ] = = "ogg" ) {
  5. header ( "Content-type:video/ogg" );
  6. ReadFile ( $_server [ "Script_filename" ] );
  7. }
  8. else {
  9. return FAL SE;
  10. }
  11. ?>
 
  
  

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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.