How PHP handles the Web request process

Source: Internet
Author: User
Tags autoload fpm
In the PHP world there are many development frameworks, such as Laravel, thinkphp, etc., but no matter what the general framework, they are in the same mode of processing Web requests, this article first elaborated the PHP Development Web application Basic architecture, The processing flow of laravel and thinkphp in processing Web Requests is then analyzed separately.

PHP's basic architecture for developing Web applications

When PHP develops a Web application, the request needs to point to the specific portal file. Webserver is a content distributor, he accepts the user's request, if the request is a CSS, JS and other static files, webserver will find this file, and then sent to the browser, if the request is/ index.php, according to the configuration file, webserver know that this is not a static file, you need to find a PHP parser to handle, then he will simply handle the request to the PHP parser.

Webserver will send the requested URL, data, Http header and other information to the PHP parser based on the CGI protocol. The PHP parser then parses the php.ini file, initializes the execution environment, processes the request, and returns the processed result in the CGI-formatted format, exiting the process. The Web server then returns the results to the browser. The entire process is as shown.

FastCGI

The PHP parser here is the implementation of the CGI protocol program, each request comes when he will parse the php.ini file, initialize the execution environment, which causes the PHP parser performance is low, so there is a modified CGI version of the fastcgi. FastCGI is a language-agnostic protocol used to communicate programs (such as PHP, Python, Java) and Web servers (Apache2, Nginx), and in theory any program written in any language can provide Web services through fastcgi. It is characterized by the dynamic allocation of processing processes to requests to achieve efficiency gains, and most FASTCGI implementations maintain a process pool. FastCGI initiates a master process, parses the configuration file, initializes the execution environment, and then starts multiple worker processes. When the request comes in, the master process passes the request to a worker process and then immediately accepts the next request. And when the worker process is not enough, master can pre-boot several worker processes in accordance with the configuration, although the idle worker process is too long and automatically shuts down, which improves performance and saves system resources. The entire process fastcgi plays a role in managing the CGI process.

php-fpm

PHP-FPM is a program specifically for PHP implementation of the FASTCGI protocol, it is actually a PHP fastcgi process Manager, is responsible for managing a process pool, invoking the PHP parser to handle requests from the Web server. PHP-FPM can smooth over the modification of the php.ini file.

Create a new helloworld.php file and write the following code

   <?php  echo "HelloWorld,";      echo "This is my first php script.";  Echophpinfo ();? >

After configuring the PHP runtime environment such as Webserver and PHP-FPM, you can get the output directly by accessing the file in the browser.

PHP-based web framework

The PHP web Framework is

A tool that enables developers to quickly develop a common feature wrapper for PHP development based on a pattern

Its main tasks include:

Code reuse: Defines the placement and loading rules for packages, classes, functions, and recommends the direct integration of composer and its autoload features.

Request Distribution Management: This is the route, the rest wind framework like rewrite, the simple point of the framework mainly through the parameters to locate the module and the method is located.

Profile Management: Load and load configuration data dynamically

Error and Exception management: Exception snapping, error logging, and error code specification.

Layout and template engine: How to plan page layouts, how widgets are reused, how Ajax pages are combined, how to redirect expired sessions, how data and templates are rendered HTML, whether to compress and set expiration headers.

Database: How to integrate controller, what kind of driver to support, consider the extensibility of master-slave separation, and whether to use ORM

As the best programming voice in the World, PHP is widely used in web development. Because of its syntax and C-like, with a very flat learning curve, more and more people use PHP for rapid development of web products. The PHP world also has a lot of development frameworks, such as Laravel, thinkphp, etc., but no matter what the general framework, they are in the same mode of processing Web requests, this article first elaborated the PHP Development Web application Basic architecture, The processing flow of laravel and thinkphp in processing Web Requests is then analyzed separately.

PHP's basic architecture for developing Web applications

When PHP develops a Web application, the request needs to point to the specific portal file. Webserver is a content distributor, he accepts the user's request, if the request is a CSS, JS and other static files, webserver will find this file, and then sent to the browser, if the request is/ index.php, according to the configuration file, webserver know that this is not a static file, you need to find a PHP parser to handle, then he will simply handle the request to the PHP parser.

Webserver will send the requested URL, data, Http header and other information to the PHP parser based on the CGI protocol. The PHP parser then parses the php.ini file, initializes the execution environment, processes the request, and returns the processed result in the CGI-formatted format, exiting the process. The Web server then returns the results to the browser. The entire process is as shown.

FastCGI

The PHP parser here is the implementation of the CGI protocol program, each request comes when he will parse the php.ini file, initialize the execution environment, which causes the PHP parser performance is low, so there is a modified CGI version of the fastcgi. FastCGI is a language-agnostic protocol used to communicate programs (such as PHP, Python, Java) and Web servers (Apache2, Nginx), and in theory any program written in any language can provide Web services through fastcgi. It is characterized by the dynamic allocation of processing processes to requests to achieve efficiency gains, and most FASTCGI implementations maintain a process pool. FastCGI initiates a master process, parses the configuration file, initializes the execution environment, and then starts multiple worker processes. When the request comes in, the master process passes the request to a worker process and then immediately accepts the next request. And when the worker process is not enough, master can pre-boot several worker processes in accordance with the configuration, although the idle worker process is too long and automatically shuts down, which improves performance and saves system resources. The entire process fastcgi plays a role in managing the CGI process.

php-fpm

PHP-FPM is a program specifically for PHP implementation of the FASTCGI protocol, it is actually a PHP fastcgi process Manager, is responsible for managing a process pool, invoking the PHP parser to handle requests from the Web server. PHP-FPM can smooth over the modification of the php.ini file.

Create a new helloworld.php file and write the following code

<?php  echo "HelloWorld,";      echo "This is my first php script.";  Echophpinfo ();? >

After configuring the PHP runtime environment such as Webserver and PHP-FPM, you can get the output directly by accessing the file in the browser.

PHP-based web framework

The PHP web Framework is

A tool that enables developers to quickly develop a common feature wrapper for PHP development based on a pattern

Its main tasks include:

Code reuse: Defines the placement and loading rules for packages, classes, functions, and recommends the direct integration of composer and its autoload features.

Request Distribution Management: This is the route, the rest wind framework like rewrite, the simple point of the framework mainly through the parameters to locate the module and the method is located.

Profile Management: Load and load configuration data dynamically

Error and Exception management: Exception snapping, error logging, and error code specification.

Layout and template engine: How to plan page layouts, how widgets are reused, how Ajax pages are combined, how to redirect expired sessions, how data and templates are rendered HTML, whether to compress and set expiration headers.

Database: How to integrate controller, what kind of driver to support, consider the extensibility of master-slave separation, and whether to use ORM

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.