Apache's prefork mode and worker mode The

  

prefork mode This multiprocessing module (MPM) implements a non-threaded, pre-derived web server that works like Apache 1.3. It is suitable for systems that do not have thread-safe libraries and need to avoid thread compatibility issues. It is the best MPM in the case where each request is made independent of each other, so that if one request has a problem, it will not affect other requests.

This MPM has a strong self-adjusting ability and requires only a few configuration commands to adjust. The most important thing is to set MaxClients to a large enough value to handle potential request spikes, but not too large, so that the memory used needs to exceed the size of physical memory.

worker Mode This Multiprocessing Module (MPM) enables web servers to support mixed multi-threaded multi-processes. Since threads are used to process requests, massive requests can be processed, while system resources are less expensive than process-based MPMs. However, it also uses multiple processes, each with multiple threads to get the stability of the process-based MPM.

The most important instructions for controlling this MPM are the ThreadsPerChild directive that controls the number of threads allowed to be created per child process, and the MaxClients directive that controls the total number of threads allowed to be established.


Prefork and worker mode switch 1. Change the current prefork mode startup file to mv httpd httpd.prefork2. Change the worker mode startup file to mv httpd.worker httpd3. Modify Apache configuration The file vi /usr/local/apache2/conf/extra/httpd-mpm.conf finds the following section inside, and can modify the load and other parameters appropriately: <IfModule mpm_worker_module>StartServers 2MaxClients 150MinSpareThreads 25MaxSpareThreads 75ThreadsPerChild 25MaxRequestsPerChild 0</IfModule>4. Start the service /usr/local/apache2/bin/apachectl restart and switch to worker mode to start apache2

In stability and security considerations, it is not recommended to change the operation mode of apache2, use the system default prefork. In addition, many php modules can't work in the worker mode. For example, the php that comes with redhat linux can't support thread safety. So it is best not to switch the working mode.

Compare for prefork and worker patterns The prefork mode uses multiple subprocesses, each with only one thread. Each process can only maintain one connection at a certain time. On most platforms, Prefork MPM is more efficient than Worker MPM, but memory usage is much larger. Prefork's wireless program design will have advantages over workers in some cases: it can use third-party modules that don't handle thread-safety, and it's easier to debug for platforms that have trouble debugging threads.

The worker mode uses multiple child processes, each with multiple threads. Each thread can only maintain one connection at a certain time. In general, on a high-traffic HTTP server, Worker MPM is a good choice because the memory usage of Worker MPM is much lower than that of Prefork MPM. But the worker MPM is also imperfect. If a thread crashes, the whole process will be together with all its threads. "Dead" is a system that must be recognized by the system as "running memory". Every thread is safe & rdquo;.

In general, the prefork method is slightly faster than the worker, but it requires a little more cpu and memory resources than the woker.

prefork mode configuration details<IfModule mpm_prefork_module>ServerLimit 256StartServers 5MinSpareServers 5MaxSpareServers 10MaxClients 256MaxRequestsPerChild 0</IfModule>ServerLimit The default MaxClient is 256 threads at most. If you want to set a larger value, add ServerLimit parameter. 20000 is the maximum value of the ServerLimit parameter. If you need more, you must compile apache, and you don't need to recompile Apache before. Prerequisites: Must be placed in front of other directives

StartServers specifies the number of child processes that are created when the server starts. Prefork defaults to 5.

MinSpareServers specifies the minimum number of free child processes. The default is 5. If the current number of free child processes is less than MinSpareServers, Apache will spawn a new child process at a rate of one per second. This parameter should not be set too large.

MaxSpareServers sets the maximum number of free child processes, the default is 10. If there are currently idle child processes that exceed the number of MaxSpareServers, the parent process will kill the extra child processes. This parameter should not be set too large. If you set the value of this directive to be smaller than MinSpareServers, Apache will automatically change it to ”MinSpareServers+1″.

MaxClients limits the maximum number of client access requests at the same time (the number of concurrent threads in a single process). The default is 256. Any request that exceeds the MaxClients limit will enter the waiting queue. Once a link is released, the request in the queue will be serviced. To increase this value, you must increase ServerLimit at the same time.

MaxRequestsPerChild The maximum number of requests that each child process will allow for the servo during its lifetime. The default is 10000. After the limit of MaxRequestsPerChild is reached, the child process will end. If MaxRequestsPerChild is ”0″, the child process will never end. Setting MaxRequestsPerChild to a non-zero value has two advantages: 1. It prevents (accidental) memory leaks from running indefinitely, thus running out of memory. 2. Give the process a finite lifetime, which helps reduce the number of active processes when the server load is reduced.

Worker Mode Configuration Details<IfModule mpm_worker_module>StartServers 2MaxClients 150MinSpareThreads 25MaxSpareThreads 75ThreadsPerChild 25MaxRequestsPerChild 0</IfModule>

The number of child processes created when the StartServers server starts, the default value is ”3″.

MaxClients allows the maximum number of simultaneous access requests (maximum number of threads). Any request that exceeds the MaxClients limit will enter the waiting queue. The default value is < 400″, 16 (ServerLimit) multiplied by 25 (ThreadsPerChild). Therefore, to increase MaxClients, you must increase the value of ServerLimit.

MinSpareThreads 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 generate a new idle thread.

MaxSpareThreads sets the maximum number of idle threads. The default value is <quo;250″. This MPM will monitor the number of idle threads based on the entire server. If there are too many total idle threads in the server, the child process will kill the extra idle threads. The range of values ​​for MaxSpareThreads is limited. Apache will automatically correct the value you set according to the following restrictions: the worker requires it to be greater than or equal to the sum of MinSpareThreads plus ThreadsPerChild.

ThreadsPerChild The number of resident execution threads established by each child process. The default is 25. When the child process establishes these threads at startup, it no longer creates new threads.

MaxRequestsPerChild sets the maximum number of requests each child process will allow for its lifetime. After the limit of MaxRequestsPerChild is reached, the child process will end. If MaxRequestsPerChild is ”0″, the child process will never end. Setting MaxRequestsPerChild to a non-zero value has two advantages: 1. It prevents (accidental) memory leaks from running indefinitely, thus running out of memory. 2. Give the process a finite lifetime, which helps reduce the number of active processes when the server load is reduced. Note that for KeepAlive links, only the first request will be counted. In fact, it changes the behavior of each child process to limit the maximum number of links.

Copyright © Windows knowledge All Rights Reserved