Apache prefork mode and worker mode the user

Source: Internet
Author: User
Tags memory usage thread

  Prefork mode

This multi-channel module (MPM) implements a non-threading, pre-built 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 to require each request to be independent of each other, so that if a request has a problem it does not affect other requests.

This mpm has a strong self-regulation capability and requires minimal configuration instruction tuning. The most important thing is to set the maxclients to a value that is large enough to handle the potential request spikes, but not too large to use more memory than the physical memory.

  Worker mode

This multi-channel module (MPM) enables the network server to support mixed multithreaded multiple processes. Because you use threads to process requests, you can handle massive requests, and system resources are less expensive than MPM. However, it also uses multiple processes, each with multiple threads, to gain MPM stability based on process.

The most important instruction to control this MPM is to control the Threadsperchild instructions for the number of threads that each subprocess allows to establish, and the maxclients instructions that control the number of bus processes allowed.

  Prefork and worker mode switching

1. Rename the current prefork mode boot file

MV HTTPd Httpd.prefork

2. Rename the boot file for worker mode

MV Httpd.worker httpd

3. Modify Apache configuration file

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

Find the following paragraph inside, you can modify the load parameters such as:

Startservers 2

MaxClients 150

Minsparethreads 25

Maxsparethreads 75

Threadsperchild 25

Maxrequestsperchild 0

4. Restart Service

/usr/local/apache2/bin/apachectl restart

You can switch to worker mode to start apache2

In stability and security considerations, it is not recommended to replace the apache2 operation mode, using the system default Prefork can be. Many other PHP modules do not work in worker mode, such as Redhat Linux with PHP also does not support thread safety. So it's best not to switch working mode.

  Comparison of prefork and worker patterns

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 memory usage is much higher. Prefork's wireless path design will in some cases be more advantageous than the worker: it can use Third-party modules that do not handle thread safety well, and it is easier to debug for those platforms where thread debugging is difficult.

Worker mode uses multiple child processes, each of which has multiple threads. Each thread can maintain only one connection at a certain time. In general, the worker mpm is a good choice on a high-traffic HTTP server, because worker MPM memory usage is much lower than prefork MPM. But the worker mpm also by the imperfect place, if a thread crashes, the whole process will "die" along with all its threads. Because the thread shares the memory space, a program must be recognized by the system at run time as "every thread is safe."

Overall, the prefork mode is slightly faster than the worker, but it requires a little more CPU and memory resources than the Woker.

  Prefork Mode Configuration Detailed

Serverlimit 256

Startservers 5

Minspareservers 5

Maxspareservers 10

MaxClients 256

Maxrequestsperchild 0

Serverlimit

The default maxclient maximum is 256 threads, and if you want to set a larger value, add serverlimit this parameter. 20000 is the maximum value for serverlimit this parameter. If you need to be larger, you must compile Apache before you need to recompile Apache.

Prerequisite: Must be placed in front of other directives

Startservers

Specifies the number of child processes established when the server is started, prefork defaults to 5.

Minspareservers

Specifies the minimum number of idle child processes, which defaults to 5. If the current number of idle child processes is less than minspareservers, Apache will produce a new subprocess at a maximum rate of one per second. This parameter should not be set too large.

Maxspareservers

Sets the maximum number of idle child processes, which defaults to 10. If there is currently an idle subprocess exceeding the maxspareservers number, the parent process kills the extra child processes. This parameter should not be set too large. If you set the value of the directive to be smaller than Minspareservers, Apache will automatically modify it to "Minspareservers+1″."

MaxClients

Limit the number of client-side maximum access requests (the number of concurrent threads per process) for the same time, and defaults to 256. Any requests exceeding the maxclients limit will enter the waiting queue, and once a link is released, 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 subprocess allows for the servo during its lifetime, defaults to 10000. After the Maxrequestsperchild limit is reached, the subprocess will end. If Maxrequestsperchild is "0″", the child process will never end. There are two benefits to setting Maxrequestsperchild to a value other than 0:

1. Can prevent (accidental) memory leak indefinitely, thus running out of memory.

2. A finite lifespan is given to the process, thereby helping to reduce the number of active processes while the server load is reduced.

  Worker Mode configuration Detailed

Startservers 2

MaxClients 150

Minsparethreads 25

Maxsparethreads 75

Threadsperchild 25

Maxrequestsperchild 0

Startservers

The number of child processes that were established when the server was started, and the default value is "3″."

MaxClients

The 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 the result of 400″,16 (Serverlimit) multiplied by (Threadsperchild). So to increase the maxclients, you have to increase the value of the 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 numerous, the child process kills the extra idle threads. The range of maxsparethreads values is limited. Apache will automatically fix the value you set as follows: The worker requires it to be 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. The child processes create these threads at startup and no longer create new threads.

Maxrequestsperchild

Sets the maximum number of requests that each child process allows for the servo during its lifetime. Once the Maxrequestsperchild limit is reached, the child process will end. If Maxrequestsperchild is "0″", the child process will never end. There are two benefits to setting Maxrequestsperchild to a value other than 0:

1. Can prevent (accidental) memory leak indefinitely, thus running out of memory.

2. A finite lifespan is given to the process, thereby helping to reduce the number of active processes while 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 limiting 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.