A detailed explanation of Apache working mode

Source: Internet
Author: User
Tags memory usage
Apache as the most widely used Web server and the most stable open source server software, its working mode there are many, there are two main modes: Prefork mode and worker mode Prefork mode: This multi-channel module (MPM) implements a non-threading, pre-derived Web server, which is a working style similar to Apache 1.3. It is suitable for systems that do not have thread-safe libraries and need to avoid thread-compatibility issues. It requires the best mpm for each request to be independent of each other, so that if a request becomes problematic 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. But 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 number of threads that each subprocess allows to establish threadsperchild instructions, and the maxclients instructions that control the number of bus processes that are allowed to be established. View and install Apache mode: If Apache is already installed, we can use the Httpd-l command to view the current mode (there may only be one)
If PREFORK.C is found, it means that the current work is in prefork mode, and WORKER.C is working in worker mode.      If Apache is not installed, we can add the--with-pem= (prefork|worker) option at compile time to decide what mode to enable. Of course, if you have installed (the author mode is prefork) can also use the following command to switch MV httpd httpd.prefork
The mode changes after MV Httpd.worker httpd reboot. Different mode configurations: they are not the same for the httpd configuration files, they are all in the default profile httpd.conf
<ifmodule prefork.c>
Startservers 5
Minspareservers 5
Maxspareservers 20
Serverlimit 256
MaxClients 256
Maxrequestsperchild 4000
</IfModule>
#这是prefork的配置文件
<ifmodule worker.c>
Startservers 2
MaxClients 150
Minsparethreads 25
Maxsparethreads 75
Threadsperchild 25
Maxrequestsperchild 0
</IfModule>
#这是worker的配置文件
PREFORK.C Module
Prefork MPM uses multiple child processes, with only one thread per child process. 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: he is able to use Third-party modules that do not handle thread safety, and is easier to debug for those platforms where thread debugging is difficult.
Serverlimit 20000
Startservers 5
Minspareservers 5
Maxspareservers 10
MaxClients 1000
Maxrequestsperchild 0
The default maxclient maximum is 256 threads, and if you want to configure 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 5
Specifies the number of child processes established when the server is started, prefork defaults to 5.
Minspareservers 5
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 the rate of one per second. This parameter should not be set too large.
Maxspareservers 10
Configures the maximum number of idle child processes, which defaults to 10. If there is currently an idle subprocess exceeding the maxspareservers number, then the parent process will kill the extra child processes. This parameter should not be set too large. If you configure the value of the directive to be smaller than Minspareservers, Apache will automatically modify it to "minspareservers+1".
MaxClients 256
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 10000
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 configuring Maxrequestsperchild to a value other than 0:
1. Can prevent (accidental) memory leaks indefinitely, thereby depleting 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.
Working mode:
A separate control process (the parent process) is responsible for generating the child processes that are used to listen for requests and respond. Apache always tries to keep some spare (spare) or idle subprocess to meet the incoming request. This eliminates the need for the client to wait for the child process to occur before the service is received. In Unix systems, the parent process normally runs as root for state 80 ports, while Apache-generated subprocess typically runs with a low privileged user. User and group directives are used to configure low privileged users of child processes. A user running a subprocess must have read access to the content he serves, but must have as few permissions as possible for resources other than the content of the service.
WORKER.C Module
Worker MPM 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 any of its threads. Because the thread shares the memory space, a program must be recognized by the system as "every thread is safe" when it is running.
Serverlimit 50
Threadlimit 200
Startservers 5
MaxClients 5000
Minsparethreads 25
Maxsparethreads 500
Threadsperchild 100
Maxrequestsperchild 0
Serverlimit 16
The maximum number of processes that the server allows to configure. This directive is used in conjunction with Threadlimit to configure the MaxClients maximum allowable configuration value. Any changes to this instruction during the reboot will be ignored, but the changes to the maxclients will take effect.
Threadlimit 64
The maximum number of threads can be configured per child process. This directive configures the Threadsperchild limit for the number of threads that can be configured per child process. Any changes to this instruction during the reboot will be ignored, but the changes to the Threadsperchild will take effect. The default value is "64".
Startservers 3
The number of child processes that were established when the server was started, and the default value is "3".
Minsparethreads 75
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 250
Configure 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 your configured values as follows: The worker needs to be greater than or equal to Minsparethreads plus threadsperchild and
MaxClients 400
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 "serverlimit" multiplied by the (threadsperchild). Therefore, to increase the maxclients, you must increase the value of the serverlimit at the same time.
Threadsperchild 25
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 0
Configure the maximum number of requests that each subprocess 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 configuring Maxrequestsperchild to a value other than 0:
1. Can prevent (accidental) memory leaks indefinitely, thereby depleting 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.
Attention
For keepalive links, only the first request is counted. In fact, he changed the behavior of limiting the maximum number of links per child process.
Working mode:
The number of threads that each process can hold is fixed. The server will increase or decrease the number of processes based on the load. A separate control process (the parent process) is responsible for the establishment of the child process. Each child process can establish a threadsperchild number of service threads and a listener thread that listens to the access request and passes it to the service thread for processing and answering. Apache always tries to maintain a standby (spare) or idle service thread pool. In this way, the client does not have to wait for a new thread or a new process to be established to be processed. In Unix, in order to be able to bind 80 ports, the parent process is typically started as root, and then Apache establishes the subprocess and thread with the less privileged user. The user and group directives are used to configure permissions for the Apache subprocess. Although the child process must have read access to the content it provides, it should give him less privileges as much as possible.   In addition, the permissions configured by these directives will be inherited by the CGI script unless suexec is used. View the Apache process: Because of the Apache working mode relationship, the httpd process view is also different.
For Prefork, because a process corresponds to only one thread, we can use the # PS aux when viewing it | grep httpd |grep-v grep
Root 3704 0.0 0.9 10548 3452? Ss Sep08 0:00/usr/sbin/httpd
Apache 3778 0.0 0.5 10548 2112? S Sep08 0:00/usr/sbin/httpd
Apache 3779 0.0 0.5 10548 2112? S Sep08 0:00/usr/sbin/httpd
Apache 3780 0.0 0.5 10548 2112? S Sep08 0:00/usr/sbin/httpd
Apache 3781 0.0 0.5 10548 2112? S Sep08 0:00/usr/sbin/httpd
Apache 3782 0.0 0.5 10548 2112? S Sep08 0:00/usr/sbin/httpd and the worker mode requires # Pstree |grep httpd
|-HTTPD---2*[httpd---26*[{httpd}]]

Before the Apache mainstream model for Prefork, now the worker model is also beginning to rise, the difference, the worker model can deal with high traffic, but not very good security; Prefork mode security is better, but performance will be almost, you can according to your own server categories Choose a different pattern and use Apache better.

Original source: http://lxycneo.blog.51cto.com/2534579/671527

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.