天天看点

【深度】一文读懂cgi,fast-cgi,php-cgi,php-fpm

这几个概念,总是不是太清晰,索性写篇文章整理一下,让模糊的清晰起来,让清楚的更清楚。

一、是什么

cgi,fastcgi是协议,协议是规定数据传输格式的

php-cgi,php-fpm是程序,程序完成某一种功能的代码块

CGI

Common Gateway Interface, is a prootocal for HTTP server to contact with programs on other servers, which can be used into any languages with standard input, standard output and environmental variables, such as PHP, Perl, or Tcl.

FastCGI

it is a kind of CGI which is long-live, which will always be running. With FastCGI, it’ll take less time to fork(which is a problem of fork-and-execute mode in CGI). In additional, FastCGI also supports for distributed computing.

It is also not language related, which is an opened extension of CGI, which is used to keep CGI running in the memory. It’s well-known that loading of CGI has been the main reason of low performance.

The Process Manager of FastCGI will initiate itself to create several CGI processes, which are used to wait for connection of Web servers. When requests from clients have reached the Web server, the Process Manager of FastCGI will select a CGI set up before to connect, whose environmental variables and standard input will be sent to the sub process php-cgi of FastCGI.

This sub process will return standard output and error info to the Web server with the same connection. Requests will be finished when it closes the connection.

Therefore, FastCGI only set once for parsing php.ini, loading extensions and initiating all the data structures.

PHP-CGI

one kind of the Process Manager of FastCGI, which is within php itself.

After changing php.ini, you should reboot PHP-CGI to make the new php.ini work.

When a PHP-CGI process is killed, all the PHP code will cannot run.(PHP-FPM and Spawn-FCGI do not have the same problem)

PHP-FPM

another kind of the Process Manager of FastCGI, It’s actually a patch for PHP, which is used to integrate the Process Manager of FastCGI into PHP, which should be make into PHP before version 5.3.2.

PHP-FPM can be used to control sub processes of PHP-CGI:

options

# --start: start a fastcgi process of php

# --stop: force to kill a fastcgi process of php

# --quit: smooth to kill a fastcgi process of php

# --restart: restart a fastcgi process of php

# --reload: smooth to reload php.ini

# --logrotate: enable log files again

二、CGI和FastCGI区别

我们都知道,web_server(apache,nginx等)只是内容分发者,如果是index.html之类的静态文件,server会在文件系统中找到这个文件并返回,但是对于动态语言,如index.php,server会把这个请求转发给php解释器来处理,那么从server到php解释器,需要转发哪些数据呢?这就是由CGI协议规定的,如下图所示[2]。

【深度】一文读懂cgi,fast-cgi,php-cgi,php-fpm

FastCGI也是一种协议,是为了提升CGI的性能而生的,是CGI协议的升级版。怎么做到的呢?

当server收到请求后,会启动对应的CGI程序,此处是PHP解析器,然后PHP解析器会加载配置文件,初始化执行环境,然后处理请求,再以CGI规定的数据格式返回结果到server,关闭进程,CGI的性能瓶颈就在于每次请求都要重复加载配置,初始化环境,退出进程。

fastCGI会先启动一个master,解析配置文件,初始化执行环境,然后再启动多个worker。当请求过来时,master会把请求传递给其中的一个空闲worker,然后可以继续接受下一个请求。这样就避免了重复的加载配置,初始化执行环境等操作。而且当worker不够用时,master可以根据配置预先启动几个worker等着;当然空闲worker太多时,也会停掉一些,这样就提高了性能,也节约了资源。这就是fastcgi的对进程的管理。

fastCGI 针对php,python,java等都有实现,如下图所示[2]。

【深度】一文读懂cgi,fast-cgi,php-cgi,php-fpm

三、php-cgi和php-fpm区别

php-cgi:官方FastCGI管理器。PHP-CGI的不足:

php-cgi变更php.ini配置后需重启php-cgi才能让新的php-ini生效,不可以平滑重启。直接杀死php-cgi进程,php就不能运行了。(PHP-FPM和Spawn-FCGI没有这个问题,守护进程会平滑从新生成新的子进程)

php-fpm:是实现了FastCGI协议的接口程序,额外还提供了相对智能一些任务管理,最开始只是一个补丁,后来融合到php主分支上了,所以现在的php版本都自带php-fpm。

参考文档

1 https://help.superhosting.bg/en/cgi-common-gateway-interface-fastcgi.html

2 https://www.cnblogs.com/xjnotxj/p/7511778.htm

继续阅读