php-summary CGI, FastCGI, php-cgi, PHP-FPM, spawn-fcgi

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

What is CGI

The CGI full name is a "public Gateway Interface" (Common Gateway Interface), an HTTP server with your or other machines on the "chat" program, running on the server (physical server Ah!). On is an important Internet technology that allows a client to request data from a Web browser to a program that executes on a Web server. CGI describes a standard for transferring data between the client and the program. These executable programs are often called CGI scripts, they are ordinary programs, but also stand-alone applications, usually written in the so-called scripting language, such as Perl . The way CGI works, from a Web server perspective, is defined in a specific location (such as http://www.example.com/wiki.cgi) that can run CGI programs. when a request for a matching URL is received, the corresponding program is called and the data sent by the client is used as input to the program. The Web server collects the output of the program, with the appropriate header, and then sends it back to the client. CGI is language-independent, it is just a standard, in any language that is supported by the system, can write CGI scripts, and then set them can be executed.

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.

CGI main function is in the WWW environment, by passing some messages from the client to Wwwserver, and then by Wwwserver to start the specified code to complete the specific work. So, more specifically, CGI is only executable code on Wwwserver, and her job is to control the message request and generate and return the required files. With CGI, your server can read and display formats that the client cannot read (like SQLDatabase). And it can be like gateway, between the server and the client, to generate the information required by the client. Basically, under this type of Master-slave (client/server) environment, the agreement of its IPC (interprocess communication) is accomplished by means of message passing and memory sharing (environmental variables). CGI has its own specific wording and specifications, must abide by its principles, in order to achieve the purpose of the master-slave information exchange.

  What is fastcgi

FastCGI is like a resident (long-live) CGI, which 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). 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.

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. As we all know, the repeated loading of CGI interpreter is the main reason of poor CGI performance, if the CGI interpreter remains in memory and accepts the FASTCGI process manager scheduling, it can provide good performance, scalability, fail-over characteristics and so on.

  FastCGI and CGI features

1, such as cgi,fastcgi also have language-independent.

2, such as CGI, fastcgi in-process applications, running independently of the core Web server, provides a more secure environment than the 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, such as 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 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. WEB server sends CGI environment variables and 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.

  FastCGI of the poor

Because it is multi-process, so consumes more server memory than CGI multithreading, php-cgi interpreter consumes 7 to 25 megabytes per process, multiplying this number by 50 or 100 is a large amount of memory.
  
Nginx 0.8.46+php 5.2.14 (FastCGI) server in 30,000 concurrent connection, open 10 Nginx process consumes 150M memory (15m*10=150m), open 64 php-cgi process consumes 1280M of memory (20m*64= 1280M), 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.

  What is php-cgi

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

To start php-cgi, use the following command:

127.0. 0.1:9000

  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.) )

  What is PHP-FPM

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

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

Now we can download in the latest PHP 5.3.2 source tree to directly integrate the PHP-FPM branch, it is said that the next version will be fused into the main branch of PHP. 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.

PHP5.3.3 has integrated php-fpm, no longer a third-party package. PHP-FPM provides a better way to manage the PHP process, can effectively control the memory and process, can be smooth overloaded PHP configuration, more than spawn-fcgi have a bit more, so by the official PHP included. The PHP-FPM can be turned on with the –ENABLE-FPM parameter when the./configure.

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

/usr/local/php/sbin/php-fpm{start|stop|quit|restart|reload|  --Startthe PHP fastcgi process -Stop Force abort PHP fastcgi process -quit smooth termination of PHP fastcgi process -restart Restart PHP fastcgi process --reload re-smooth loading php php. INI--logrotate re-enable log file

  What is spawn-fcgi

SPAWN-FCGI is a general-purpose fastcgi Management Server, it is a part of LIGHTTPD, many people use lighttpd spawn-fcgi for fastcgi mode management work, 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), in PHP 5.3.3 can be directly used php-fpm.

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 also does not contain this piece (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 http://redmine.lighttpd.net/projects/spawn-fcgi, the latest version is http://www.lighttpd.net/download/ Spawn-fcgi-1.6.3.tar.gz

Note: 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.

127.0. 0.1 9000 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 binding to port ports
-S bound to the path of the UNIX socket
-c Specifies the number of processes generated by the fastcgi, which defaults to 5 (PHP only)
-p Specifies the PID file path of the resulting process
What identity is used by-u and-G fastcgi (-u user-G user group) to run, Ubuntu can be used under Www-data, other configurations, such as nobody, Apache, etc.

  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 used 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-boosting. 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. There are many processes that do not seem to be allocated, while others occupy very high. This may be due to uneven process assignment. This also leads to a decrease in overall response speed. The rational distribution of php-fpm leads to the reference of the overall response and the average of the task.

  Comparison of functions between PHP-FPM and spawn-fcgi

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

PHP-FPM and spawn-fcgi are the process managers that guard php-cgi.

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.