search
HomeBackend DevelopmentPHP7PHP7 kernel analysis of CGI and FastCGI

CGI: It is a protocol for data exchange between Web Server and Web Application.

FastCGI: Same as CGI, it is a communication protocol, but it has some optimizations in efficiency than CGI.

PHP-CGI: It is the interface program of PHP (Web Application) to the CGI protocol provided by Web Server.

PHP-FPM: It is an interface program for the FastCGI protocol provided by PHP (Web Application) to the Web Server. In addition, it also provides relatively intelligent task management

CGI workflow

1. If the client requests index.html, then the Web Server will find this file in the file system and send it to the browser. What is distributed here is static data.

2. When the Web Server receives the index.php request, it will start the corresponding CGI program, which is the PHP parser. Next, the PHP parser will parse the php.ini file, initialize the execution environment, then process the request, return the processed result in the format specified by CGI, exit the process, and the Web server will return the result to the browser.

FastCGI workflow

1. If the client requests index.html, then the Web Server will find this file in the file system and send it to the browser. What is distributed here is static data.

2. When the Web Server receives the index.php request, the FastCGI program (FastCGI initializes the execution environment when it starts, and each CGI process pool shares the execution environment) is in the CGI process pool. Select a CGI process to process the request, return the processed result in the format specified by CGI, and continue to wait for the next request.

Basic implementation of PHP-FPM

1. The implementation of PHP-FPM is to create a master process, create a worker pool in the master process and let it listen to the socket, and then Fork multiple sub-processes (work), and each of these sub-processes accepts the request. The processing of the sub-process is very simple. It blocks on accept after startup. When a request arrives, it starts to read the request data. After the reading is completed, it starts processing and then Return, no other requests will be received during this period, which means that the sub-process of PHP-FPM can only respond to one request at the same time. Only after this request is processed, the next request will be accepted

2. There is no direct communication between the PHP-FPM master process and the worker process. The master obtains the information of the worker process through shared memory, such as the current status of the worker process, the number of processed requests, etc. When the master process wants to kill a worker process, Notify the worker process by sending a signal.

3.PHP-FPM can monitor multiple ports at the same time. Each port corresponds to a worker pool, and each pool corresponds to multiple worker processes

PHP7 kernel analysis of CGI and FastCGI

Worker workflow

1. Waiting for the request: The worker process is blocked in fcgi_accept_request() waiting for the request;

2. Parsing the request: After the fastcgi request arrives, it is received by the worker. Then start receiving and parsing the request data until the request data is completely arrived;

3. Request initialization: Execute php_request_startup(), this stage will call each extension: PHP_RINIT_FUNCTION();

4 . Compilation and execution: Compilation and execution of PHP scripts are completed by php_execute_script();

5. Close the request: After the request is completed, execute php_request_shutdown(). At this stage, each extension will be called: PHP_RSHUTDOWN_FUNCTION(), and then Enter step (1) and wait for the next request.

Master process management

1.static: This method is relatively simple. At startup, the master forks out the corresponding number of worker processes according to the pm.max_children configuration, that is, worker The number of processes is fixed

2.dynamic: Dynamic process management, first initialize a certain number of workers according to pm.start_servers when fpm starts. During operation, if the master finds that the number of idle workers is lower than the pm.min_spare_servers configuration number (indicating that there are too many requests and the worker cannot handle them), the worker process will be forked, but the total number of workers cannot exceed pm.max_children. If the master finds that the number of idle workers exceeds pm.max_spare_servers (indicating that there are too many idle workers) ) will kill some workers to avoid taking up too many resources. The master uses these 4 values ​​​​to control the number of workers

3.ondemand: This method is generally rarely used and does not allocate worker processes at startup. Wait until there is a request and then notify the master process to fork the worker process. The total number of workers does not exceed pm.max_children. The worker process will not exit immediately after the processing is completed. It will exit when the idle time exceeds pm.process_idle_timeout

PHP-FPM Event Manager

1.sp[1] Pipeline readable event: This event is used by master to process signals

2.fpm_pctl_perform_idle_server_maintenance_heartbeat(): This It is the main event in the implementation of process management. The master starts a timer, which is triggered every 1s. It is mainly used for worker management in dynamic and ondemand modes. The master will regularly check the number of worker processes in each worker pool and implement it through this timer. Control of the number of workers

3.fpm_pctl_heartbeat(): This event is used to limit the maximum time it takes for a worker to process a single request. There is a request_terminate_timeout configuration item in php-fpm.conf. If the total time it takes for a worker to process a request exceeds this value, then The master will send the kill -TERM signal to the worker process to kill the worker process. The unit of this configuration is seconds. The default value is 0, which means turning off this mechanism.

4.fpm_pctl_on_socket_accept(): The new value monitored by the master in ondemand mode The event of request arrival, because in ondemand mode, fpm will not pre-create workers when it starts, and a child process will be generated only when there is a request, so the master process needs to be notified when the request arrives

The above is the detailed content of PHP7 kernel analysis of CGI and FastCGI. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment