What are CGI, FastCGI, PHP-CGI, PHP-FPM, and spawn-fcgi?

Source: Internet
Author: User
Tags php source code
What is CGI?

CGI stands for the Common Gateway Interface, which is a program running on a Web server. With browser input triggered, this script is usually like a bridge between a server and other programs in the system, such as a database.

CGI script Working principle:
  1. A browser request through an HTML form or hyperlink refers to the URL of the previous CGI application.
  2. The server sends and receives requests.
  3. The server executes the specified CGI application.
  4. The operations required for CGI application execution are usually based on the Content input by the browser.
  5. The CGI application formats the result as a document that can be understood by the Web server and the browser (usually an HTML webpage ).
  6. The network server returns the result to the browser.


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 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
PHP-CGI Deficiency
  1. After changing the php. ini configuration of PHP-CGI, 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

Next we can use spawn-fcgi to control the FastCGI process of PHP-CGI.

/usr/local/bin/spawn-fcgi -a -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, according to the situation of PHP installed on the system, specific settings-a bound to the address ADDR-P bound to the Port port-s bound to the path of the UNIX socket path-C specified the number of FastCGI Processes, the default value is 5 (for PHP only)-P, which specifies the PID file path of the generated process-U and-G FastCGI (-u user-G user group) to run, WWW-data can be used in Ubuntu. Other configurations, such as nobody and Apache, can be used as needed.



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. It is more convenient 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


Php-FPM and spawn-fcgi are all process managers that guard PHP-CGI.





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.