Concepts and differences between CGI, FastCGI and php-fpm

1. CGI

CGI (Common Gateway Interface), a common gateway interface, is an interface standard for transferring information between a Web server and an external application (CGI program). Through the CGI interface, the Web server can obtain the information submitted by the client, and transfer it to the CGI program on the server for processing, and finally return the result to the client.

That is, CGI is actually an interface standard. What we usually call CGI refers to a CGI program, that is, a program that implements the CGI interface standard.

As long as a language has standard input, output and environment variables, such as perl, php, C, etc., it can be used to write CGI programs.

How CGI programs work:

The web server generally only processes static file requests (such as jpg, htm, html). If a dynamic script request (such as php) is encountered, the main process of the web server will fork a new process to start the CGI program, which means that the Dynamic script requests are handed over to CGI programs for processing. Starting a CGI program requires a process, such as reading configuration files, loading extensions, etc. After the CGI program is started, it will parse the dynamic script, and then return the result to the Web server, and finally the Web server returns the result to the client, and the fork process will also be closed.

In this way, every time a user requests a dynamic script, the Web server must re-fork a new process to start the CGI program. The CGI program handles the dynamic script, and the process is closed after processing.

This way of working is very inefficient.

2. Web server built-in module

Later, a more efficient way appeared: the built-in module of the web server. For example, apache's mod_php module. Make the php interpreter into a module and load it into the apache server.

In this way, when the apache server starts, it will start the php module at the same time. When the client requests a php file, the apache server does not need to fork a new process to start the php interpreter, but directly hands the php file to the running php module for processing. Obviously, in this way, the efficiency will be higher.

由于在apache服务器启动时,才会读取php的配置文件,加载php模块,在apache的运行过程中 ,不会再重新读取php的配置文件。所以,每次我们修改了php的配置文件后,必须重启apache,新的php配置文件才会生效。

3. FastCGI

FastCGI就像是一个常驻(long-live)型的CGI程序,它可以一直运行着。FastCGI程序也可以和Web服务器分别部署在不同的主机上,它还可以接受来自其他Web服务器的请求。

FastCGI也是语言无关的。其主要行为是将CGI解释器进程保持在内存中并因此获得高效的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因。

FastCGI是一种进程管理工具,它可以在内存中管理CGI进程。

FastCGI进程管理器需要单独启动。启动FastCGI后,会生成一个FastCGI主进程和多个子进程(子进程其实就是CGI解释器进程)。

当客户端请求Web服务器上的动态脚本时,Web服务器会将动态脚本通过TCP协议交给FastCGI主进程,FastCGI主进程根据情况,安排一个空闲的子进程来解析动态脚本,处理完成后将结果返回给Web服务器,Web服务器再将结果返回给客户端。该客户端请求处理完毕后,FastCGI子进程并不会随之关闭,而是继续等待主进程安排工作任务。

由此可知,FastCGI的工作效率是非常高的。

4. php-fpm

fpm是FastCGI Process Manager的缩写,那么,fpm就是FastCGI进程管理器的简称。

php-fpm就是php中的FastCGI进程管理器。

对于php5.3之前的版本来说,php-fpm是一个第三方的补丁包,旨在将FastCGI进程管理整合进PHP包中。

在php5.3之后的版本中,php-fpm不再是第三方的包,它已经被集成到php的源码中了。php-fpm提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以php-fpm被PHP官方收购了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324620536&siteId=291194637