Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PHP-FPM configuration and tuning mode

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "PHP-FPM configuration and tuning mode". In daily operation, I believe many people have doubts about PHP-FPM configuration and tuning mode. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "PHP-FPM configuration and tuning mode". Next, please follow the editor to study!

What is PHP-FPM?

PHP-FPM is a manager for PHP FastCGI, which is actually a patch of PHP source code designed to introduce FastCGI process management into the PHP package.

What is CGI?

CGI, whose full name is "Common Gateway Interface" (Common Gateway Interface), is the interface through which HTTP servers communicate with programs on other machines, and its programs must run on the network server.

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

What is FastCGI? Advantages and disadvantages.

FastCGI is a resident CGI that can be executed all the time, as long as it is activated and does not take the time to fork every time (this is the most criticized fork-and-execute mode of CGI). It also supports distributed computing, that is, FastCGI programs can be executed on hosts other than web servers and accept requests from other web servers.

FastCGI is a language-independent, scalable architecture open extension of CGI, and its 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 performance of CGI. If the CGI interpreter is kept in memory and scheduled by the FastCGI process manager, it can provide good performance, scalability, and so on.

Because it is multi-process, it consumes more server memory than CGI multithreading. The PHP-CGI interpreter consumes 7 to 25 megabytes of memory per process, and multiplying this number by 50 or 100 is a large amount of memory.

How FastCGI works (the number of configurations is associated with memory size)

When the Web server starts, load the FastCGI process manager.

The FastCGI process manager initializes, starts multiple CGI interpreter processes (PHP-CGI) and waits for a connection from the Web server.

When the 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.

The FastCGI child process returns standard output and error information from the same connection to the Web server after processing. When the FastCGI child process closes the connection, the request is processed. 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 case, you can imagine how slow the CGI usually is, and each Web request PHP must reparse the php.ini, reload all the extensions, and reinitialize the entire data structure. With FastCGI, all of this happens only once when the process starts. In addition, database persistent connections work.

The main advantage of NOTE:FastCGI is that it separates dynamic language from HTTP Server, so Nginx and PHP/PHP-FPM are often deployed on different servers to share the pressure of front-end Nginx servers, so that Nginx specializes in processing static requests and forwarding dynamic requests, while PHP/PHP-FPM servers focus on parsing PHP dynamic requests.

PHP-CGI

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

After php-cgi changes the php.ini configuration, you need to restart php-cgi for the new php-ini to take effect, and cannot restart smoothly.

Simply kill the php-cgi process and php will not run (PHP-FPM and Spawn-FCGI do not have this problem, and the daemon will smoothly regenerate the new child process).

Nginx+PHP configuration

1. Optimize the number of processes

Cd / usr/local/php/etc# ll-rw-r--r-- 1 root root 1228 May 27 14:07 pear.conf-rw-r--r-- 1 root root 480 May 27 14:19 php-fpm.conf-rw-r--r-- 1 root root 23046 May 27 14:07 php-fpm.conf.default-rw-r--r-- 1 root root 73862 June 3 18:14 php.ini

Modify php-fpm.conf

Pm = dynamic # dynamic allocation. If pm is set to static, then only the parameter pm.max_children takes effect. Pm.max_children = 200 # the number of php-fpm processes opened in static mode He limits the maximum number of php-fpm processes in dynamic mode pm.start_servers = 120 # initial php-fpm processes in dynamic mode pm.min_spare_servers = 5 # minimum number of php-fpm processes in dynamic mode idle state pm.max_spare_servers = 180 # maximum number of php-fpm processes in dynamic mode idle state

NOTE: if pm is set to dynamic,4, all parameters will take effect. The system starts pm.start_servers php-fpm processes when php-fpm starts, and then dynamically adjusts the number of php-fpm processes between pm.min_spare_servers and pm.max_spare_servers according to the needs of the system.

2. Optimization of the maximum number of requests

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: refers to how many requests a php-fpm worker process is terminated after processing.

3. Maximum execution time optimization (php.ini)

Request_terminate_timeout = 100

NOTE:

The php script takes the minimum values of max_execution_time and request_terminate_timeout as the timeout for the script.

This is used to deal with 502 errors reported due to PHP execution time is too long.

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

Need to be combined with nginx.conf configuration.

Fastcgi_connect_timeout 300 is fastcgistered, sendings, timeout 300, fastcgigs, readouts, timeout 300

4. Slow log

Request_slowlog_timeout = 2slowlog = / home/wwwlogs/php_fpm_slow.log

NOTE:

The purpose of opening the slow log is to track and analyze that the php script has been executed for more than the set request_slowlog_timeout time, and if it exceeds this set time, then the script will be recorded.

5. Shut down and restart

Kill-INT `cat / usr/local/php/var/run/php- fpm.pid`kill-USR2 `cat / usr/local/php/var/run/php- fpm.pid`

6. Check the number of processes

# ps aux | grep-c php-fpm122

Or

# ps aux | grep php-fpm | wc-L122 at this point, the study on "PHP-FPM configuration and tuning" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report