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 the interface program of 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
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 The worker receives, then starts to receive and parse 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. Compile, Execution: The compilation and execution of the PHP script is completed by php_execute_script();
5. Shut down the request: After the request is completed, execute php_request_shutdown(). This stage will call each extension: PHP_RSHUTDOWN_FUNCTION(), and then enter step (1) to wait. 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-1) CGI and FastCGI. For more information, please follow other related articles on the PHP Chinese website!

在php5中,我们可以使用fsockopen()函数来检测TCP端口。这个函数可以用来打开一个网络连接和进行一些网络通信。但是在php7中,fsockopen()函数可能会遇到一些问题,例如无法打开端口、无法连接到服务器等。为了解决这个问题,我们可以使用socket_create()函数和socket_connect()函数来检测TCP端口。

php7.0安装mongo扩展的方法:1、创建mongodb用户组和用户;2、下载mongodb源码包,并将源码包放到“/usr/local/src/”目录下;3、进入“src/”目录;4、解压源码包;5、创建mongodb文件目录;6、将文件复制到“mongodb/”目录;7、创建mongodb配置文件并修改配置即可。

解决 PHP 7.0 中插件未显示已安装问题的方法:检查插件配置并启用插件。重新启动 PHP 以应用配置更改。检查插件文件权限,确保其正确。安装丢失的依赖项,以确保插件正常运行。如果其他步骤均失败,则重建 PHP。其他可能原因包括插件版本不兼容、加载错误版本或 PHP 配置问题。

fastcgi:fastcgi是从cgi发展改进而来的。传统cgi接口方式的主要缺点是性能很差,因为每次http服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后结果被返回给http服务器。这在处理高并发访问时,几乎是不可用的。另外传统的cgi接口方式安全性也很差,现在已经很少被使用了。fastcgi接口方式采用c/s结构,可以将http服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。当http服务器每次遇到动态程序时,可以将其直接交付给fastcg

php7.0安装部署的方法:1、到PHP官网下载与本机系统对应的安装版本;2、将下载的zip文件解压到指定目录;3、打开命令行窗口,在“E:\php7”目录下运行“php -v”命令即可。

PHP服务器环境常见的解决方法包括:确保已安装正确的PHP版本和已复制相关文件到模块目录。临时或永久禁用SELinux。检查并配置PHP.ini,确保已添加必要的扩展和进行正确设置。启动或重启PHP-FPM服务。检查DNS设置是否存在解析问题。

PHP8相较于PHP7在性能、新特性和语法改进、类型系统、错误处理和扩展等方面都有一些优势和改进。然而,选择使用哪个版本要根据具体的需求和项目情况来决定。详细介绍:1、性能提升,PHP8引入了Just-in-Time(JIT)编译器,可以提高代码的执行速度;2、新特性和语法改进,PHP8支持命名参数和可选参数的声明,使得函数调用更加灵活;引入了匿名类、属性的类型声明等等。

如何在系统重启后自动设置unixsocket的权限每次系统重启后,我们都需要执行以下命令来修改unixsocket的权限:sudo...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

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

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.

Atom editor mac version download
The most popular open source editor