Comparison of CGI, FastCGI, php-CGI, PHP-FPM, and Spawn-FCGI in PHP environment configuration? _ PHP Tutorial

Source: Internet
Author: User
Comparison of CGI, FastCGI, php-CGI, PHP-FPM, and Spawn-FCGI in PHP environment configuration ?. What is CGICGI? it is a tool called "CommonGatewayInterface". the HTTP server is used to "talk" with programs on your or other machines. The program must run in 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:

The code is as follows:


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

The code is as follows:


/Usr/local/php/sbin/php-fpm {start | stop | quit | restart | reload | logrotate}

-- Start the fastcgi process of php
-- Stop force terminate the fastcgi process of php
-- Quit: smoothly terminate the fastcgi process of php
-- Restart the fastcgi process of php
-- 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.

The code is as follows:


/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. it is set according to the PHP installed on the system.
-A is bound to the addr address.
-P Bind to port
-S Path bound to the unix socket
-C Number of FastCGI processes generated. the default value is 5 (for PHP only)
-P 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/

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

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.