0%

CGI,FastCGI,PHP,PHP-FastCGI,PHP-FPM之间的联系

CGI(Common Gateway Interface)

一个协议,约定了web服务器与客户端之间通信的数据及数据格式。
实现了CGI协议的程序叫做CGI脚本或CGI程序。

In computing, Common Gateway Interface (CGI) offers a standard protocol for web servers to execute programs that execute like Console applications (also called Command-line interface programs) running on a server that generates web pages dynamically. Such programs are known as CGI scripts or simply as CGIs. The specifics of how the script is executed by the server are determined by the server. In the common case, a CGI script executes at the time a request is made and generates HTML

FastCGI

CGI的进化,降低CGI程序在web服务器与客户端连接时的消耗,为了web服务器能同时服务更多的客户端。
具体消耗体现在:每当客户端有一个请求到web服务器,CGI程序会新起一个进程处理新的请求,这样进程启动初始化的过程非常耗费资源。
FastCGI的解决办法是启动若干FastCGI进程,每次新请求到来直接发送给进程,请求结束不会终止进程,而是继续等待下个请求的到来。这样虽然占用一定内存,但节约了每次启动新进程的时间,空间换时间。

Instead of creating a new process for each request, FastCGI uses persistent processes to handle a series of requests. These processes are owned by the FastCGI server, not the web server.
To service an incoming request, the web server sends environment information and the page request itself to a FastCGI process over either a Unix domain socket, a named pipe or a TCP connection. Responses are returned from the process to the web server over the same connection, and the web server subsequently delivers that response to the end-user. The connection may be closed at the end of a response, but both the web server and the FastCGI service processes persist.
Each individual FastCGI process can handle many requests over its lifetime, thereby avoiding the overhead of per-request process creation and termination. Processing of multiple requests simultaneously can be achieved in several ways: by using a single connection with internal multiplexing (i.e. multiple requests over a single connection); by using multiple connections; or by a combination of these techniques. Multiple FastCGI servers can be configured, increasing stability and scalability.
Web site administrators and programmers can find that the separation of web applications from the web server in FastCGI has many advantages over embedded interpreters (mod_perl, mod_php, etc.). This separation allows server and application processes to be restarted independently – an important consideration for busy web sites. It also enables the implementation of per-application / hosting service security policies, which is an important requirement for ISPs and web hosting companies.[3] Different types of incoming requests can be distributed to specific FastCGI servers which have been equipped to handle those particular types of requests efficiently.

PHP

单纯的指php的可执行文件,比如一个简单的php脚本可以用。

1
php test.php

PHP-FastCGI

内部集成的FastCGI程序。

PHP-FPM(FastCGI Process Manager):

PHP-FPM 实现了PHP-FastCGI的功能,又添加了一些新的功能。
例如进程池功能,通过master主进程调度worker进程。

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.
These features include:

  • Adaptive process spawning (NEW!)
  • Basic statistics (ala Apache’s mod_status) (NEW!)
  • Advanced process management with graceful stop/start
  • Ability to start workers with different uid/gid/chroot/environment and different php.ini (replaces safe_mode)

参考资料: