Apache is a relatively good middleware for static resources, but not very good for dynamic requests, and Tomcat is the opposite.
Apache uses more work mode, mainly prefork and worker two modes
1. Prefork mode
Prefork mode is the process work mode, using multiple child processes, with only one thread per child process. Each process can only maintain one connection at a certain time. On most platforms, the PREFORKMPM is more efficient than the worker mpm, but the memory usage is much larger. Prefork's wireless path design in some cases will be more advantageous than the worker: he is able to use third-party modules that do not handle thread safety, and he is more likely to debug some of the platforms that are difficult to debug.
The specific configuration is as follows:
serverlimit
//maximum number of processes to start, the default maxclient maximum is 256 processes, if you want to configure a larger value, you have to add serverlimit this parameter. 20000 is the maximum value of the Serverlimit parameter. If it needs to be larger, Apache must be compiled, previously without recompiling Apache. Effective premise: Must be placed in front of other directives
startservers 5
//Initialize several processes to start, prefork default is 5.
minspareservers 5
//minimum number of idle processes, default is 5. If the current number of idle child processes is less than minspareservers, then Apache will produce a new subprocess at a maximum speed of one second. Do not set this parameter too large.
maxspareservers
//maximum number of idle processes, default is 10. If there are currently more than maxspareservers of idle child processes, then the parent process will kill the extra child processes. Do not set this parameter too large. If you configure the value of this directive to be smaller than Minspareservers, Apache will automatically modify it to "minspareservers+1".
maxclients
//At the same time, Apache provides the maximum number of connections to the client at the same time (the number of concurrent threads per process), which defaults to 256. Any request exceeding the maxclients limit will enter the waiting queue, and once a link is freed, the request in the queue will be serviced. To increase this value, you must increase the serverlimit at the same time.
maxrequestsperchild 10000
//The maximum number of requests that each child process is allowed to process during its lifetime, which defaults to 10000. When the Maxrequestsperchild limit is reached, the child process will end. If Maxrequestsperchild is "0", the child process will never end.
There are two benefits to configuring Maxrequestsperchild to a non-0 value:
1. Ability to prevent (accidental) memory leaks from being carried out indefinitely, thus exhausting memory.
2. Give the process a limited lifespan, thus helping to reduce the number of active processes when the server load is reduced.
Working mode:
A separate control process (the parent process) is responsible for generating child processes that are used to listen for requests and respond. Apache always tries to keep some spare (spare) or idle sub-processes used to greet
The upcoming request. This eliminates the need for the client to wait for the child process to be generated before the service is received. In Unix systems, the parent process typically runs as root for 80 ports, and Apache-generated child processes are typically run with a low-privileged user. User and group directives are used to configure low-privileged users of child processes. The user who runs the child process must have read access to the content he serves, but must have as few permissions as possible for resources other than the content of the service.
Prefork Mode monitoring:
Based on the above configuration, the monitoring results are as follows:
1 daemons, 7 idle processes, plus equal to the number of startservers in the configuration, and 7 idle processes within the Minspareservers and Maxspareservers range.
2. Worker Mode
Worker mode is a multi-process multithreaded mode that uses multiple child processes, each with multiple threads. Each thread can only maintain one connection at a certain time. In general, working with the worker mpm on a high-traffic HTTP server is a good choice because workermpm memory usage is much lower than prefork MPM. But the worker mpm is also imperfect, and if a thread crashes, the entire process will "die" along with any of its threads. Because threads share memory space, a program must be recognized by the system as "every thread is safe" when it runs.
The specific configuration is as follows:
Serverlimit
Maximum number of processes allowed to be configured by the server. The value must be greater than or equal to Maxclients/threadsperchild. This instruction is used in conjunction with Threadlimit to configure the value of the MaxClients maximum allowable configuration. Any changes to this instruction during the reboot will be ignored, but the changes to the maxclients will take effect.
Threadlimit
The maximum number of threads that can be configured per child process. This instruction configures the maximum number of threads that can be configured for each child process threadsperchild. This value should be maintained with the maximum value that Threadsperchild may reach.
To Threadlimit >= Threadsperchild. Any changes to this instruction during the reboot will be ignored, but the changes to the Threadsperchild will take effect.
startservers 5
Number of child processes established at server startup.
minsparethreads
Minimum number of idle threads, this MPM will monitor the number of idle threads based on the entire server. If the total number of idle threads in the server is too small, the child process will generate a new idle thread.
maxsparethreads
Configures the maximum number of idle threads. The default value is "250". This MPM will monitor the number of idle threads based on the entire server. If the total number of idle threads in the server is too many, the child process will kill the extra idle thread maxsparethreads the range of values is limited. Apache will automatically fix your configuration according to the following restrictions
maxclients
Maximum number of Access requests (maximum number of threads) that allow simultaneous servos. Any requests exceeding the maxclients limit will enter the waiting queue. Therefore, to increase maxclients, you must increase the value of serverlimit at the same time. Calculation formula: Maxclients<=serverlimit *threadsperchild, set the initial value to (the maximum physical memory in MB/2), and then dynamically adjust according to the load situation. such as a 4G memory machine, then the initial value is 4000/2=2000
Threadsperchild
The number of resident execution threads that each child process establishes, and the child processes are no longer establishing new threads when they are established at startup.
Maxrequestsperchild 0
Configures the maximum number of requests that each child process is allowed to servo during its lifetime. When the Maxrequestsperchild limit is reached, the child process will end. If Maxrequestsperchild is "0", the child process will never end.
There are two benefits to configuring Maxrequestsperchild to a non-0 value:
1. Ability to prevent (accidental) memory leaks from being carried out indefinitely, thus exhausting memory.
2. Give the process a limited lifespan, thus helping to reduce the number of active processes when the server load is reduced.
Note: For a keepalive link, only the first request is counted.
How
works:
The number of threads that each process can have is fixed. The server increases or decreases the number of processes depending on the load. A separate control process (parent process) is responsible for the creation of child processes. Each subprocess can establish a
Threadsperchild number of service threads and a listener thread that listens to the access request and passes it to the service thread for processing and answering. Apache always tries to maintain a standby (spare) or free service thread pool. This way, the client does not have to wait for new threads or new processes to be established to be processed. In Unix, in order to be able to bind port 80, the parent process is typically started as root, and then Apache creates child processes and threads with lower-privileged users. The user and group directives are used to configure permissions for the Apache child process. Although the child process must have read access to the content it provides, it should give him less privileges as much as possible. Also, unless suEXEC is used, the permissions configured by these directives will be inherited by the CGI script.
Formula:
Threadlimit >= Threadsperchild
MaxClients = Minsparethreads+threadsperchild
Hard limit:
The two instructions for Serverlimi and threadlimit determine the number of active child processes and the hard limit of the number of threads per child process. To change this hard limit you must completely stop the server and then start the server (a direct restart is not possible).
Apache has a hard limit in compiling serverlimit, and you can't go beyond this limit.
Prefork MPM Max is "Serverlimit 200000"
The maximum number of other MPM (including work MPM) is "Serverlimit 20000
Apache has a hard limit in compiling threadlimit, and you can't go beyond this limit.
Mpm_winnt is "Threadlimit 15000"
Other MPM (including work prefork) are "Threadlimit 20000
Attention:
Be especially careful when using serverlimit and Threadlimit. If you configure Serverlimit and Threadlimit as a higher value, you will have too much shared memory to be allocated. When configured to exceed the system's processing power, Apache may not boot, or the system will become unstable.
Worker Mode monitoring
(1) The first case
Initialize 4*25=100 is greater than maxsparethreads 75, so to kill a process, that is, 25 threads, so the last left 100-25 = 75, and then lose a listener thread, and finally left 74 idle threads
(2) The second case
Initialize the 3*25=75, which is 1 daemon threads, 74 idle threads, because less than minsparethreads 75, need to start a process that is 25 threads, so finally 1 daemon threads, 99 idle threads. Some people will ask 99 is greater than maxsparethreads 75, this item does not meet. This is because the maxsparethreads configuration is invalidated when the actual number of threads re-verifies the minsparethreads and maxsparethreads conflicts, whichever is minsparethreads
(3) The third case
Initialize 3*30=90,1 Guardian, 89 idle, greater than maxsparethreads 75, so to kill 1 processes that 30 threads, the last remaining 1 guard, 59 idle
(4) Fourth case
Initialize 5*40=200,1 Guardian, 199 idle, greater than maxsparethreads 150, kill a process that is 40 threads, idle 159, or greater than maxsparethreads 150, and then kill a process, idle to 119, At this point is smaller than the value of Minsparethreads, and then start a process to become idle to 159 and greater than maxsparethreads, then the conflict, Maxsparethreads is invalid, so the end is 1 Guardian, 159 idle
Summarize:
How to properly configure Apache
To configure according to the actual situation, is to ensure that the number of concurrent, or to reduce memory consumption. If the system does not require high concurrency for most of the time, the number of processes/threads that are initialized can be set less. If most of the time the system requires a high amount of concurrency, the number of initialization processes/threads can be configured a little more so that the process/thread is frequently started and killed, which makes it easier to consume system resources.
Apache Monitoring tuning