How does the nginx server execute the php project?

Source: Internet
Author: User
The full name of CGI is "CommonGatewayInterface". an HTTP server is a tool used to "talk" with programs on your or other machines. programs must run on network servers.

Full range of PHP Video Tutorial: elaborate PHP-http://www.xishuophp.com/


In fact, the following concepts are understood, and you will understand the problems to be explained in this article. 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 itself can be seen as a protocol standard. it can write specific protocols that comply with the interface standard in any language, as long as the language has standard input, output, and environment variables. Such as php, perl, and tcl.


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 is also known as a protocol standard. for example, the php-fpm mentioned below is a fastCGI Process Manager/engine that supports parsing php.



FastCGI features
  1. FastCGI has language independence.

  2. FastCGI applications 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 corrupt 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. FastCGI does not rely on the internal architecture of any Web server. Therefore, FastCGI remains stable even if the server technology changes.

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)

PHP-CGI

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

Lack of PHP-CGI:

  1. After changing the php. ini configuration, you must 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 .)

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.

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), the current Spawn-FCGI is.

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

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. It may be caused by uneven process task allocation. This also led 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.


Summary:

  FAstCGI is a communication interface between nginx and php. the actual processing process of this interface is to start the php-fpm process to parse the php script., That isPhp-fpm is equivalent to a dynamic application serverTo enable nginx dynamic parsing of php. Therefore, if the nginx server needs to support php parsing, you need to add the php configuration in nginx. conf: forward the php script to the IP address and port of the fastCGI process listener (specified in the php-fpm.conf ). During php installation, you must enable the fastCGI support option and compile and install the php-fpm patch/extension.Start the php-fpm process to parse the php script forwarded by nginx through fastCGI..


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.