Apache prefork mode and worker Mode

Source: Internet
Author: User

Prefork Mode
This multi-path processing module (MPM) implements a non-threaded, pre-derived Web server, which works in a way similar to Apache 1.3. It is suitable for systems that do not have a thread security library and need to avoid thread compatibility issues. It is the best MPM for each request to be independent from each other, so that if a request fails, it will not affect other requests.

This MPM has a strong self-regulation capability and requires only a small amount of configuration instructions. The most important thing is to set maxclients to a value that is large enough to handle potential request peaks. At the same time, it cannot be too large, so that the memory used exceeds the physical memory size.

Worker Mode
This multi-path processing module (MPM) enables network servers to support mixed multi-threaded Multi-process. Because a thread is used to process requests, a large number of requests can be processed, and the overhead of system resources is less than the process-based MPM. However, it also uses multiple processes, and each process has multiple threads to obtain the stability of the process-based MPM.

The most important command to control this MPM is the threadsperchild command that controls the number of threads allowed for each sub-process, and the maxclients command that controls the total number of threads allowed to be established.

 

Switching between prefork and worker Modes
1. Rename the current prefork Startup File
MV httpd. prefork
2. Rename the Startup File in worker Mode
MV httpd. Worker httpd
3. Modify the Apache configuration file
VI/usr/local/apache2/CONF/extra/httpd-mpm.conf
Find the following section, and modify the parameters such as load:
<Ifmodule mpm_worker_module>
Startservers 2
Maxclients 150
Minsparethreads 25
Maxsparethreads 75
Threadsperchild 25
Maxrequestsperchild 0
</Ifmodule>
4. Restart the service
/Usr/local/apache2/bin/apachectl restart
Start apache2 in worker mode.

For stability and security considerations, we do not recommend you change the apache2 running mode. Use the system default prefork. In addition, many PHP modules cannot work in worker mode. For example, PHP in RedHat Linux does not support thread security. So it is best not to switch the working mode.

Comparison of prefork and worker Modes
In prefork mode, multiple sub-processes are used. Each sub-process has only one thread. Each process can maintain only one connection at a specified time. On most platforms, the prefork MPM is more efficient than the worker mpm, but the memory usage is much larger. Prefork's wireless program design will be more advantageous in some cases than worker: it can use third-party modules that do not handle thread security well, and for platforms that have difficulty in thread debugging, it is easier to debug.

In worker mode, multiple sub-processes are used. Each sub-process has multiple threads. Each thread can maintain only one connection at a specified time. Generally, 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. However, the worker MPM is also imperfect. If a thread crashes, the whole process will "die" together with all its threads ". because threads share memory space, a program must be recognized by the system as "Every thread is safe" during runtime ".

In general, the speed of the prefork method is slightly higher than that of the worker method. However, it requires more CPU and memory resources than woker.

Prefork mode configuration
<Ifmodule mpm_prefork_module>
Serverlimit 256
Startservers 5
Minspareservers 5
Maxspareservers 10
Maxclients 256
Maxrequestsperchild 0
</Ifmodule>
Serverlimit
The default maxclient has a maximum of 256 threads. To set a larger value, add the serverlimit parameter. 20000 is the maximum value of serverlimit. If you need more, you must compile Apache without re-compiling Apache.
Prerequisite: It must be placed before other instructions.

Startservers
Specifies the number of sub-processes created when the server starts. The default value of prefork is 5.

Minspareservers
Minimum number of idle sub-processes. The default value is 5. If the number of idle sub-processes is less than minspareservers, Apache will generate a new sub-process at a maximum speed per second. Do not set this parameter too large.

Maxspareservers
Set the maximum number of idle sub-processes. The default value is 10. If there are idle sub-processes that exceed the number of maxspareservers, the parent process will kill the redundant sub-processes. Do not set this parameter too large. If you set the value of this command to a smaller value than minspareservers, Apache will automatically change it to "minspareservers + 1 ″.

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

Maxrequestsperchild
Each sub-process allows the maximum number of requests of the servo within its lifetime. The default value is 10000. When the limit of maxrequestsperchild is reached, the sub-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 can prevent (accidental) infinite Memory leakage and thus exhaust the memory.
2. A limited life cycle is provided for processes, which helps reduce the number of active processes when server load is reduced.

Worker mode configuration
<Ifmodule mpm_worker_module>
Startservers 2
Maxclients 150
Minsparethreads 25
Maxsparethreads 75
Threadsperchild 25
Maxrequestsperchild 0
</Ifmodule>

Startservers
Number of sub-processes created when the server starts. The default value is "3 ″.

Maxclients
Maximum number of access requests (maximum number of threads) that can be simultaneously servo ). 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 add maxclients, you must add the serverlimit value at the same time.

Minsparethreads
The minimum number of Idle threads. The default value is "75 ″. This MPM monitors the number of Idle threads based on the entire server. If the total number of Idle threads on the server is too small, the child process will generate a new idle thread.

Maxsparethreads
Set the maximum number of Idle threads. The default value is 250 ″. This MPM monitors the number of Idle threads based on the entire server. If the total number of Idle threads on the server is too large, the child process will kill the redundant Idle threads. The value range of maxsparethreads is limited. Apache will automatically correct the value you set according to the following restrictions: worker requires that the value is greater than or equal to minsparethreads and the sum of threadsperchild.

Threadsperchild
The number of resident execution threads created by each sub-process. The default value is 25. After these threads are created at startup, the child process will no longer create new threads.

Maxrequestsperchild
Sets the maximum number of requests that each sub-process allows for the servo during its lifetime. When the limit of maxrequestsperchild is reached, the sub-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 can prevent (accidental) infinite Memory leakage and thus exhaust the memory.
2. A limited life cycle is provided for processes, which helps reduce the number of active processes when server load is reduced.
Note that for keepalive connections, only the first request is counted. In fact, it changes the behavior of each sub-process to limit the maximum number of links.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.