Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of prefork mode and worker mode of Apache

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the introduction of prefork mode and worker mode of Apache". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the introduction of Apache's prefork mode and worker mode".

Prefork mode

This multiplexing module (MPM) implements a non-threaded, pre-derived web server that works similar to Apache 1.3. It is suitable for systems that do not have thread-safe libraries and need to avoid thread compatibility problems. It is the best MPM that requires that each request be independent of each other, so that if there is a problem with one request, other requests will not be affected.

This MPM has a strong self-tuning ability and requires very little adjustment of configuration instructions. The most important thing is to set MaxClients to a numeric value large enough to handle potential request peaks, but not so large that you need to use more memory than physical memory.

Worker mode

This multiplexing module (MPM) enables the network server to support mixed multithreading and multiprocess. Because threads are used to process requests, it can handle a large number of requests, and the overhead of system resources is less than that of process-based MPM. However, it also uses multiple processes, each with multiple threads, to achieve the stability of a process-based MPM.

The most important instructions to control this MPM are the ThreadsPerChild instruction that controls the number of threads allowed to be established by each child process, and the MaxClients instruction that controls the number of buses allowed to be established.

Switching between prefork and worker modes

1. Rename the current prefork mode startup file

Mv httpd httpd.prefork

two。 Rename the startup file in worker mode

Mv httpd.worker httpd

3. Modify Apache configuration file

Vi / usr/local/apache2/conf/extra/httpd-mpm.conf

Find the following paragraph, and modify the load and other parameters as appropriate:

StartServers 2

MaxClients 150

MinSpareThreads 25

MaxSpareThreads 75

ThreadsPerChild 25

MaxRequestsPerChild 0

4. Restart the service

/ usr/local/apache2/bin/apachectl restart

You can start apache2 in worker mode.

For stability and security considerations, it is not recommended to change the operation mode of apache2, just use the system default prefork. In addition, many php modules do not work in worker mode, for example, redhat linux's native php does not support thread safety. So it's best not to switch working modes.

Comparison of prefork and worker modes

The prefork mode uses multiple child processes, each with only one thread. Each process can maintain only one connection at a certain time. On most platforms, Prefork MPM is more efficient than Worker MPM, but uses much more memory. Prefork's wireless range design will have an advantage over worker in some cases: it can use third-party modules that do not handle thread safety properly, and it is easier to debug for platforms where thread debugging is difficult.

The worker mode uses multiple child processes, each with multiple threads. Each thread can maintain only one connection at a certain time. Generally speaking, on a high-traffic HTTP server, Worker MPM is a better choice because Worker MPM uses much less memory than Prefork MPM. But worker MPM is also imperfect. If a thread crashes, the whole process will die along with all 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 approach is slightly faster than worker, but it also requires slightly more cpu and memory resources than woker.

Detailed explanation of prefork mode configuration

ServerLimit 256

StartServers 5

MinSpareServers 5

MaxSpareServers 10

MaxClients 256

MaxRequestsPerChild 0

ServerLimit

The default MaxClient is 256threads. If you want to set a larger value, add the parameter ServerLimit. 20000 is the maximum value of the parameter ServerLimit. If larger is needed, apache must be compiled; previously, there was no need to recompile Apache.

Effective premise: must be placed in front of other instructions

StartServers

Specifies the number of child processes established when the server starts, and prefork defaults to 5.

MinSpareServers

Specifies the minimum number of free child processes, which defaults to 5. If the current number of idle child processes is less than MinSpareServers, then Apache will generate new child processes at a maximum rate of one per second. Do not set this parameter too large.

MaxSpareServers

Set the maximum number of free child processes, default to 10. If there are currently more free child processes than MaxSpareServers, the parent process will kill the extra child processes. Do not set this parameter too large. If you set the value of the instruction to be less than MinSpareServers, Apache will automatically change it to "MinSpareServers+1".

MaxClients

Limit the maximum number of client access requests at a 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, and once a link is released, the request in the queue will be served. To increase this value, you must also increase ServerLimit.

MaxRequestsPerChild

The maximum number of requests allowed for servo per child process during its lifetime, default is 10000. When 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 benefits:

1. It can prevent (accidental) memory leaks from going on indefinitely, thus running out of memory.

two。 Give processes a limited lifespan, which helps to reduce the number of active processes when the server load is reduced.

Detailed explanation of worker mode configuration

StartServers 2

MaxClients 150

MinSpareThreads 25

MaxSpareThreads 75

ThreadsPerChild 25

MaxRequestsPerChild 0

StartServers

The number of child processes established when the server starts. The default value is "3".

MaxClients

The maximum number of access requests allowed for simultaneous servo (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). So to increase MaxClients, you have to 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 generate new idle threads.

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 there are too many total free 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: 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 value is 25. Child processes do not create new threads after they are established at startup.

MaxRequestsPerChild

Sets the maximum number of requests that each child process is allowed to serve during its lifetime. When 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 benefits:

1. It can prevent (accidental) memory leaks from going on indefinitely, thus running out of memory.

two。 Give processes a limited lifespan, which helps to reduce the number of active processes when the server load is reduced.

Note that for KeepAlive links, only the first request is counted. In fact, it changes the behavior of limiting the maximum number of links per child process.

At this point, I believe you have a deeper understanding of "the introduction of Apache's prefork mode and worker mode". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report