In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
1. Main configuration parameters of php-fpm.conf
Pm = dynamic; indicates which process quantity management method is used
Dynamic indicates that the number of php-fpm processes is dynamic, starting with the number specified by pm.start_servers. If there are more requests, it will automatically increase to ensure that the number of idle processes is not less than pm.min_spare_servers. If there are a large number of processes, it will be cleaned accordingly to ensure that the number of redundant processes is not more than pm.max_spare_servers. Static indicates that the number of php-fpm processes is static, and the number of processes is the number specified by pm.max_children from beginning to end, no longer increasing or decreasing. Pm.max_children = 300; number of php-fpm processes opened in static mode pm.start_servers = 20; number of initial php-fpm processes in dynamic mode pm.min_spare_servers = 5; minimum number of php-fpm processes in dynamic mode
Pm.max_spare_servers = 35; maximum number of php-fpm processes in dynamic mode
Note: numerical settings, with reference to your actual hardware configuration, can be calculated with reference to the total memory / 30m.
If pm is set to static, then only the parameter pm.max_children takes effect. The system will open a set number of php-fpm processes.
If pm is set to dynamic, then the pm.max_children parameter is invalid and the next three parameters take effect. The system will start pm.start_servers php-fpm processes at the beginning of php-fpm operation, and then dynamically adjust the number of php-fpm processes between pm.min_spare_servers and pm.max_spare_servers according to the needs of the system.
1. Pm mode selection
In fact, like Apache, running PHP programs will more or less have memory leaks after execution. This is why a php-fpm process takes up only about 3 megabytes of memory at the beginning and rises to 20-30 megabytes after a period of time.
For servers with large memory (such as more than 8G), static max_children is actually more appropriate, because it does not require additional process number control and will improve efficiency. Because frequently switching php-fpm processes can sometimes lag, it is better to turn on static when the memory is large enough. The amount can also be obtained according to the total memory / 30m, for example, 8GB memory can be set to 100, so the memory consumed by php-fpm can be controlled in the appearance of 2G-3G.
If the memory is slightly smaller, such as 1GB, then specifying the number of dynamic processes is more conducive to server stability. This ensures that php-fpm only gets enough memory and allocates a small amount of memory to other applications, which will make the system run more smoothly.
For servers with small memory, such as 256m VPS, even if 10 php-cgi processes consume 200m of memory based on a 20m memory, the crash of the system should be normal.
Therefore, you should try to control the number of php-fpm processes, generally clear the memory consumed by other applications, give it a small amount of static, it will make the system more stable.
Or use dynamic mode, because dynamic mode will end the excess processes and can reclaim and release some memory, so it is recommended to use on servers or VPS with less memory, the maximum amount is based on the total memory / 20m.
For example, for the 512m VPS, it is recommended that the pm.max_spare_servers be set to 20. As for pm.min_spare_servers, it is recommended to set it according to the load of the server, and the more appropriate value is between 5 and 10.
Summary: dynamic is recommended for small memory (pm = dynamic) and static is recommended for large memory (pm = static).
2. How big is the pm.max_children setting
In principle, the larger the better, the more php-cgi processes will be processed quickly, and the number of queued requests will be very small.
Setting "max_children" also needs to be set according to the performance of the server.
The calculation method is as follows:
Generally speaking, a server normally consumes about 20M~30M per php-cgi, so I set my "max_children" to 40. 20M*40=800M means that the memory consumed by all PHP-CGI at the peak is less than 800m, which is lower than my effective memory 2Gb.
If my "max_children" setting is small, such as 5-10, then the php-cgi will be "very tired", the processing speed is slow, the waiting time is longer, and the CPU is high.
The 504 Gateway Time-out error occurs if the request is not processed for a long time, and the 502 Bad gateway error occurs if the tired php-cgi that is working on encounters a problem.
The better setting mode of max_children is set according to req/s (throughput, the maximum number of requests processed by the server per unit time, unit req/s). If the program has a processing capacity of 100 req/s, it is better to set 100, which is adjusted dynamically.
3. How big is the request_terminate_timeout setting
The calculation method is as follows:
If your server performance is good enough and the broadband resources are sufficient, you can set "request_terminate_timeout" to 0s directly if the PHP script does not have loops or BUG. 0s means to keep the PHP-CGI running without a time limit.
And if you can't do this, that is to say, there may be a BUG in your PHP-CGI, or your broadband is insufficient or some other reason causes your PHP-CGI to fake death, then it is recommended that you assign a value to "request_terminate_timeout", which can be set according to the performance of your server.
Generally speaking, the better the performance, the higher you can set it, 20-30 minutes. Because my server PHP script takes a long time to run, some of it may take more than 10 minutes, so I set it for 900s, which does not cause the PHP-CGI to die and cause the error of 502Bad gateway.
Configure php slow logs for monitoring
1. Enable the slow log method
If you use php-fpm to manage php, you can turn it on as follows:
First open the php-fpm.conf configuration file.
Vim / usr/local/php/etc/php-fpm.conf
The settings before PHP 5.3.3 are as follows:
5s
< value name="slowlog">Logs/php-fpm-slowlog.log
Or
After PHP 5.3.3, the following settings are set:
Request_slowlog_timeout = 5sslowlog = / usr/local/php/var/log/php-fpm-slowlog.logrequest_terminate_timeout = 10s
Description:
Request_slowlog_timeout is how long the script takes to record to the log file; slowlog is the storage path of the log file; request_terminate_timeout terminates the process that takes too long to execute
2. Use of slow log
When enabled, if a script is executed for more than the specified time, information similar to the following will be written to the specified log file:
[06-Dec-2017 20:05:31] [pool www] pid 22271script_filename = / home/wwwroot/default/tz/tz.php [0x00007f75e662a398] preg_match_all () / home/wwwroot/default/tz/tz.php:453 [0x00007f75e6627f08] sys_linux () / home/wwwroot/default/tz/tz.php:410
Since I put the slowlog log here in / usr/local/php/var/log/php-fpm-slowlog.log, I just need to check this log!
Vim / usr/local/php/var/log/php-fpm-slowlog.log
4. Slow log analysis
Here is a log picture to do a simple analysis
Description:
Script_filename is the entry file; session_start, session, run, start, sleep: indicates that the execution time of this method exceeds the execution time. The number after each line colon is the line number.
When enabled, it is also recorded in the error log file (php-fpm.log). As follows:
[06-Dec-2017 20:59:53] NOTICE: finished trace of 31450 [06-Dec-2017 20:59:56] WARNING: [pool www] child 31437, script'/ home/wwwroot/default/tz/tz.php' (request: "GET / tz/tz.php") executing too slow (1.047562 sec) Logging [06-Dec-2017 20:59:56] NOTICE: child 31437 stopped for tracing [06-Dec-2017 20:59:56] NOTICE: about to trace 31437 [06-Dec-2017 20:59:56] NOTICE: finished trace of 31437 [06-Dec-2017 21:00:05] WARNING: [pool www] child 31448, script'/ home/wwwroot/default/tz/tz.php' (request: "GET / tz/tz.php") executing too slow (1.013736 sec) Logging [06-Dec-2017 21:00:05] NOTICE: child 31448 stopped for tracing [06-Dec-2017 21:00:05] NOTICE: about to trace 31448 [06-Dec-2017 21:00:05] NOTICE: finished trace of 31448 [06-Dec-2017 21:00:10] WARNING: [pool www] child 31481, script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.134845 sec) Logging [06-Dec-2017 21:00:10] WARNING: [pool www] child 31478, script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.169301 sec), logging [06-Dec-2017 21:00:10] WARNING: [pool www] child 31475 Script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.009847 sec), logging [06-Dec-2017 21:00:10] WARNING: [pool www] child 31468, script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.019848 sec) Logging [06-Dec-2017 21:00:10] WARNING: [pool www] child 31455, script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.147848 sec), logging [06-Dec-2017 21:00:10] WARNING: [pool www] child 31451 Script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.076841 sec), logging [06-Dec-2017 21:00:10] WARNING: [pool www] child 31447, script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.119846 sec) Logging [06-Dec-2017 21:00:10] WARNING: [pool www] child 31443, script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.177849 sec), logging [06-Dec-2017 21:00:10] WARNING: [pool www] child 31436 Script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.092818 sec), logging [06-Dec-2017 21:00:10] WARNING: [pool www] child 31433, script'/ home/wwwroot/default/TTTTT_GAME/index.php' (request: "GET / TTTTT_GAME/index.php") executing too slow (1.162842 sec) Logging [06-Dec-2017 21:00:10] NOTICE: child 31433 stopped for tracing [06-Dec-2017 21:00:10] NOTICE: about to trace 31433 [06-Dec-2017 21:00:10] ERROR: failed to ptrace (PEEKDATA) pid 31433: Input/output error (5) [06-Dec-2017 21:00:10] NOTICE: finished trace of 31433 [06-Dec-2017 21:00:10] NOTICE: child 31436 stopped for tracing [06-Dec-2017 21:00:10] NOTICE: about to trace 31436
Configure the maximum number of file handles that can be opened by the php-fpm process
Rlimit_files = 1024
Default is 1024, and this value does not need to be configured
Fourth, php-fpm occupies cpu and memory too high 100% solution
Server php-fpm suddenly takes up too much cpu and memory, and its server configuration is 4 cores and 8 gigabytes of memory. "502Bad gateway" often occurs because php-fpm occupies too much cpu.
Server environment: LNMP one-click installation package
The "elegant black probe" is used to check the performance of the server, and the results are as follows:
Of course, in addition to using probes, if you use the system command on the server: top can also be viewed:
1. Interpretation of CPU indicators
Cpu (s): 0.0%us, 0.5%sy, 0.0%ni, 99.5%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Us: percentage of CPU occupied by user space sy: percentage of CPU occupied by kernel space ni: percentage of processes within user process space that have changed priorities CPU percentage id: percentage of idle CPU wa: percentage of CPU time waiting for input and output hi: hardware interrupt si: software interrupt st: real-time
The solution is as follows:
Check the log more, solve the problem according to the log, and finally check the configuration file. Do you need configuration and tuning?
As there are some steps that are not easy to express, please take a simple look at the logic. You need to know a lot of things to understand it. Please take a look at the links in this article, otherwise you can't understand them.
2. View its php-fpm.conf configuration
As you can see from the above configuration file, it is dynamic, and the default number of startup processes is 4, the maximum is 6, and the minimum is 4.
3. View the average load of linux
As you can see from the above, the six php-fpm processes all occupy a high amount of cpu space, and the average load (load average) is as follows:
Average load per minute: 2.32
5-minute average load: 2.18
Average load for 15 minutes: 3.95
It can be said that its average load is now close to its total number of cpu cores: 4; server configuration upgrades need to be considered!
4. Linux average load Load Average
1 、 Load Average
The system load (System Load) is a measure of how busy the system CPU is, that is, how many processes are waiting to be scheduled by CPU (the length of the queue for the process to wait).
Average load (Load Average) is the average load of the system over a period of time, which usually takes 1 minute, 5 minutes, and 15 minutes.
2. View Load Average
Top, w, uptime and other commands can all view the system load.
3. Three numerical descriptions of Load Average
Let me take the load average:1.97,2.14,2.99 in the figure as an example:
The first place 1.97: the average load of the last 1 minute is 2.14: the average load of the last 5 minutes is the third place 2.99: the average load of the last 15 minutes
4. The meaning of Load Average value
1. Single core processor
(for example, 1 1-core cpu)
Suppose our system is single-CPU, single-core, compare it to an one-way road, and compare CPU tasks to cars.
When there are not many cars, load 1
2. Multicore processor
(for example: 2 cpu or a 2-core cpu)
We often find that the server Load > 1 still works well because the server is a multicore processor (Multi-core).
Suppose our server a CPU is 2 cores, then it will mean that we have 2 roads, when our Load = 2, all the roads are full of vehicles.
Tip:
Chip manufacturers often have multiple CPU cores within one CPU, which is called multi-core CPU.
In terms of system load, multicore CPU is similar to multiple CPU, so when considering system load, you must consider how many CPU this computer has and how many cores each CPU has. Then, divide the system load by the total number of cores, as long as the load of each core does not exceed 1.0, it indicates that the computer is running normally.
3. View server cpu information
Cat / proc/cpuinfo
4. View the total number of cpu cores on the server
Grep 'model name' / proc/cpuinfo | wc-l or grep-c' model name' / proc/cpuinfo
5. Load Average alert value (single core)
Load
< 0.7时:系统很闲,马路上没什么车,要考虑多部署一些服务 0.7 < Load < 1时:系统状态不错,马路可以轻松应对 Load == 1时:系统马上要处理不多来了,赶紧找一下原因 Load >At 1: 00: the road is so busy that every car entering the road can't run very fast.
6. Load Average key value (single core)
Usually we first look at the 15-minute load, and if the load is high, then look at the 1-minute and 5-minute load to see if there is a downward trend.
If the load value for 1 minute is more than 1, then we don't have to worry, but if the load exceeds 1 for 15 minutes, we need to see what happens. So we need to look at these three values according to the actual situation.
As we all know, "load average" returns a total of three averages: 1 minute system load, 5 minutes system load, and 15 minutes system load.
If only one minute of the system load is greater than 1.0, the other two time periods are less than 1.0, which indicates that it is only a temporary phenomenon and the problem is not big.
If the average system load is greater than 1.0 within 15 minutes (after adjusting the number of CPU cores), the problem persists, not temporarily. Therefore, you should mainly observe the "15-minute system load" and use it as an indicator of the normal operation of the computer.
7. Specific analysis according to the specific situation (single core)
1 minute Load > 1minute 5 minute Load1,15 minute Load1,5 minute Load > 1minute 15 minute Load > 1: short, medium and long term are busy, the system is "congested" 1 minute Load1,15 minute Load > 1: idle in the short term, busy in the medium and long term, don't be nervous, the system "congestion is improving"
184 total: total number of processes
4 running:4 number of running processes
The number of sleep processes of 143sleeping:180
0 stoppe:0 number of stopped processes
0 zombie:0 number of frozen processes
5. Change the php-fpm.conf configuration file for tuning
Since the server has 8 GB of memory, it should be able to start about 200 php-fpm processes, so I modified it as follows:
In addition to the above configuration tests, I also changed "pm= dynamic" to "pm= static" configuration for testing, and the results are not satisfactory. The specific results are as follows:
6. Check the average load of linux again
As can be seen from the above configuration, although each php-fpm takes up less cpu space, the total amount is still close to 100%.
And the average load (load average) is as follows:
Average load per minute: 289.73
5-minute average load: 264.27
15-minute average load: 179.20
It can be said that its average load now far exceeds the total number of cpu cores: 4; the server configuration must be upgraded.
The reason why it is so high, in addition to its own server configuration can not keep up with, another is that I set pm.max_spare_servers to 512, if a thread occupies 20m of memory, it needs 51220m; its current server only has 8G of memory and 4 cores of CPU;, so this load can reach more than 200s. This can be regarded as a small mistake in the test.
Normally, it is sufficient for a thread to take up 20,30m memory, 8G memory and set 100cm 200 memory.
184 total: 678 total processes
4 running:211 number of running processes
The number of sleep processes of 143sleeping:327
0 stoppe:140 number of stopped processes
0 zombie:0 number of frozen processes
Summary:
Because of the error test, I re-modified the configuration file to "pm= static" and "pm.max_children = 100". It is totally unreasonable that the cpu and memory are still very high and the load is very high.
Just imagine: for a server with 4 cores and 8 gigabytes of memory, the larger the thread setting, the higher the CPU usage (set in the maximum allowable range of memory), and the smaller the setting, the smaller the CPu footprint, which is not normal. The most important thing is that although the php-fpm thread is small and takes up less cpu space, there are more 502s. This further shows that the current configuration of the server can not support the existing business.
Now my friend has applied to the headquarters for a server with 8 cores and 16 GB of memory. After the application is successful, the default starting thread is 100, and the maximum thread is 200. Now it has returned to normal, as shown below:
It can also be explained here that sometimes the problem is not your own reason, or the reason why the server hardware configuration can not keep up.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.