PHP 5.4 Built-in 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:

    1. $ php-s localhost:8000

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 Start Web Server

    1. $ CD ~/public_html
    2. $ php-s localhost:8000

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
    4. Press Ctrl-c to quit

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
    9. [Thu Jul 10:48:52]:: 1:39149 get/favicon.ico-request Read

Example #2 Start Web the root directory of the specified document when the server

    1. $ CD ~/public_html
    2. $ php-s localhost:8000-t foo/

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
    4. Press Ctrl-c to quit

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. <?php
    2. router.php
    3. if (Preg_match ('/\. (?:p ng|jpg|jpeg|gif) $/', $_server["Request_uri"])) {
    4. return false; Serve the requested resource as-is.
    5. } Else {
    6. echo "<p>welcome to Php</p>";
    7. }
    8. ?>
    9. $ php-s localhost:8000 router.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:

    1. <?php
    2. router.php
    3. if (Php_sapi_name () = = ' Cli-server ') {
    4. /* route static assets and return false */
    5. }
    6. /* Go on with normal index.php operations */
    7. ?>
    8. $ php-s localhost:8000 router.php

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 FALSE;
    10. }
    11. ?>
    12. $ php-s localhost:8000 router.php

If you want to be able to access this built-in foot Bath Web server remotely, your boot command needs to be changed to the following:

Example #6 remote access to this built-in Web Server

    1. $ php-s 0.0.0.0:8000

This allows you to access the built-in Web server remotely via port 8000.

PHP 5.4 Built-in Web server

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.