Prefork mode
This multi-path processing module (MPM) implements a non-threaded, pre-derived Web server that works in a similar way to Apache 1.3. It is suitable for systems that do not have a thread-safe library and need to avoid threading compatibility issues. It is the best mpm to require each request to be independent of each other, so that if a request has a problem it will not affect the other request.
This MPM has a strong self-tuning capability and requires very little adjustment of the configuration instructions. The most important thing is to set the maxclients to a value that is large enough to handle the potential request spikes, and not too large to use more memory than the size of the physical memory.
Worker mode
This multi-path processing module (MPM) enables a network server to support mixed multithreaded multi-process. Because a thread is used to process requests, a large amount of requests can be processed, while the overhead of system resources is less than the process-based MPM. However, it also uses a multi-process, with each process having multiple threads to obtain the stability of the process-based MPM.
The most important instructions for controlling this mpm are the Threadsperchild instructions that control the number of threads allowed to be established by each subprocess, and the maxclients instruction that controls the number of buses allowed to be established.
Prefork and worker mode switching
1. Rename the current prefork mode startup file
MV httpd httpd.prefork
2. Rename the startup file for worker mode
MV Httpd.worker httpd
3. Modifying the Apache configuration file
vi/usr/local/apache2/conf/extra/httpd-mpm.conf
Find the following paragraph inside, you can properly modify the load parameters such as:
<ifmodule mpm_worker_module>
startservers 2
maxclients
minsparethreads
maxsparethreads
Threadsperchild
Maxrequestsperchild 0
</IfModule>
4. Restart the service
/usr/local/apache2/bin/apachectl Restart
You can switch to worker mode to start apache2
In the stability and security considerations, it is not recommended to replace the operating mode of APACHE2, using the system default Prefork can be. In addition, many PHP modules do not work in worker mode, for example, Redhat Linux comes with PHP that does not support thread safety. Therefore, it is best not to switch to working mode.
Comparison of prefork and worker modes
Prefork mode uses 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 Prefork MPM 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 a worker: it can use third-party modules that do not handle thread safety, and it is easier to debug for platforms that have difficulty debugging threads.
Worker mode uses multiple child processes, each of which has multiple threads. Each thread can only maintain one connection at a certain time. In general, worker MPM is a good choice on a high-traffic HTTP server because the worker MPM uses much less memory than Prefork MPM. But the worker mpm is also imperfect, and if a thread crashes, the entire process will "die" along with all of its threads. Because threads share memory space, a program must be recognized by the system as "every thread is safe" at run time.
Overall, the prefork is slightly faster than the worker, but it requires slightly more CPU and memory resources than Woker.
prefork Mode configuration detailed
<ifmodule mpm_prefork_module>
Serverlimit
startservers 5
minspareservers 5
Maxspareservers Ten
maxclients
Maxrequestsperchild 0
</IfModule>
Serverlimit
The default maxclient maximum is 256 threads, if you want to set a larger value, add serverlimit this parameter. 20000 is the maximum value of the Serverlimit parameter. If you need to be larger, you must compile Apache before you need to recompile Apache.
Effective premise: Must be placed in front of other directives
Startservers
Specifies the number of child processes that are established when the server starts, and Prefork defaults to 5.
Minspareservers
Specifies the minimum number of idle child processes, which defaults to 5. If the number of currently 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
Sets the maximum number of idle child processes, which defaults to 10. If there are currently more than maxspareservers of idle child processes, the parent process kills the extra child processes. Do not set this parameter too large. If you set the value of this directive to be smaller than Minspareservers, Apache will automatically modify it to "minspareservers+1".
MaxClients
Limit the number of client maximum access requests at the same time (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
The maximum number of requests that each child process allows for a servo during its lifetime, by default 10000. When the Maxrequestsperchild limit is reached, the child process will end. If Maxrequestsperchild is "0", the child process will never end. Setting Maxrequestsperchild to a value other than 0 has two benefits:
1. You can 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.
worker mode configuration detailed
<ifmodule mpm_worker_module>
startservers 2
maxclients
minsparethreads
maxsparethreads
Threadsperchild
Maxrequestsperchild 0
</IfModule>
Startservers
The number of child processes that were established when the server started, and the default value is "3".
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. The default value is "Serverlimit", multiplied by the result of (Threadsperchild). Therefore, to increase maxclients, you must increase the value of serverlimit at the same time.
Minsparethreads
The minimum number of idle threads, the default value is "75". 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 produce a new idle thread.
Maxsparethreads
Sets 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 kills the extra idle threads. The value range of the maxsparethreads is limited. Apache will automatically fix the values you set as follows: The worker requires that it is greater than or equal to Minsparethreads plus threadsperchild.
Threadsperchild
The number of resident execution threads established by each child process. The default value is 25. When a child process establishes these threads at startup, no new threads are established.
Maxrequestsperchild
Sets the maximum number of requests per child process to allow the 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. Setting Maxrequestsperchild to a value other than 0 has two benefits:
1. You can 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 keepalive links, only the first request is counted. In fact, it changes the behavior of each child process to limit the maximum number of links.
This article from "Silence" blog, declined reprint!
Apache Working mode and configuration