Two common working modes of Apache: prefork and worker

Source: Internet
Author: User

Apache is the most widely used and stable open-source server software for today's Web servers. There are many working modes of Apache. Currently, there are two main modes: prefork mode and worker mode.

I. Two modes

Prefork mode:

Prefork is the default MPM on the UNIX platform. Multiple Sub-processes are used, and each sub-process has only one thread. Each process can maintain only one connection at a specified time, which is efficient, but the memory usage is large.
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.

Worker mode:

Worker uses multiple sub-processes. Each sub-process has multiple threads. Each thread can maintain only one connection at a specified time. The memory usage is small and suitable for HTTP servers with high traffic. The disadvantage is that if a thread crashes, the whole process will "die" together with any of its threads ", therefore, ensure that a program must be recognized by the system as "Every thread is safe" during running ".
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.


Ii. View and install Apache Mode

1. The current mode is often used.

If Apache has been installed, run the "httpd-L" command to view the current mode. If prefork. C is found, the current job is in the prefork mode, and worker. C works in the worker mode.

If Apache is not installed yet, we can add the -- with-PEM = (prefork | worker) option during compilation to determine the mode to enable.

2. Switch Mode

A. Rename the current prefork Startup File.

mv httpd httpd.prefork

B. Rename the Startup File in worker mode.

mv httpd.worker httpd

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

D. Restart the service.

/usr/local/apache2/bin/apachectl restart

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.

 

Iii. 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.

 

Iv. Explanation of prefork mode configuration

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

 

5. Worker mode configuration

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

Maxrequestworkers = maxclients maximum number of connections

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.

 

Vi. Summary

In the past, the mainstream Apache mode was prefork, and now the worker mode has started to increase. The difference is that the worker mode can cope with high traffic, but the security is not good. The prefork mode is more secure, however, the performance may be poor. You can select different modes based on the type of your server to better use Apache.


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.