PHP Performance Tuning---PHP-FPM Configuration and Usage Summary

Source: Internet
Author: User
Tags fpm php performance tuning php script php source code high cpu usage nginx server server memory

PHP-FPM Configuration and Usage Summary:

PHP-FPM is a PHP fastcgi manager, which is actually a patch of PHP source code, designed to introduce fastcgi process management into the PHP package, we have to patch it into the PHP source code, and then compile to use. Now we can open and use it directly in PHP 5.3.2 and later, because PHP has already paid for it from that version to the package, so it's no longer a fix pack.

· Understanding of several concepts

· nginx+php Configuration

· Operation of the PHP-FPM

I. Understanding of several concepts

1), CGI

The CGI full name "Public Gateway Interface" (Common Gateway Interface) is an interface that the HTTP server communicates with programs on other machines, and its programs must be run on a network server.

Note:

CGI can be written in any language, as long as the language has standard input, output, and environment variables.

2), FastCGI

FastCGI is a resident 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. We know that the repeated loading of the CGI interpreter is the main reason for the poor CGI performance, and if the CGI interpreter is kept in memory and accepted by the FASTCGI process Manager, it can provide good performance, scalability, and so on.

Advantages:

1. fastcgi is language-independent;

2. FastCGI is run independently of the core Web server, providing 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. and the application code of the malicious API can even steal the key of another application or core server;

3. FASTCGI technology currently supports languages such as: C + +, Java, Perl, TCL, Python, SmallTalk, Ruby, etc. Related modules are also available on popular servers such as Apache, ISS, lighttpd, etc.;

4. FastCGI does not rely on the internal architecture of any Web server, so fastcgi remains stable even if the server technology changes;

Disadvantages:

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.

The above data is from the Nginx 0.8.x + PHP 5.2.13 (FastCGI) build 10 times times more than Apache Web Server (6th edition).

Principle:

1. When the Web server starts, load the FASTCGI process manager;

2. FASTCGI Process Manager initialization, start multiple CGI interpreter processes (php-cgi) and wait for connections 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, and the Web server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.

4. The FASTCGI child process returns standard output and error information from the same connection to the Web server after processing is complete. 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 the Web server). In CGI mode, php-cgi exits here.

In the above scenario, you can imagine how slow CGI is usually, and each Web request PHP must re-parse php.ini, reload all extensions, and reinitialize all data structures. With fastcgi, all of this occurs only once when the process is started. Additionally, the database persistent connection can work.

Note:

FastCGI's main advantage is to separate the dynamic language and HTTP server, so nginx and PHP/PHP-FPM are often deployed on different servers to share the pressure of the front-end Nginx server, so that nginx-only processing static requests and forwarding dynamic requests, and PHP /PHP-FPM Server-Exclusive parsing of PHP dynamic requests.

3), php-cgi

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

PHP-CGI deficiency:

1. php-cgi change php.ini configuration to restart the php-cgi to allow the new Php-ini to take effect, can not smooth restart.

2. Directly kill the php-cgi process, PHP will not be able to run (PHP-FPM and spawn-fcgi without this problem, the daemon will smoothly regenerate new child processes).

4),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 directly use 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. In lighttpd-1.4.15

Included in the version (HTTP://WWW.LIGHTTPD.NET/DOWNLOAD/LIGHTTPD-1.4.15.TAR.GZ), the current 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 find the latest version of its release address by searching the lighttpd.net website for "spawn-fcgi".

5), Contrast spawn-fcgi

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 the uneven distribution of process tasks. This also leads to a decline in the overall response rate. The rational distribution of php-fpm leads to the reference of the overall response and the average of the task.

Second, nginx+php configuration

1, Process number optimization

PM = dynamic

Pm.max_children = 300

Pm.start_servers = 20

Pm.min_spare_servers = 5

Pm.max_spare_servers = 35

2, the maximum number of requests optimization

Pm.max_requests = 10240

Note:

This is used to deal with memory leaks caused by PHP parsers or referenced third-party libraries.

Maximum number of requests: A PHP-FPM worker process terminates after processing how many requests.

3. Maximum Execution time optimization (php.ini)

Request_terminate_timeout = 20

Note:

This is used to deal with 502 errors due to the long execution time of PHP.

This time-length configuration can be configured in php.ini (max_execution_time) or php-fpm.conf, and can be implemented in php-fpm.conf in order not to affect global configuration.

It is important to note that the combination of the Max_fail in the nginx.conf (larger) and the fail_timeout (smaller) should be configured together.

Nginx.conf:

Location ~ \.php$ {

...

Fastcgi_connect_timeout 180;

Fastcgi_read_timeout 600;

Fastcgi_send_timeout 600;

...

}

Note:

The maximum execution time for a script set by PHP-FPM is long enough, but when executing a time-consuming PHP script, it turns out that the Nginx error has changed to 504. This is because we are only modifying the configuration of PHP, Nginx also has a communication with the upstream server timeout period configuration factcgi_connect/read/send_timeout.

4, PHP-FPM high CPU usage troubleshooting

Top command:

After executing the top command directly, enter 1 to see the CPU usage of each core:

SAR command:

Installation of SAR and IOSTAT commands:

Sysstat.x86_64:The SAR and Iostat systemmonitoring commands

Yum Install-y sysstat.x86_64

Perform:

$sar-P All 1 100

Note:

-P all means monitoring all cores;

1 indicates the acquisition every 1 seconds;

100 means acquisition 100 times;

5. Turn on slow log

Slowlog = log/$pool. Log.slow

Request_slowlog_timeout = 2

Note:

The above is to open php-fpm slow log, Time threshold value of 2 seconds;

Perform:

Grep-v "^$" Php.slow.log | Cut-d ""-F 3,2 | Sort |uniq-c | SORT-K1,1NR | Head-n 50

Note:

Sort: Sorting words

Uniq-c: Displays unique rows and the number of occurrences of the bank in the file at the beginning of each line

SORT-K1,1NR: Sorted by first field, numerically, and in reverse order

Head–n 10: Take the first 10 rows of data

Ps:

The purpose of the slow log is to keep track of the time that the PHP script executes longer than the set request_slowlog_timeout, and if this setting time is exceeded, the script is recorded.

Third, the operation of PHP-FPM

The PHP-FPM under PHP5.3.3 no longer supports commands such as/USR/LOCAL/PHP/SBIN/PHP-FPM (Start|stop|reload), which PHP-FPM previously had, and requires the use of signal control:

The master process can understand the following signals

INT, term terminates immediately?;

QUIT smooth termination?;

USR1 reopen log file?;

USR2 smoothly overloads all worker processes and reloads the configuration and binary modules;

Example:?

PHP-FPM closed:? kill-int ' Cat/usr/local/php/var/run/php-fpm.pid '?

PHP-FPM restart:? KILL-USR2 ' Cat/usr/local/php/var/run/php-fpm.pid '

To view the number of PHP-FPM processes:

$ps aux | Grep-c PHP-FPM

PHP Performance Tuning---PHP-FPM Configuration and Usage Summary

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.