Apache three modes of operation and related configurations

Source: Internet
Author: User
Tags epoll

One, prefork MPMKeywords: multi-process

Prefork mode can be considered an old but very stable pattern. Apache starts by pre-deriving some child processes from the fork, then waits for the request to come in, and always the view keeps some spare child processes. This is done to reduce the overhead of frequently creating and destroying processes. There is only one thread in each child process, and only one request can be processed within a single point in time.

In Unix systems, the parent process typically runs as root for 80 ports, and Apache-generated child processes are typically run with a low-privileged user. User and group directives are used to configure low-privileged users of child processes. The user who runs the child process 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.

Advantages:

Mature, compatible with all new and old modules. The process is completely independent, making it very stable. At the same time, there is no need to worry about thread safety. (The expansion of our common mod_php,php does not need to support thread safety)

Disadvantages:

A process consumes more of the system resources, consuming more memory. Moreover, it is not good at handling high concurrent requests, in which case it will put the request into the queue and wait until there are available processes before the request is processed.

related configurations in httpd-mpm.conf:
<ifmodule mpm_prefork_module>#服务器启动时建立的子进程数量 startservers5#空闲子进程的最小数量, default 5, and if the number of currently idle child processes is less than minspareservers, then Apache will produce new child processes.    Do not set this parameter too large. Minspareservers5#空闲子进程的最大数量, default 10; If there are currently more than maxspareservers of idle child processes, the parent process kills the extra child processes. This parameter also does not need to be set too large, if you set it smaller than Minspareservers, Apache will automatically modify it to Minspareservers+1. MaxspareserversTen#限定服务器同一时间内客户端最大接入的请求数量, the default is 150; Any requests that exceed this limit are queued, but a connection is freed and the request in the queue will be serviced. MaxClients Max#每个子进程在其生命周期内允许最大的请求数量, if the total number of requests has reached this value, the child process will end, and if set to 0, the child process will never end.    If this value is set to a value other than 0, you can prevent memory leaks caused by running PHP. Maxrequestsperchild0</IfModule>

The number of processes created, up to 32 per second, until the value of the Minspareservers setting is met. This is the origin of pre-derivation (prefork). This pattern eliminates the need to generate new processes when the request arrives, reducing overhead to increase performance.

When the number of concurrent requests reaches maxclients (such as 256), only 10 of the idle processes are available. Apache continues to increase the creation process. Until the number of processes reaches 256.

Apache gradually removes the process until the number of processes reaches maxspareservers when the concurrency spikes are over and the number of concurrent requests may be only one.

Second, the Worker MPMKeywords: multi-process + multithreading

Worker mode is a multi-process + multithreaded mode, compared to the previous one. It also pre-fork several sub-processes (a smaller number), each of which can generate some 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 free service thread pool. This way, the client does not have to wait for new threads or new processes to be established to be processed. In Unix, in order to be able to bind port 80, the parent process is typically started as root, and then Apache creates child processes and threads with lower-privileged users. The user and group directives are used to configure permissions for the Apache child process. Although the child process must have read access to the content it provides, it should give him less privileges as much as possible. Also, unless suEXEC is used, the permissions configured by these directives will be inherited by the CGI script.

Threads are lighter than processes because threads typically share the memory space of the parent process, so memory consumption decreases, and in high concurrency scenarios, it behaves better than the prefork pattern.

Some people may find it strange, so why not use multithreading here directly (i.e. to implement multiple processes within a process) and to introduce multiple processes?

The main reason is that stability needs to be considered, and if one thread hangs abnormally, it causes the parent process to hang with the other normal child threads (they are all under the same process). In multi-process + multithreaded mode, each process is independent, and if a thread has an exception, the only part of the service that is affected is Apache, not the entire service. Other processes can still work.

Advantages:

Occupy less memory and perform better in high concurrency.

Disadvantages:

You must consider the issue of thread safety, because multiple child threads are the memory addresses of the shared parent process. If you use keep-alive long connection, perhaps there is almost no request in the middle, then there will be blocking, the thread is suspended, you need to wait until the timeout is released. If too many threads are occupied, this can also lead to the availability of service-free threads in high concurrency scenarios. (This problem will also occur in prefork mode.)

The long connection of the ps:http1.1 keep-alive is to allow the next socket communication to be reused before the connection is created, thereby reducing the overhead of connection creation and destruction. Keeping a connection keeps a process or thread waiting, even if no data comes in.

Related Configuration
 <ifmodule mpm_worker_module>  #服务器启动时建立的子进程数量 startservers  2      #限定服务器同一时间内客户端最大接入的请求数量, the default is 150; Any requests that exceed this limit will go to the waiting queue, but one connection is freed and the request in the queue is serviced. MaxClients  150   #空闲子进程的最小数量 Minspa Rethreads  25   #空闲子进程的最大数量 Maxsparethre       Ads  75   #每个子进程产生的线程数量 Threadsperchild  25   #每个子进程在其生命周期内允许最大的请求数量, If the total number of requests has reached this value, the child process will end, and if set to 0, the child process will never end.    Setting this value to a value other than 0 prevents the memory leaks that are caused by running PHP. Maxrequestsperchild  0  </ifmodule>  
Understanding Configuration:

The master control process generates "Startservers" sub-processes, each of which contains a fixed number of threadsperchild threads, each of which processes requests independently. Similarly, in order to avoid generating threads when requests arrive, Minsparethreads and maxsparethreads set the minimum and maximum number of idle threads, while maxclients sets the total number of threads in all child processes. If the total number of threads in an existing child process does not meet the load, the control process will derive the new child process.

Third, the Event MPMKeywords: multi-process + multi-threaded +epoll

This is the newest pattern in Apache, which is already stable and usable in the current version. It is much like the worker pattern, and the biggest difference is that it solves the problem of wasting the resources of a long-occupied thread in the keep-alive scenario (some threads are being keep-alive, hanging there waiting, there is almost no request coming in, wait until the timeout).

In the event mpm, there will be a dedicated thread to manage these keep-alive types of threads, and when there is a real request coming in, pass the request to the service thread, and then allow it to be released when the execution is complete. In this way, one thread can handle several requests and implement asynchronous non-blocking.

Event MPM fails when it encounters some incompatible modules, it will fall back into worker mode, and a worker thread can process a request. The official comes with the module, all support event MPM.

Related configuration:

Note that the event MPM requires the support of the Linux system (Linux 2.6+) for Epoll to be enabled.

Also, the need to add is the HTTPS connection (SSL), its operating mode is still similar to the worker way, the thread will be occupied, know the connection is closed. Part of the older information, said that the event MPM does not support SSL, the statement is a few years ago, it is now supported.

Related configuration:
 <ifmodule mpm_worker_module>  #服务器启动时建立的子进程数量 startservers  3   #空闲子进程的最小数量 minsparethreads  75   #空闲子进程的最小数量 maxsparethreads  250   #每个子进程产生的线程数量 Threadsperchild  25      #限定服务器同一时间内客户端最大接入的请求数量, the default is 150; Any requests that exceed this limit will go to the waiting queue, but one connection is freed and the request in the queue is serviced. Maxrequestworkers  400   # Each child process allows the maximum number of requests in its lifetime, and if the total number of requests has reached this value, the child process will end, and if set to 0, the child process will never end.    Setting this value to a value other than 0 prevents the memory leaks that are caused by running PHP. Maxrequestsperchild  0  </ifmodule>  

Apache three modes of operation and related configurations

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.