What are CGI, FastCGI, PHP-CGI, PHP-FPM, and Spawn-FCGI ?, Fastcgiphp-cgi

Source: Internet
Author: User

What are CGI, FastCGI, PHP-CGI, PHP-FPM, and Spawn-FCGI ?, Fastcgiphp-cgi

  What is CGI?

CGI is a Common Gateway Interface. It is a tool for the HTTP server to "talk" with programs on your or other machines, the program must run on the network server.

CGI can be written in any language as long as it has standard input, output, and environment variables. Such as php, perl, and tcl.

  What is FastCGI?

FastCGI is like a long-live CGI. It can be executed all the time, it will not take time to fork every time (this is the most criticized fork-and-execute mode for CGI ). It also supports distributed operations, that is, FastCGI programs can run on hosts other than website servers and receive requests from other website servers.

FastCGI is a CGI open extension in a language-independent and scalable architecture. Its main behavior is to maintain the CGI interpreter process in the memory and thus achieve high performance. As we all know, repeated loading of the CGI interpreter is the main cause of low CGI performance. If the CGI interpreter is kept in the memory and is scheduled by the FastCGI Process Manager, it can provide good performance, scalability, Fail-Over features, and so on.

  FastCGI and CGI features

1. For example, CGI and FastCGI have language independence.

2. applications such as CGI and FastCGI in processes run independently of core web servers and provide a safer environment than APIs. (APIs links the application code with the core web server, which means that the application in a wrong API may damage other applications or core servers; malicious API application code can even steal the keys of another application or core server .)

3. Currently, FastCGI technology supports the following languages: C/C ++, Java, Perl, Tcl, Python, SmallTalk, and Ruby. Related modules are also available on popular servers such as Apache, ISS, and Lighttpd.

4. For example, CGI and FastCGI do not depend on the internal architecture of any Web server. Therefore, even if the server technology changes, FastCGI remains stable.

  FastCGI Working Principle
  
1. Load FastCGI Process Manager (iis isapi or Apache Module) when the Web Server starts)
  
2. FastCGI Process Manager initializes itself, starts Multiple CGI interpreter processes (multiple php-cgi are visible), 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 sub-process php-cgi.
  
4. After the FastCGI sub-process completes processing, the standard output and error messages are returned from the same connection to the Web Server. When the FastCGI sub-process closes the connection, the request processing is complete. The FastCGI sub-process then waits for and processes the next connection from the FastCGI Process Manager (running on the Web Server. In CGI Mode, php-cgi exits here.
  
In the above cases, you can imagine how slow CGI is. For each Web request, PHP must re-Parse php. ini, re-load all extensions, and initialize all data structures. With FastCGI, all of these occur only once when the process starts. An additional benefit is that Persistent database connection can work.

  FastCGI Deficiency

Because it is a multi-process, it consumes more server memory than CGI multithreading, The PHP-CGI interpreter consumes 7 to 25 MB memory per process, multiply this number by 50 or 100, which is a large amount of memory.
  
The Nginx 0.8.46 + PHP 5.2.14 (FastCGI) server consumes 30 thousand MB of memory (15 MB * 10 = 150 MB) for the 10 Nginx processes enabled with concurrent connections ), the 64 opened php-cgi processes consume 1280 MB of memory (20 mb * 64 = Mb). In addition, the system consumes less than 2 GB of memory. If the server memory is small, only 25 php-cgi processes can be started, so that the total memory consumed by php-cgi is 500 mb.

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

  What is PHP-CGI

PHP-CGI is the FastCGI manager that comes with PHP.

Start the PHP-CGI with the following command:

php-cgi -b 127.0.0.1:9000

  PHP-CGI Deficiency

1. After php-cgi changes the php. ini configuration, restart php-cgi to make the new php-ini take effect.

2. directly kill the php-cgi process, and php cannot run. (PHP-FPM and Spawn-FCGI don't have this problem, and the daemon will smoothly generate new sub-processes .)

  What is PHP-FPM

PHP-FPM is a PHP FastCGI manager that is only used for PHP and can be downloaded at http://php-fpm.org/download.

PHP-FPM is actually a patch of PHP source code, designed to integrate FastCGI process management into the PHP package. You must patch it to your PHP source code before using it after compiling and installing PHP.

Now we can download the source code tree in the latest PHP 5.3.2 to directly integrate the branch of the PHP-FPM, it is said that the next version will be integrated into the main branch of PHP. Compared with Spawn-FCGI, PHP-FPM has better control over CPU and memory, and the former is easy to crash and must be monitored with crontab, while PHP-FPM has no such troubles.

PHP5.3.3 has already integrated php-fpm and is no longer a third-party package. PHP-FPM provides a better way of PHP process management, can effectively control the memory and process, can smoothly load PHP configuration, than spawn-fcgi has more, so PHP officially included. You can enable PHP-fpm with the-enable-FPM parameter in./configure.

Use PHP-FPM to control FastCGI processes of PHP-CGI

/Usr/local/php/sbin/php-fpm {start | stop | quit | restart | reload | logrotate} -- start fastcgi process of php -- stop force terminate fastcgi process of php -- quit smoothly terminates php fastcgi process -- restart restarts php fastcgi process -- reload re-smoothly loads php. ini -- logrotate re-enable log files

  What is Spawn-FCGI?

Spawn-FCGI is a common FastCGI management server. It is part of lighttpd. Many people use Spawn-FCGI of Lighttpd to manage FastCGI mode, but it has many disadvantages. While the emergence of PHP-FPM has mitigated some problems, but the PHP-FPM has a disadvantage is to re-compile, which for some already running environment may have a small risk (refer ), you can use the PHP-FPM directly in php 5.3.3.

Spawn-FCGI has become a single project, which is more stable and convenient for the configuration of many Web sites. Many sites have already used it with nginx to solve dynamic web pages.

The latest lighttpd does not contain this one either (http://www.lighttpd.net/search? Q = Spawn-FCGI), but it can be found in previous versions. Included in the lighttpd-1.4.15 version (http://www.lighttpd.net/download/lighttpd-1.4.15.tar.gz)

Currently, Spawn-FCGI is composed

Note: The latest Spawn-FCGI can be searched for "Spawn-FCGI" on the lighttpd.net website to find its latest release address.

Next we can use Spawn-FCGI to control the FastCGI process of php-CGI.

    /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-CGI

The parameter description is as follows:

-F specifies the location of the execution program of the process that calls FastCGI, Which is set according to the PHP installed on the system
-A is bound to the addr address.
-P: bind to port
-S: The path to bind to the unix socket
-C indicates the number of FastCGI processes generated. The default value is 5 (for PHP only)
-P specifies the PID file path of the Generated Process
-U and-g FastCGI use the identity (-u user-g user group) to run. www-data can be used in Ubuntu, and other configuration based on the situation, such as nobody and apache.

  Comparison between PHP-FPM and spawn-CGI

PHP-FPM is very convenient to use, configuration is in the PHP-FPM.ini file, and start, restart can be done from php/sbin/PHP-FPM. More convenient is to modify php. ini can be directly loaded using the PHP-FPM reload, without killing the process can complete the php. ini modification Load
The results show that using PHP-FPM can greatly improve php performance. PHP-FPM Control Process cpu recovery speed is slow, memory allocation is very even.

The CPU usage of the process controlled by Spawn-FCGI decreases rapidly, while the memory allocation is uneven. Many processes do not seem to be assigned, but others occupy a high volume. This may be due to uneven process task allocation, which also leads to a decrease in the overall response speed. The rational allocation of PHP-FPM leads to the mention of the overall response and the average task.

  Comparison of PHP-FPM and Spawn-FCGI Functions

Http://php-fpm.org/about/

PHP-FPM and Spawn-FCGI are all process managers that guard php-cgi.

Reference:

Http://topic.csdn.net/u/20100216/22/5809e272-6f67-4248-bde9-99deeae5215b.html
Http://topic.csdn.net/u/20101015/19/8ae74452-ec6b-448e-9942-21faeb008cd7.html
Http://club.topsage.com/thread-768488-1-1.html
Http://www.unixaid.info/index.php/productsapp/23-servsf/842-spawn-fcgi
Http://www.fastcgi.com/drupal/node/2
Http://baike.baidu.com/view/641394.htm
Http://baike.baidu.com/view/32614.htm
Http://blog.yation.com/network/fastcgi/

Related Article

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.