Built-in HTTP Server "modern PHP"

Source: Internet
Author: User

Directory

    • Start the server
    • Configure the server
    • Router Script
    • Determine if the server is built-in

PHP5.4.0, PHP has a built-in Web server. It's a great tool for local development, and it's easy to debug locally without having to install Wamp, Xamp, or da Dah's Web server.

Start the server

Go to the root of the project and execute the command

php -S localhost:4000   #地址:监听端口

Or specify the site root directory directly

php -S localhost:4000 -t D:\website   

When the browser opens localhost:4000, you can go to the Web browsing app.

To stop the PHP Web server, close the terminal app or press CTRL + C.

Configure the server

Specifies the initialization file. Special requirements for memory usage, file upload, parsing, or bytecode caching are available using a proprietary PHP initial configuration file.

php -S localhost:4000 -c app/config/php/ini
Router Script

The built-in server cannot route parsing, forwarding, redirecting, etc., and does not support. htaccess files. It is therefore difficult to use common front-end controllers in most popular PHP frameworks.

Use router scripting to compensate for this missing feature. Execute this router script before each HTTP request is processed. It works just like the. htaccess file.

php -S localhost:4000 router.php

The router script, such as a request for a picture, returns the corresponding picture, but the request to the HTML file displays "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 "<p>Welcome to PHP</p>";}?>
Determine if the server is built-in
<?php    if(php_sapi_name() === 'cli_server'){        //PHP内置服务器    }else{        //其他Web服务器    }

Disadvantage: Can not be used in production environment, can only be used for local development.

    • Poor performance. Only one request can be processed at a time;
    • Support for a small number of media types;
    • Support for a small number of URL rewrite rules.

Built-in HTTP Server "modern PHP"

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.