In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the detailed introduction of PHP7 kernel CGI and FastCGI", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the detailed introduction of PHP7 kernel CGI and FastCGI".
CGI: a protocol for data exchange between Web Server and Web Application.
FastCGI: with CGI, it is a communication protocol, but it does some optimization in efficiency than CGI.
PHP-CGI: is the interface program of PHP (Web Application) to the CGI protocol provided by Web Server.
PHP-FPM: it is the interface program of PHP (Web Application) to FastCGI protocol provided by Web Server. In addition, it also provides some relatively intelligent task management.
CGI workflow
1. If the client requests index.html, then Web Server will find the file in the file system and send it to the browser, where static data is distributed.
two。 When Web Server receives the request for index.php, it starts the corresponding CGI program, which is the parser of PHP. Next, the PHP parser parses the php.ini file, initializes the execution environment, then processes the request, returns the processed result in the format specified by CGI, exits the process, and Web server returns the result to the browser.
FastCGI workflow
1. If the client requests index.html, then Web Server will find the file in the file system and send it to the browser, where static data is distributed.
two。 When the Web Server receives the index.php request, the FastCGI program (FastCGI initializes the execution environment at startup, and each CGI process pool shares the execution environment for each CGI process) selects a CGI process in the CGI process pool to process the request, then returns the processed result in the specified format of the CGI, and continues to wait for the next request.
Basic implementation of PHP-FPM
The implementation of 1.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 out multiple sub-processes (work), each of which has its own accept request. The processing of the child process is very simple. It blocks on the accept after startup, starts to read the request data after the request arrives, starts processing after reading, and then returns. During this period, it will not receive other requests. That is to say, the child process of PHP-FPM can only respond to one request at a time, and the next request will not be accept until the request has been processed.
There is no direct communication between the master process of 2.PHP-FPM and the worker process. Master obtains the information of the worker process through shared memory, such as the current status of the worker process, the number of requests processed, and so on. When the master process wants to kill a worker process, it notifies the worker process by sending a signal.
3.PHP-FPM can listen on multiple ports at the same time, each port corresponds to a worker pool, and each pool corresponds to multiple worker processes
Worker workflow
1. Wait for a request: the worker process blocks the fcgi_accept_request () waiting for the request
two。 Parse request: when the fastcgi request arrives, it is received by worker, and then begins to receive and parse the request data until the request data arrives completely
3. Request initialization: execute php_request_startup (), which calls each extension's: PHP_RINIT_FUNCTION ()
4. Compilation, execution: php_execute_script () completes the compilation and execution of the PHP script
5. Close the request: execute php_request_shutdown () after the request is completed, which calls the: PHP_RSHUTDOWN_FUNCTION () of each extension, and then proceed to step (1) to wait for the next request.
Master process management
1.static: this method is relatively simple. At startup, master configures fork according to pm.max_children to produce a corresponding number of worker processes, that is, the number of worker processes is fixed.
2.dynamic: dynamic process management. First, initialize a certain number of worker according to pm.start_servers when fpm starts. If master finds that the number of idle worker is lower than the number of pm.min_spare_servers configuration (indicating that there are more requests and worker cannot handle it), it will fork worker the process, but the total number of worker cannot exceed pm.max_children. If master finds that the number of free worker exceeds pm.max_spare_servers, it will kill some worker to avoid taking up too much resources. Master uses these four values to control the number of worker.
3.ondemand: this method is rarely used. The worker process is not assigned at startup, and the master process fork worker process is notified when there is a request. The total number of worker does not exceed pm.max_children. The worker process will not exit immediately after processing, and 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 is the main event of the process management implementation. Master starts a timer, which is triggered every 1 second, which is mainly used for worker management in dynamic and ondemand modes. Master will regularly check the number of worker processes in each worker pool, and use this timer to control the number of worker.
3.fpm_pctl_heartbeat (): this event is used to limit the maximum time taken by worker to process a single request. There is a configuration item for request_terminate_timeout in php-fpm.conf. If the total time taken by worker to process a request exceeds this value, master will send a kill-TERM signal to the worker process to kill the worker process. This configuration unit is seconds. The default value is 0, which means that this mechanism is disabled.
4.fpm_pctl_on_socket_accept (): the event that a new request that master listens to in ondemand mode arrives. Because in ondemand mode, worker is not pre-created when fpm starts, and child processes are generated only when there is a request, so you need to notify the master process when the request arrives.
At this point, I believe you have a deeper understanding of the "detailed introduction of PHP7 kernel CGI and FastCGI". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.