[Modern PHP] Chapter II new features 7 built-in HTTP server

Source: Internet
Author: User
Tags php web server
[Modern PHP] Chapter II new features seven built-in HTTP servers

Built-in HTTP server


Did you know that PHP has a built-in Web server starting from 5.4.0? For PHP developers who only know to use Apache or Nginx to preview PHP pages, this is another gem that has not been explored. Although you can't use PHP's built-in Web server in a production environment, This feature is a perfect tool for local development.


Whether I'm writing PHP code or not, I use PHP's built-in Web server every day anyway. I'll use it to preview the Laravel and Slim framework (the author of the framework, the author of the book, Josh Lockhart) application, which is also used when building a website using the Drupal Framework's content management system. Use it to preview static HTML and CSS even when constructing a prototype page.


Remember, PHP's built-in server is a Web server. It only uses the HTTP protocol, and it can return static resources in addition to PHP files. We can do a good job of developing and previewing HTML pages locally without using Mamp, Wamp, or other heavyweight Web servers.


Start the server


Starting a PHP Web server is simple. Open your terminal program, enter your project's document root directory and execute the following command:

Php-s localhost:4000


This command launches a new PHP Web server that we can access via localhost. It listens on port 4000. Your current working directory is the Web server's document root directory.


Open your browser and visit http://localhost:4000 to preview your app. Because you use a Web browser to browse your own applications, each HTTP request is recorded in your terminal program and output directly from the terminal, which detects whether your application will be 400 or 500 errors.


Sometimes you will need to allow other devices to access the PHP Web servers in your local environment (for example, using your ipad or a local Windows virtual machine for previewing). To implement this requirement, you can have the PHP Web server use 0.0.0.0 instead of localhost to listen for requests from all network connections.

Php-s 0.0.0.0:4000


When you are ready to stop the PHP Web server, close the terminal program directly or press CTRL + C.


Configure the server


An application that corresponds to a php ini profile is a very common process, especially if your application has special needs for memory usage, file uploads, debugging, or byte-code caching. When using a PHP built-in server, we can use the-c parameter to tell the server to use a specified INI file:

Php-s localhost:8000-c App/config/php.ini


It's a good idea to put the custom INI file in the root of the application, and even use versioning to save the INI file for sharing with other developers on the development team.


Routing scripts


Unlike Apache or Nginx, The PHP built-in server has an obvious flaw: it does not support the. htaccess file. So many popular PHP frameworks use a front-end controller that is cumbersome to use on a PHP built-in server.


The front-end controller is a separate PHP file, and all HTTP requests are forwarded to it (via the. htaccess file or the rewrite rule). The front-end controller file is responsible for routing the request and distributing it to the corresponding PHP code. This common pattern is used by Symfony and other popular PHP frameworks.


The PHP built-in server uses routing scripts to compensate for this flaw slightly. The routing script is executed before each HTTP request. If the script returns false, the static resource corresponding to the current HTTP request address is returned, whereas the content returned by the script is returned to the browser as an HTTP return. In other words, your routing script is actually copying the functionality of the. htaccess file.


Using a routing script is simply a matter of sending the file address of a PHP script to the PHP built-in server's startup command as a Eucalyptus tree:

Php-s localhost:8000 router.php


Determine the request from the built-in server


Sometimes it is useful to be able to differentiate between PHP scripts that are accessed from a PHP built-in server or a traditional Web server like Apache and Nginx. Perhaps you need to add a dedicated HTTP header to Nginx (for example, Status:) but not on the PHP Web server. You can use the Php_sapi_name () function to determine the PHP Web server. This function returns the string Cli-server when the script you are currently requesting is from a PHP built-in server:

!--? phpif (php_sapi_name () = = = and #39; cil-server& #39;) {//php built-in Web server} else {/other W EB server}  
disadvantage


php built-in Web server is not available in the production environment. It is designed for local development only. If you're using a PHP built-in server on your product server, you'll be faced with a lot of disappointing users and flood-like notifications from Pingdom.


  • built-in server performance is It can only process one request at a time, and other requests will be blocked by the block. If a PHP file is stuck in a slow query or a remote API return, your Web app will not respond.
  • built-in servers support only limited MIME types
  • The built-in server uses routing scripts to limit URL rewriting. You need Apache or Nginx for more advanced url rewriting operation

  • 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.