PHP environment Configuration CGI, FastCGI, php-cgi, php-fpm, spawn-fcgi comparison? _php Tips

Source: Internet
Author: User
Tags data structures fpm php source code server memory
What is a CGI

The CGI full name is the "Public Gateway Interface" (Common Gateway Interface), a tool that HTTP servers "talk" to programs on your or other machines, and their programs must run on a network server.

CGI can be written in any language, as long as the language has standard input, output, and environment variables. such as PHP,PERL,TCL, etc.

What is fastcgi

FastCGI is like a resident (long-live) CGI, which can be executed all the time, and it won't take a while to fork once (this is CGI's most criticized Fork-and-execute model). It also supports distributed operations, where the FastCGI program can execute on a host other than the Web server and accept requests from other Web servers.

The fastcgi is a language-independent, scalable, CGI-open extension that primarily acts to keep the CGI interpreter process in memory and thus achieve higher performance. As we all know, the repeated loading of CGI interpreters is the main reason for the low performance of CGI, and can provide good performance, scalability, fail-over characteristics, and so on if the CGI interpreter remains in memory and accepts the FASTCGI process Manager schedule.

FastCGI and CGI features

1, such as cgi,fastcgi also has language-independent nature.

2, such as CGI, fastcgi applications in the process, running independently of the core Web server, provide a more secure environment than the API. (APIs link application code to the core Web server, which means that an application in the wrong API can damage other applications or core servers; The malicious API's application code can even steal the keys of another application or core server.) )

3, the FASTCGI technology currently supports languages: C + +, Java, Perl, TCL, Python, SmallTalk, Ruby and so on. Related modules are also available on popular servers such as Apache, ISS, and lighttpd.

4, such as cgi,fastcgi is not dependent on any Web server's internal architecture, so even if the server technology changes, fastcgi is still stable.

The working principle of fastcgi
1. Load fastcgi process Manager at Web Server startup (IIS ISAPI or Apache Module)
2, the FASTCGI process manager itself initializes, initiates multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web server.
3. When a client request arrives at the Web server, the FASTCGI process manager selects and connects to a CGI interpreter. The WEB server sends CGI environment variables and standard input to the fastcgi subprocess php-cgi.
4. The fastcgi child process completes processing and returns standard output and error information from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed. The fastcgi process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In the CGI mode, php-cgi quits here.
In the above scenario, you can imagine how slow CGI is usually. Each Web request PHP must reparse php.ini, reload all extensions, and reinitialize all data structures. With fastcgi, all of these occur only once when the process is started. An additional benefit is that persistent database connections (persistent DB connection) can work.

The shortage of fastcgi

Because it is a multiple process, it consumes more server memory than CGI multithreading, and the PHP-CGI interpreter consumes 7 to 25 megabytes per process, multiplying this number by 50 or 100 is a large amount of memory.
The Nginx 0.8.46+php 5.2.14 (FastCGI) server consumes 150M of memory (Nginx) with 10 15m*10=150m processes on 30,000 concurrent connections, and 64 php-cgi processes that open consume 1280M of memory (20m*64= 1280M), combined with the memory consumed by the system itself, consumes less than 2GB memory. If the server memory is small, you can simply turn on 25 php-cgi processes so that the total amount of memory consumed by php-cgi is 500M.

The above data is excerpted from Nginx 0.8.x + PHP 5.2.13 (FastCGI) to build a Web server 10 times times better than Apache (6th edition)

What is php-cgi

PHP-CGI is the fastcgi Manager with PHP.
Start php-cgi, using the following command:

Copy Code code as follows:


the shortage of php-cgi

1, php-cgi change php.ini configuration needs to restart php-cgi to make the new Php-ini effective, can not smooth restart

2, directly kill the php-cgi process, PHP can not run. (PHP-FPM and spawn-fcgi do not have this problem, the daemon will smoothly regenerate new child processes.) )

  What is PHP-FPM

PHP-FPM is a PHP fastcgi manager that is only used in PHP and can be downloaded in

PHP-FPM is actually a patch of PHP source code, designed to integrate FASTCGI process management into the PHP package. It must be patch to your PHP source code and can be used only after compiling and installing PHP.

Now we can download the new PHP 5.3.2 source tree to directly integrate the PHP-FPM branch, it is said that the next version will be merged into the main branch of PHP. Relative SPAWN-FCGI,PHP-FPM in the CPU and memory control is better, and the former is very easy to crash, must be monitored with crontab, and PHP-FPM does not have this annoyance.

PHP5.3.3 has integrated PHP-FPM and is no longer a third party package. PHP-FPM provides a better way to manage PHP processes, can effectively control memory and process, can smooth overload PHP configuration, more than spawn-fcgi have a bit more, so the official PHP included. /configure with –ENABLE-FPM parameters to open the PHP-FPM.

Using PHP-FPM to control the fastcgi process of php-cgi

Copy Code code as follows:


--start start the fastcgi process of PHP
--stop forced to terminate PHP fastcgi process
--quit smooth termination of PHP fastcgi process
--restart Restart PHP's fastcgi process
--reload to reload PHP php.ini
--logrotate log file back on

What is spawn-fcgi

SPAWN-FCGI is a common fastcgi Management Server, it is a part of the LIGHTTPD, many people use LIGHTTPD spawn-fcgi mode of management work, but there are a lot of shortcomings. and PHP-FPM to alleviate some of the problems, but PHP-FPM has a drawback is to recompile, this for some already running environment may have a small risk (refer), in the PHP 5.3.3 can directly use PHP-FPM.

SPAWN-FCGI has now become a single project, more stable, but also to many Web site configuration to bring convenience. There are already a number of sites to match it with Nginx to solve dynamic Web pages.

The latest lighttpd also does not include this piece (Http://, but it can be found in previous versions. Included in the lighttpd-1.4.15 version (

The current spawn-fcgi download address is and the latest version is Spawn-fcgi-1.6.3.tar.gz

Note: The latest spawn-fcgi can go to the Web site to search "spawn-fcgi" to find its latest version of the release address

Here we can use spawn-fcgi to control php-cgi fastcgi process.

Copy Code code as follows:

/usr/local/bin/spawn-fcgi-a 9000-c 5-u www-data-g www-data-f

The parameter meaning is as follows:

-f Specifies the location of the executing program that invokes the FASTCGI process, based on the conditions of the PHP installed on the system
- a bind to address addr
-P is bound to ports port
-S path to a UNIX socket
-C Specifies the number of fastcgi processes generated, default 5 (PHP only)
-p Specifies the PID file path of the resulting process
-U and-G fastcgi use what identity (-u user-G user group) to run, Ubuntu can use Www-data, other according to the situation configuration, such as nobody, Apache, etc.

  PHP-FPM and spawn-cgi contrast test

PHP-FPM is very convenient to use, the configuration is in the Php-fpm.ini file, and start, restart can be carried out from the PHP/SBIN/PHP-FPM. It is more convenient to modify the php.ini can be used directly after the PHP-FPM reload loading, without killing the process can be completed php.ini modified loading
The results show that using PHP-FPM can make PHP a little more performance-enhancing. PHP-FPM-controlled process CPU recovery is slow, memory allocation is very uniform.

SPAWN-FCGI-controlled processes have a fast CPU drop, while memory allocations are unevenly distributed. There are many processes that do not seem to be allocated, while others occupy a high number of them. This may be caused by uneven allocation of process tasks. This also leads to a decline in the overall response rate. and PHP-FPM reasonable distribution, resulting in the overall response to the reference as well as the average task.

  Comparison of PHP-FPM and SPAWN-FCGI functions

PHP-FPM, spawn-fcgi are the daemon php-cgi process Manager.

Reference Documentation:


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.