关于小悟志网站地图归档友情链接联系Feed

云上小悟 + 

首页 » 网站建设 »

配置Apache的MPM模块(Worker模式)

网站建设
2017年6月4日 / 28次阅读
标签:APACHE

拍拍贷

文章《配置Apache的MPM模块(Worker模式)》的特色图片

优化配置Apache的MPM模块,只针对Worker模式。下面的英文来自Apache官方文档,中文是麦新杰的翻译和个人理解说明。

 

A single control process (the parent) is responsible for launching child processes. Each child process creates a fixed number of server threads as specified in the ThreadsPerChild directive, as well as a listener thread which listens for connections and passes them to a server thread for processing when they arrive.

在Worker模式下,Apache启动之后,会先启动一个control process(父进程),再由这个父进程按照配置文件里面的参数启动子进程。

每个子进程创建一组固定数量的服务线程,这个固定的数量由ThreadsPerChild参数(默认25)确定;同时,还会创建一个监听线程(listener thread)。也就是说,每个子进程创建ThreadsPerChild+1个线程。

Apache HTTP Server always tries to maintain a pool of spare or idle server threads, which stand ready to serve incoming requests. In this way, clients do not need to wait for a new threads or processes to be created before their requests can be served. The number of processes that will initially launch is set by the StartServers directive. During operation, the server assesses the total number of idle threads in all processes, and forks or kills processes to keep this number within the boundaries specified by MinSpareThreads and MaxSpareThreads. Since this process is very self-regulating, it is rarely necessary to modify these directives from their default values. The maximum number of clients that may be served simultaneously (i.e., the maximum total number of threads in all processes) is determined by the MaxRequestWorkers directive. The maximum number of active child processes is determined by the MaxRequestWorkers directive divided by the ThreadsPerChild directive.

Apache HTTP Server总是试图保持一定数量的空闲服务线程,这样在有请求到来的时候,可以理解处理,而不是先创建新的子进程和服务线程。

在服务器运行过程中,Apache会自动的创建或删除子进程,将服务器线程的总数量控制在MinSpareThreads和MaxSpareThreads之间。这个过程是自动的,在服务器负载不高的时候进行,而且,几乎没有必要去修改这两项配置的默认值。MinSpareThreads的默认值也是25,这里有个细节,即要理解Apache创建的每个子进程都有固定数量的线程,默认是25个,那么MinSpareThreads和MaxSpareThreads这两个参数的值也应该是ThreadsPerChild的整数倍。一般不要修改ThreadsPerChild的值。

StartServers参数决定了在Apache HTTP Server启动的时候,创建的子进程数。如果StartServers设置为1,启动Apache后,你会看到两个httpd进程,其中一个是父进程。

MaxRequestWorkers参数用来控制最大可以并发处理的服务线程数,这个值同样也需要是ThreadsPerChild的值的整数倍。因此,Apache能够创建的最大子进程说可以用MaxRequestWorkers/ThreadsPerChild得到。

Two directives set hard limits on the number of active child processes and the number of server threads in a child process, and can only be changed by fully stopping the server and then starting it again. ServerLimit is a hard limit on the number of active child processes, and must be greater than or equal to the MaxRequestWorkers directive divided by the ThreadsPerChild directive. ThreadLimit is a hard limit of the number of server threads, and must be greater than or equal to the ThreadsPerChild directive.

ServerLimit参数,顾名思义,决定了最大子进程的数量,这个值必须大于等于MaxRequestWorkers/ThreadsPerChild。

ThreadLimit参数,顾名思义,服务线程的最大数,这个值必须大于等于ThreadsPerChild。

这两个参数在实际配置中,可能不会出现,仔细想想,这两个参数的确是有些多余的。在改变配置的时候,这些参数可以帮助我们减少配置量,并防止出错。

In addition to the set of active child processes, there may be additional child processes which are terminating, but where at least one server thread is still handling an existing client connection. Up to MaxRequestWorkers terminating processes may be present, though the actual number can be expected to be much smaller. This behavior can be avoided by disabling the termination of individual child processes, which is achieved using the following:

  1. set the value of MaxConnectionsPerChild to zero
  2. set the value of MaxSpareThreads to the same value as MaxRequestWorkers

子进程还有可能被终止,当此子进程中最后一个服务线程工作完成之后。

比如MaxRequestWorkers/ThreadsPerChild=8,表示系统最大可以启动8个子进程,在繁忙过去后,系统要终止一些子进程,让服务线程的数量维持在MinSpareThreads和MaxSpareThreads之间。

可以通过如下配置,使子进程不会被终止:

1, 将MaxConnectionsPerChild设置为0;

2, 让MaxSpareThreads=MaxRequestWorkers;

MaxConnectionsPerChild controls how frequently the server recycles processes by killing old ones and launching new ones.

MaxConnectionsPerChild参数表示每个子进程处理多少个请求之后,就会被终止。如果你的服务器有多余1个子进程,建议将MaxConnectionsPerChild设置为一个常数,比如100000,这表示在处理了100000个请求之后,这个子进程会被终止,这样可以有效防止系统的内存泄露。如果你的服务器资源只够开1个子进程,就可以考虑按上诉建议配置,子进程永不终止。

下面是官方教材给出的一个典型配置:

ServerLimit         16
StartServers         2
MaxRequestWorkers  150
MinSpareThreads     25
MaxSpareThreads     75
ThreadsPerChild     25

以上配置MaxRequestWorkers/ThreadsPerChild=6,而ServerLimit=16,16个子进程是达不到的;但是如果将MaxRequestWorkers配置成1000,这时MaxRequestWorkers/ThreadsPerChild=40,而ServerLimit=16,这时最大也就16个子进程,MaxRequestWorkers这个配置上限就达不到。

While the parent process is usually started as root under Unix in order to bind to port 80, the child processes and threads are launched by the server as a less-privileged user. The User and Group directives are used to set the privileges of the Apache HTTP Server child processes. The child processes must be able to read all the content that will be served, but should have as few privileges beyond that as possible.

父进程通常是root启动的,这样权限才足够去绑定80端口。而子进程是父进程启动的,权限较低。httpd.conf配置文件中,通过User和Group指令来制定子进程的用户和组信息。这就是为什么网站资源文件都要给这个User和Group授权。

本文链接:http://www.maixj.net/wz/apache-mpm-worker-15661
云上小悟 麦新杰(QQ:1093023102)

相关文章

评论是美德

无力满足评论实名制,评论对非实名注册用户关闭,有事QQ:1093023102.


前一篇:
后一篇:

栏目精选


©Copyright 麦新杰 Since 2014 云上小悟独立博客版权所有 备案号:苏ICP备14045477号-1

网站二维码
拍拍贷
go to top