What is CGI FastCGI php-cgi php-fpm spawn-cgi? The relationship between them

Source: Internet
Author: User
Tags fpm php source code server memory

1. Cgi:common Gateway Interface external applications:

CGI is an interface standard between an external application (CGI program) and a Web server, and is a discipline for passing information between CGI programs and Web servers.
The CGI specification allows the Web server to execute external programs and send their output to a Web browser.
When the user is able to access a particular dynamic resource (example *.cgi), the Web server is triggered, and the Web server invokes an additional application (or process) based on the CGI protocol to execute the dynamic program, and the code that formats the dynamic program into HTML is then returned to the Web server.
It is a protocol that allows a Web server to interact with an application server.
CGI can be written in any language, as long as the language has standard input, output, and environment variables.
such as: Perl, Bourne Shell, PHP, C and C + +. CGI: protocol

2. FASTCGI: Like a resident (long-live) type of CGI:

1. It can be executed all the time, so long as it is activated, it will not take a moment to fork once (this is the most notorious fork-and-execute mode of CGI).
2. It also supports distributed operations where the FastCGI program can execute and accept requests from other Web servers on hosts other than the Web server.
3.FastCGI is a language-independent, extensible architecture for CGI open extensions whose main behavior is to keep the CGI interpreter process in memory and thus achieve high performance.

3.FASTCGI vs. CGI advantages:

The CGI interpreter's repeated loading is the main reason for the poor CGI performance
If the CGI interpreter remains in memory and accepts the FASTCGI process Manager schedule, it can provide good performance, scalability, Fail-over features, and so on
You can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all extensions, and initialize all data structures.
With fastcgi, all of this occurs only once when the process is started. An additional benefit is that the database connection work can be sustained

4.FastCGI and CGI features:

1, cgi,fastcgi also has language independence.
2. CGI, fastcgi application in process, runs independently of the core Web server, provides a more secure environment than API
(APIs link the application's code with the core Web server, which means that an application in the wrong API can corrupt other applications or core servers; The application code of a malicious API can even steal the key from another application or core server.) )
3, FASTCGI technology currently supports languages include: C + +, Java, Perl, TCL, Python, SmallTalk, Ruby and so on. Related modules are also available on popular servers such as Apache, ISS, lighttpd and more.
4, cgi,fastcgi does not depend on any Web server's internal architecture, so even if the server technology changes, fastcgi is still stable.

How the 5.FastCGI works:

1. Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) when Web server starts
2, the FASTCGI process manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web server.
3. When a client request arrives at Web server, the FASTCGI process manager selects and connects to a CGI interpreter. The webserver sends the CGI environment variable and the standard input to the FASTCGI child process php-cgi.
4. After the fastcgi process finishes processing, the standard output and error information are returned from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In CGI mode, php-cgi exits here. In the above scenario, you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all extensions, and initialize all data structures. With fastcgi, all of this occurs only once when the process is started. An additional benefit is that the persistent database connection (persistent connection) can work.

6.FastCGI of deficiency
       因为是多进程,所以比CGI多线程消耗更多的服务器内存,PHP-CGI解释器每进程消耗7至25兆内存,将这个数字乘以50或100就是很大的内存数。

Nginx 0.8.46+php 5.2.14 (FastCGI) server in 30,000 concurrent connection, open 10 Nginx process consumes 150M memory (15m10=150m), open 64 php-cgi process consumes 1280M of memory (20m64=128 0M), combined with the memory consumed by the system itself, consumes less than 2GB of memory.
If the server memory is small, you can only open 25 php-cgi processes, so that the total amount of memory consumed by php-cgi is 500M.

7. What is php-cgi

PHP-CGI is PHP's own fastcgi manager
To start php-cgi, use the following command:/usr/bin/php-cgi-b 127.0.0.1:9000

8.php-cgi of the poor

1, php-cgi change php.ini configuration need to restart php-cgi to let the new Php-ini take effect, 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 smooth the new child process from the newly generated)

9. What is PHP-FPM

php-fpm is a PHP fastcgi manager that is used only for PHP, PHP-FPM is actually a patch of PHP source code designed to integrate FASTCGI process management into a PHP package. It must be patch
To your PHP source code, you can use it after compiling and installing PHP.
PHP5.3.3 has integrated PHP-FPM and is no longer a third-party package.
Relative SPAWN-FCGI,PHP-FPM in the CPU and memory control are better, and the former is easy to crash, must be monitored with crontab, and PHP-FPM does not have this annoyance.
PHP-FPM provides a better way to manage the PHP process, can effectively control memory and process, can be smooth overloaded PHP configuration, more than spawn-fcgi has more advantages, so by the official PHP included.
In./configure with the –ENABLE-FPM parameter use PHP-FPM to control the fastcgi process of the php-cgi
/usr/local/php/sbin/php-can open php-fpm. Fpm{start|stop|quit|restart|reload|logrotate}
--start starting PHP's fastcgi process
--stop forcing the fastcgi process to terminate PHP
--quit smooth termination of PHP fastcgi process
--restart restarting PHP's fastcgi process
--reload re-load PHP php.ini
--logrotate re-enabling the log file

10. What is spawn-fcgi

SPAWN-FCGI is a generic fastcgi Management Server, it is part of LIGHTTPD, many people use lighttpd spawn-fcgi for fastcgi mode management, but there are many shortcomings.
And the php-fpm of how much to alleviate some of the problems, but PHP-FPM has a drawback is to recompile, which for some already running environment may have a small risk (refer),
The PHP-FPM can be used directly in PHP 5.3.3.
SPAWN-FCGI has now become a single project, more stable, but also to many Web site configuration to facilitate. There are a number of sites to match it with Nginx to solve dynamic Web pages.
The newest lighttpd didn't include this one, either.
The latest spawn-fcgi can search the Lighttpd.net website for "spawn-fcgi" to find the latest version of its release address
So we can use spawn-fcgi to control the php-cgi fastcgi process.
/usr/local/bin/spawn-fcgi-a 127.0.0.1-p9000-c 5-u www-data-g www-data-f/usr/bin/php-cgi
The parameters have the following meanings:
-f Specifies the execution program location of the process that calls fastcgi, depending on the situation of PHP installed on the system
-a bind to address addr
-P is bound to port number, default is 5 (PHP only)
-S bound to the path of the UNIX socket
-p Specifies the PID file path of the resulting process
-c Specifies the resulting fastcgi process
What identity is used by-u and-G fastcgi (-u user-G user group) to run,
Www-data can be used under Ubuntu, other configuration according to the situation, such as nobody, Apache, etc.

11.PHP-FPM and spawn-cgi comparison test:

The PHP-FPM is very convenient to use, the configuration is in the Php-fpm.ini file, and the start, restart can be done from the PHP/SBIN/PHP-FPM.
More convenient is to modify the php.ini can be directly using the PHP-FPM reload to load, do not have to kill the process to complete the php.ini modification loading results show that using PHP-FPM can make PHP a small performance improvement.
PHP-FPM controlled process CPU recovery is slow, memory allocation is very uniform.
SPAWN-FCGI-controlled processes CPU drops quickly, while memory allocations are not evenly spaced. This may be due to uneven process assignment. This also leads to a decrease in overall response speed.
and PHP-FPM reasonable allocation, resulting in the overall response of the reference and the average of the task

PHP-FPM and spawn-fcgi function comparison php-fpm, spawn-fcgi are daemon php-cgi process Manager.

What is CGI FastCGI php-cgi php-fpm spawn-cgi? The relationship between them

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.