Several working modes of apache and apache
Although apache servers are used in the project, they are all configured with parameters that can be used or simply adjusted to be able to accept high concurrency and are not carefully understood. In addition, apache recently used the mpm_event mode, it was found that the cpu usage was very high. At first, I thought it was an apache and php configuration problem. Later I modified the event parameter to restore to normal.
Next we will introduce the common prefork and event modes.
1. prefork
A non-threaded, pre-derived module, which means that prefork will use multiple sub-processes, each of which has only one thread to accept the request; each process can only process one http link, until the link is released.
<IfModule mpm_prefork_module>ServerLimit 20000StartServers 5MinSpareServers 5MaxSpareServers 10MaxClients 1000MaxRequestsPerChild 0</IfModule>
StartServers 5
Number of subprocesses created after apache is started. The default value of prefork is 5.
MinSpareServers 5
Minimum number of idle processes. The default value is 5. When the number of idle sub-processes is less than 5, apache will immediately generate a new sub-process.
MaxSpareServers 10
The maximum number of idle processes. The default value is 10. When the number of idle sub-processes exceeds 10, the parent process will kill the redundant sub-processes. When the load on our website is too large, we can increase MinSpareServers and MaxSpareServers as appropriate.
MaxClients 1000
The number of http requests that apache can accept. When the number of requests exceeds 1000, redundant requests will enter the Request queue until the previous request is processed. When there are a lot of remaining resources on our server but access is slow, we need to check if this parameter is set too low. Of course, the default value of Maxclients is 256. To increase this value, you also need to increase serverlimit, but the maximum value of serverlimit is 20000.
Apache2.3.1 and later versions of MaxClients are called MaxRequestWorkers
MaxRequestPerChild 1000
The maximum number of requests that each sub-process can accept. If more than 1000 requests are received, the sub-process will be destroyed automatically. 0 indicates that the sub-process will never be destroyed. Although more requests can be received, memory leakage may occur.
2. event
The prefork and mpm methods are insufficient in extremely busy server applications. Although the HTTP Keepalive method can reduce the number of TCP connections and network load, the Keepalive must be bound to the service process or thread,
This causes a busy server to consume all threads. Event MPM is a new model to solve this problem. It separates service processes from connections. The processing speed on the server is very fast, and there is a very high click
The number of available threads is the critical resource limit. The Event MPM method is the most effective. A busy server working in the Worker MPM mode can withstand tens of thousands of visits per second (for example, in large news
And Event MPM can be used to handle higher loads. It is worth noting that Event MPM cannot work with secure HTTP (HTTPS) access.
The event and work share the same characteristics. They both process requests through threads. A parent process creates multiple child processes, and a child process creates multiple threads.
<IfModule mpm_event_module> ServerLimit 1000 StartServers 20 MinSpareThreads 25 MaxSpareThreads 1200 ThreadsPerChild 50 MaxRequestWorkers 2000 MaxConnectionsPerChild 1000</IfModule>
StartServers 20
After apache is started, 20 sub-processes are created. Because the default Serverlimit is 16, when StartServers is greater than 16, the error "changing ServerLimit to 1000 from original value of 16 not allowed" will be reported.
During restart ", but we can reset it through Serverlimit, but we need to stop the apache service and start it again. The direct restart is invalid.
MinSpareThreads 25
Minimum number of Idle threads
Maxsparethread s 1200
The maximum number of Idle threads. Note that this value must be greater than StartServers * ThreadsPerChild = 20*50 = 1000. If MaxSpareThreads is 800 or less than 1000
StartServers-MaxSpareThreads/50 = 4, then four processes will be killed. We can view through top. After restart, multiple processes will be generated, and redundant processes will be killed after a while.
ThreadsPerChild 50
Each process can generate 50 threads. Note that the default ThreadLimit is 64. When ThreadsPerChild is greater than 64, the error "ThreadsPerChild of 500 exceeds ThreadLimit of 64" is reported,
Decreasing to match ", but we can reset it through ThreadLimit, but we need to stop the apache service and start it again. The direct restart is invalid.
MaxRequestWorkers 2000
The maximum number of worker threads is equal to ServerLimit * ThreadPerChild. If the default ServerLimit is 16, 2000> 16*50. The log reports the error "MaxRequestWorkers of 1000 wowould require 20 ".
Servers and exceed ServerLimit of 16, decreasing to 800 ". In this case, we need to modify ServerLimit. We need to stop the apache service before starting it. The direct restart is invalid.
MaxConnectionsPerChild 1000
Maximum number of connections per Process
Ps: Since prefork and event are commonly used, I have re-understood the parameters based on my usual problems. It seems that the basic skills are not solid.
By the way, we can use the preceding command to check the mode used by httpd:
[root@usvr157 apache]$ /usr/local/apache/bin/httpd -lCompiled in modules: core.c mod_so.c http_core.c event.c