apache工作模式prefork或worker,与please see the ServerLimit报错
http://hi.baidu.com/clock/item/dafb32d14c26b7d6251f4052
服务器不稳定,是worker模式。使用的是默认配置,没有进行优化。
修改http.conf文件
增加
<IfModule mpm_worker_module>
StartServers 2
MaxClients 700
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
ServerLimit 30
MaxRequestsPerChild 10000
</IfModule>
and would exceed the ServerLimit value of 16.
Automatically lowering MaxClients to 400. To increase,
please see the ServerLimit directive.
Syntax OK
StartServers 2
MaxClients 700
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 10000
</IfModule>
就不会报错了
另外:
因此要使 ServerLimit 生效,必须先停止 Apache: apachectl stop,再启动Apache: apachectl start
http://bbs.chinaunix.net/thread-1536505-1-1.html
配置有问题,3个警告分别告诉你: 1.ThreadsPerChild 100 超过了默认值64 2.2500不是64的整数倍 3.39个进程超出了默认进程最大数16 所以你的配置最后被降到了ThreadsPerChild 64,ServerLimit 16,MaxClients 1024 要增加这几个默认值要改代码 |
http://awenhaowenchao.iteye.com/blog/1735968
一般apache采用prefork和worker机制,通过apachectl -l命令查看默认使用的prefork机制。需要修改prefork策略
那么需要做如下修改:
1,/usr/local/apache2/conf/http.conf 引入(include)prefork配置文件/usr/local/apache2/conf/extra/httpd-mpm.conf,
2,修改httpd-mpm.conf配置文件的prefork配置部分
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 1000 ##必须放在MaxClients上面 且值>=MaxCliens
MaxClients 1000
MaxRequestsPerChild 10000
</IfModule>
注意:
1,默认情况下prefork配置部分没有ServerLimit配置,其在/usr/loca/apache2/bin/httpd(16进制)中编译,一般会报错如下
MaxClients of 1000 exceeds ServerLimit value of 256 servers,
lowering MaxClients to 256. To increase, please see the ServerLimit directive.
2,修改conf文件后,需要重新启动
3,
prefork采用预派生子进程方式,用单独的子进程来处理 不同的请求,进程之间彼此独立。
相对于prefork,worker全新的支持多线程和多进程混合模型的MPM。由于 使用线程来处理,所以可以处理相对海量的请求,而系统资源的开销要小于基于进程的服务器。但是,worker也使用了多进程,每个进程又生成多个线程,以 获得基于进程服务器的稳定性。(第三点摘自:http://www.itlearner.com/article/4443)
http://www.num123.com/post/113
[root@redhat5 conf]# ../bin/apachectl -t
WARNING: MaxClients of 1000 exceeds ServerLimit value of 256 servers,
lowering MaxClients to 256. To increase, please see the ServerLimit
directive.
Syntax OK
编辑httpd-mpm.conf 文件,加入ServerLimit指令即可。
<IfModule mpm_prefork_module>
StartServers 10
MinSpareServers 5
MaxSpareServers 25
ServerLimit 1500
MaxClients 1000
MaxRequestsPerChild 128
</IfModule>
关于apache使用prefork还是worker工作模式可以在编译apache时指定,默认是prefork工作模式,可以使用/home/apache/bin/apachectl -l 命令查看,要想配置生效必须./apachectl stop 然后./apachectl start ,不能使用./apachectl restart 这样是不生效的。
http://blog.cnr.cn/18/viewspace-11871.html
疑问:
ps aux|grep http|grep -v grep|wc -l 的值不会高于260
http://bbs.linuxpk.com/thread-19431-1-1.html
你应该好好理解一下ServerLimit就像一个木桶,用他来装MaxClients这个水,所以ServerLimit一定要大于MaxClients
而且ServerLimit需要放在MaxClients上面,更确切的说ServerLimit允许的最大总进程数阀值
http://clip.artchiu.org/2009/11/09/apache-%E4%B9%8B%E8%AC%8E%E6%A8%A3-serverlimit-%E6%8C%87%E4%BB%A4-directive/
而後總算在網友的文章Apache 2.0 中 prefork.c 模組與 worker.c 模組的比較中找到端倪:
ServerLimit 要放在 MaxClients 前面!!!
<IfModule mpm_prefork_module> ServerLimit 500 # ServerLimit 放在 MaxClients 前面!!! StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 300 MaxRequestsPerChild 0 </IfModule>
http://blog.csdn.net/aovenus/article/details/6541941
1、Redhat Linux下查看apache版本号
在Apache安装目录bin下,使用以下命令查看即可。
使用命令:./httpd -v
示例:
2、查看Apache当前工作模式
Apache有prefork和worker工作模式
使用命令:./apachectl –l
示例:
从以上结果可知,当时httpd工作在prefork模式下。
在configure时,可以通过指定参数,将工作模式设置为worker模式或prefork模式。
使用命令:./configure –with-mpm=worker
示例:设置为worker模式
Apache服务的两种工作模式详解:
http://www.cnblogs.com/ghj1976/archive/2011/01/11/1932764.html
prefork的工作原理及配置
如果不用“--with-mpm”显式指定某种MPM,prefork就是Unix平台上缺省的MPM。它所采用的预派生子进程方式也是Apache 1.3中采用的模式。prefork本身并没有使用到线程,2.0版使用它是为了与1.3版保持兼容性;另一方面,prefork用单独的子进程来处理不同的请求,进程之间是彼此独立的,这也使其成为最稳定的MPM之一。
若使用prefork,在make编译和make install安装后,使用“httpd -l”来确定当前使用的MPM,应该会看到prefork.c(如果看到worker.c说明使用的是worker MPM,依此类推)。再查看缺省生成的httpd.conf配置文件,里面包含如下配置段:
<IfModule prefork.c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
prefork的工作原理是,控制进程在最初建立“StartServers”个子进程后,为了满足MinSpareServers设置的需要创建一个进程,等待一秒钟,继续创建两个,再等待一秒钟,继续创建四个……如此按指数级增加创建的进程数,最多达到每秒32个,直到满足MinSpareServers设置的值为止。这就是预派生(prefork)的由来。这种模式可以不必在请求到来时再产生新的进程,从而减小了系统开销以增加性能。
MaxSpareServers设置了最大的空闲进程数,如果空闲进程数大于这个值,Apache会自动kill掉一些多余进程。这个值不要设得过大,但如果设的值比MinSpareServers小,Apache会自动把其调整为MinSpareServers+1。如果站点负载较大,可考虑同时加大MinSpareServers和MaxSpareServers。
MaxRequestsPerChild设置的是每个子进程可处理的请求数。每个子进程在处理了“MaxRequestsPerChild”个请求后将自动销毁。0意味着无限,即子进程永不销毁。虽然缺省设为0可以使每个子进程处理更多的请求,但如果设成非零值也有两点重要的好处:
◆ 可防止意外的内存泄漏;
◆ 在服务器负载下降的时侯会自动减少子进程数。
因此,可根据服务器的负载来调整这个值。个人认为10000左右比较合适。
MaxClients是这些指令中最为重要的一个,设定的是Apache可以同时处理的请求,是对Apache性能影响最大的参数。其缺省值150是远远不够的,如果请求总数已达到这个值(可通过ps -ef|grep http|wc -l来确认),那么后面的请求就要排队,直到某个已处理请求完毕。这就是系统资源还剩下很多而HTTP访问却很慢的主要原因。系统管理员可以根据硬件配置和负载情况来动态调整这个值。虽然理论上这个值越大,可以处理的请求就越多,但Apache默认的限制不能大于256。如果把这个值设为大于256,那么Apache将无法起动。事实上,256对于负载稍重的站点也是不够的。在Apache 1.3中,这是个硬限制。如果要加大这个值,必须在“configure”前手工修改的源代码树下的src/include/httpd.h中查找256,就会发现“#define HARD_SERVER_LIMIT 256”这行。把256改为要增大的值(如4000),然后重新编译Apache即可。在Apache 2.0中新加入了ServerLimit指令,使得无须重编译Apache就可以加大MaxClients。下面是prefork配置段:
<IfModule prefork.c>
StartServers 10
MinSpareServers 10
MaxSpareServers 15
ServerLimit 2000
MaxClients 1000
MaxRequestsPerChild 10000
</IfModule>
上述配置中,ServerLimit的最大值是2000,对于大多数站点已经足够。如果一定要再加大这个数值,对位于源代码树下server/mpm/prefork/prefork.c中以下两行做相应修改即可:
#define DEFAULT_SERVER_LIMIT 256
#define MAX_SERVER_LIMIT 2000
worker的工作原理及配置
相对于prefork,worker是2.0 版中全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理,所以可以处理相对海量的请求,而系统资源的开销要小于基于进程的服务器。但是,worker也使用了多进程,每个进程又生成多个线程,以获得基于进程服务器的稳定性。这种MPM的工作方式将是Apache 2.0的发展趋势。
在configure -with-mpm=worker后,进行make编译、make install安装。在缺省生成的httpd.conf中有以下配置段:
<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
worker的工作原理是,由主控制进程生成“StartServers”个子进程,每个子进程中包含固定的ThreadsPerChild线程数,各个线程独立地处理请求。同样,为了不在请求到来时再生成线程,MinSpareThreads和MaxSpareThreads设置了最少和最多的空闲线程数;而MaxClients设置了所有子进程中的线程总数。如果现有子进程中的线程总数不能满足负载,控制进程将派生新的子进程。
MinSpareThreads和MaxSpareThreads的最大缺省值分别是75和250。这两个参数对Apache的性能影响并不大,可以按照实际情况相应调节。
ThreadsPerChild是worker MPM中与性能相关最密切的指令。ThreadsPerChild的最大缺省值是64,如果负载较大,64也是不够的。这时要显式使用ThreadLimit指令,它的最大缺省值是20000。上述两个值位于源码树server/mpm/worker/worker.c中的以下两行:
#define DEFAULT_THREAD_LIMIT 64
#define MAX_THREAD_LIMIT 20000
这两行对应着ThreadsPerChild和ThreadLimit的限制数。最好在configure之前就把64改成所希望的值。注意,不要把这两个值设得太高,超过系统的处理能力,从而因Apache不起动使系统很不稳定。
Worker模式下所能同时处理的请求总数是由子进程总数乘以ThreadsPerChild值决定的,应该大于等于MaxClients。如果负载很大,现有的子进程数不能满足时,控制进程会派生新的子进程。默认最大的子进程总数是16,加大时也需要显式声明ServerLimit(最大值是20000)。这两个值位于源码树server/mpm/worker/worker.c中的以下两行:
#define DEFAULT_SERVER_LIMIT 16
#define MAX_SERVER_LIMIT 20000
需要注意的是,如果显式声明了ServerLimit,那么它乘以ThreadsPerChild的值必须大于等于MaxClients,而且MaxClients必须是ThreadsPerChild的整数倍,否则Apache将会自动调节到一个相应值(可能是个非期望值)。下面是worker配置段:
<IfModule worker.c>
StartServers 3
MaxClients 2000
ServerLimit 25
MinSpareThreads 50
MaxSpareThreads 200
ThreadLimit 200
ThreadsPerChild 100
MaxRequestsPerChild 0
</IfModule>
通过上面的叙述,可以了解到Apache 2.0中prefork和worker这两个重要MPM的工作原理,并可根据实际情况来配置Apache相关的核心参数,以获得最大的性能和稳定性。