Linux php running mode and Related Terms

Source: Internet
Author: User

1 Preface

I have tested the stress on apache, lighttpd, and nginx three web servers over the past few days. Apache runs in apache module and lighttpd and nginx runs in fastcgi mode. The basic test result is that apache is slow to reflect, and the concurrency is not large. Cpu consumption is high, and the cpu consumption is high after testing. Lighttpd is fast and has a high concurrency. It reflects the high speed and consumes less cpu, which is about 2-3 times that of apache. What is the configuration of Lighttpd. The php-cgi process crashes after stress testing. However, it can be determined that the memory consumption is compared. So there are several problems:

1) Why is the load of apache still high after the test is completed, and lighttpd immediately drops down?

2) Why does nginx memory consume so much?

3) Why does the php-cgi crash during the stress test of nginx?

2. Three php running Modes in linux

In Linux, php has three running modes: First APACHE2HANDLER, second CGI, and third fastcgi.

APACHE2HANDLER is the apache built-in php running method. This method is generally used to print the content that will be seen by the phpinfo function.

In Apache's working mode, after the Apache server is started, multiple process copies are generated in the memory in advance. Once a request appears, these idle sub-processes are used for processing immediately, in this way, there is no latency caused by the generation of sub-processes. These server copies do not exit immediately after processing an HTTP request, but are stuck in the computer waiting for the next request. The response to client browser requests is faster and the performance is high.

This explains why the load on the server after the stress test of apache is high. The Generated Process copy will remain in the memory and wait for the following request. This process is a round-robin process.

1) CGI is an old mode, which has been rarely used in recent years. In cgi Mode, a connection request must first create a cgi sub-process, then process the request, and end the sub-process after processing. This is the fork-and-execute mode. Therefore, the number of connection requests on the cgi-based server is the number of cgi sub-processes. Repeated sub-process loading is the main cause of low cgi performance.

2) FastCGI is like a long-live CGI. It can always be executed. Once activated, it will not take time to fork every time. PHP uses PHP-FPM (FastCGI Process Manager), the full name of PHP FastCGI Process Manager for management.

 

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 the 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. So what is php-cgi?

3. PHP-CGI

PHP-CGI is the FastCGI manager that comes with PHP. It can be directly started. Currently, both lighttpd and nginx can be combined with php-cgi to run php. lighttpd can directly run php-cgi in combination to run php, which is relatively stable. I have tested it, no major problems have occurred, and the efficiency is acceptable. But nginx and php-cgi run php directly in combination. This is a nightmare. The php-cgi process often crashes and consumes a lot of memory! This leaves the second and third questions in the beginning. I haven't figured out yet. Please let me know if you know. Thank you!

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

So what is PHP-FPM?

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

So what is Spawn-FCGI?

5. 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 following code uses 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

 

6. Summary

Well, many php developers may not understand the concepts of these terms very well, and I am not very clear before. Among them, spawn-FCGI and php-fpm do not seem to directly manage fastcgi, but manage php-cgi and indirectly manage fastcgi. This is just my humble opinion. If you have different opinions, contact us for a discussion,

 

Author: tengzhaorong

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.