Apache working mode: prefork and worker

Source: Internet
Author: User

One or two modes

Prefork mode:

Prefork is the default (default) MPM on UNIX platforms, using multiple child processes, with only one thread per child process. Each process can only maintain one connection at a certain time, and is efficient, but has a large memory footprint .
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.

Worker mode:

Workers use multiple child processes, each of which has multiple threads, and each thread can only maintain one connection at a certain time, with a smaller memory footprint and is suitable for high-traffic HTTP servers . The downside is that if a thread crashes, the entire process will "die" with any of its threads, so ensure that a program must be recognized by the system as "every thread is safe" at run time.
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. But it also uses multiple processes, each with multiple threads, to get the stability of the process-based MPM.


Second, the Apache mode of viewing and installation

1, often see the current mode

If Apache is already installed, we can use the "httpd-l" command to view the current mode. If PREFORK.C is found, it is currently working in Prefork mode, and WORKER.C is working in worker mode.

If Apache is not yet installed, we can join the--with-pem= (prefork|worker) option when compiling to determine what mode is enabled.

2. Switch mode

A. Renaming the current prefork mode startup file

MV HTTPd Httpd.prefork

B. Renaming the startup file for worker mode

MV Httpd.worker httpd

C. 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 2MaxClients 150MinSpareThreads 25MaxSpareThreads 75ThreadsPerChild 25MaxRequestsPerChild 0</ifmodule>

D. Restarting the service

/usr/local/apache2/bin/apachectl restart

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.

Iii. comparison of prefork and worker patterns

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 in the imperfect place, 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.

Four, Prefork mode configuration detailed

<ifmodule mpm_prefork_module>serverlimit 256StartServers 5MinSpareServers 5MaxSpareServers 10MaxClients 256MaxRequestsPerChild 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.

Five, the worker mode configuration detailed

<ifmodule mpm_worker_module>startservers 2MaxClients 150MinSpareThreads 25MaxSpareThreads 75ThreadsPerChild 25MaxRequestsPerChild 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.

Vi. Summary

The previous Apache mainstream model for Prefork, now the worker mode has begun to rise, the difference is that the worker mode can handle high traffic, but the security is not very good, prefork mode security is better, but performance will be very close, You can choose different mode according to your own server's category, better use Apache.


Apache working mode: prefork and worker

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.