Apache的prefork模式和worker模式

发表时间:2010-01-16 文章分类:apache 阅读:107920

文章链接:http://www.webzone8.com/article/227.html
  prefork模式
这个多路处理模块(MPM)实现了一个非线程型的、预派生的web服务器,它的工作方式类似于Apache 1.3。它适合于没有线程安全库,需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM,这样若一个请求出现问题就不会影响到其他请求。

[separator]

这个MPM具有很强的自我调节能力,只需要很少的配置指令调整。最重要的是将MaxClients设置为一个足够大的数值以处理潜在的请求高峰,同时又不能太大,以致需要使用的内存超出物理内存的大小。

worker模式
此多路处理模块(MPM)使网络服务器支持混合的多线程多进程。由于使用线程来处理请求,所以可以处理海量请求,而系统资源的开销小于基于进程的MPM。但是,它也使用了多进程,每个进程又有多个线程,以获得基于进程的MPM的稳定性。

控制这个MPM的最重要的指令是,控制每个子进程允许建立的线程数的ThreadsPerChild指令,和控制允许建立的总线程数的MaxClients指令。

prefork和worker模式的切换
1.将当前的prefork模式启动文件改名
mv httpd httpd.prefork
2.将worker模式的启动文件改名
mv httpd.worker httpd
3.修改Apache配置文件
vi /usr/local/apache2/conf/extra/httpd-mpm.conf
找到里边的如下一段,可适当修改负载等参数:

StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0

4.重新启动服务
/usr/local/apache2/bin/apachectl restart
即可换成worker方式启动apache2

处于稳定性和安全性考虑,不建议更换apache2的运行方式,使用系统默认prefork即可。另外很多php模块不能工作在worker模式下,例如redhat linux自带的php也不能支持线程安全。所以最好不要切换工作模式。

prefork和worker模式的比较
prefork模式使用多个子进程,每个子进程只有一个线程。每个进程在某个确定的时间只能维持一个连接。在大多数平台上,Prefork MPM在效率上要比Worker MPM要高,但是内存使用大得多。prefork的无线程设计在某些情况下将比worker更有优势:它可以使用那些没有处理好线程安全的第三方模块,并且对于那些线程调试困难的平台而言,它也更容易调试一些。

worker模式使用多个子进程,每个子进程有多个线程。每个线程在某个确定的时间只能维持一个连接。通常来说,在一个高流量的HTTP服务器上,Worker MPM是个比较好的选择,因为Worker MPM的内存使用比Prefork MPM要低得多。但worker MPM也由不完善的地方,如果一个线程崩溃,整个进程就会连同其所有线程一起”死掉”.由于线程共享内存空间,所以一个程序在运行时必须被系统识别为”每个线程都是安全的”。

总的来说,prefork方式速度要稍高于worker,然而它需要的cpu和memory资源也稍多于woker。

prefork模式配置详解

ServerLimit 256
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 256
MaxRequestsPerChild 0

ServerLimit
默认的MaxClient最大是256个线程,如果想设置更大的值,就的加上ServerLimit这个参数。20000是ServerLimit这个参数的最大值。如果需要更大,则必须编译apache,此前都是不需要重新编译Apache。
生效前提:必须放在其他指令的前面

StartServers
指定服务器启动时建立的子进程数量,prefork默认为5。

MinSpareServers
指定空闲子进程的最小数量,默认为5。如果当前空闲子进程数少于MinSpareServers ,那么Apache将以最大每秒一个的速度产生新的子进程。此参数不要设的太大。

MaxSpareServers
设置空闲子进程的最大数量,默认为10。如果当前有超过MaxSpareServers数量的空闲子进程,那么父进程将杀死多余的子进程。此参数不要设的太大。如果你将该指令的值设置为比MinSpareServers小,Apache将会自动将其修改成”MinSpareServers+1″。

MaxClients
限定同一时间客户端最大接入请求的数量(单个进程并发线程数),默认为256。任何超过MaxClients限制的请求都将进入等候队列,一旦一个链接被释放,队列中的请求将得到服务。要增大这个值,你必须同时增大ServerLimit。

MaxRequestsPerChild
每个子进程在其生存期内允许伺服的最大请求数量,默认为10000.到达MaxRequestsPerChild的限制后,子进程将会结束。如果MaxRequestsPerChild为”0″,子进程将永远不会结束。将MaxRequestsPerChild设置成非零值有两个好处:
1.可以防止(偶然的)内存泄漏无限进行,从而耗尽内存。
2.给进程一个有限寿命,从而有助于当服务器负载减轻的时候减少活动进程的数量。

worker模式配置详解

StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0


StartServers
服务器启动时建立的子进程数,默认值是”3″。

MaxClients
允许同时伺服的最大接入请求数量(最大线程数量)。任何超过MaxClients限制的请求都将进入等候队列。默认值是”400″,16(ServerLimit)乘以25(ThreadsPerChild)的结果。因此要增加MaxClients的时候,你必须同时增加ServerLimit的值。

MinSpareThreads
最小空闲线程数,默认值是”75″。这个MPM将基于整个服务器监视空闲线程数。如果服务器中总的空闲线程数太少,子进程将产生新的空闲线程。

MaxSpareThreads
设置最大空闲线程数。默认值是”250″。这个MPM将基于整个服务器监视空闲线程数。如果服务器中总的空闲线程数太多,子进程将杀死多余的空闲线程。MaxSpareThreads的取值范围是有限制的。Apache将按照如下限制自动修正你设置的值:worker要求其大于等于MinSpareThreads加上ThreadsPerChild的和。

ThreadsPerChild
每个子进程建立的常驻的执行线程数。默认值是25。子进程在启动时建立这些线程后就不再建立新的线程了。

MaxRequestsPerChild
设置每个子进程在其生存期内允许伺服的最大请求数量。到达MaxRequestsPerChild的限制后,子进程将会结束。如果MaxRequestsPerChild为”0″,子进程将永远不会结束。将MaxRequestsPerChild设置成非零值有两个好处:
1.可以防止(偶然的)内存泄漏无限进行,从而耗尽内存。
2.给进程一个有限寿命,从而有助于当服务器负载减轻的时候减少活动进程的数量。
注意对于KeepAlive链接,只有第一个请求会被计数。事实上,它改变了每个子进程限制最大链接数量的行为。
有趣的视频娱乐一下

原文链接:http://www.webzone8.com/article/227.html

用户评论

用户【27.150.243*****】说:It抯 arduous to search out knowledgeable folks on this topic, but you sound like you already know what you抮e speaking about! Thanks

用户【27.150.243*****】说:I and my pals came checking out the good recommendations found on your web page and then unexpectedly came up with a terrible suspicion I had not expressed respect to the blog owner for those techniques. All of the people are already for this reason joyful to read through them and have in effect honestly been making the most of those things. Appreciation for being considerably kind and for choosing this kind of perfect subject areas most people are really desperate to be aware of. Our honest regret for not expressing appreciation to you sooner.

用户【27.150.243*****】说:You made some respectable points there. I looked on the web for the problem and located most people will associate with together with your website.

用户【27.150.243*****】说:Thanks so much for giving everyone an exceptionally superb possiblity to read from this web site. It is often very pleasant and jam-packed with fun for me personally and my office friends to visit your website nearly 3 times in a week to read through the latest guidance you will have. And lastly, I am just certainly happy with all the breathtaking knowledge you give. Certain 1 facts in this article are in reality the most impressive we have all had.

用户【27.150.243*****】说:I simply wanted to compose a small word to be able to thank you for these fantastic tips and tricks you are giving out here. My rather long internet search has now been rewarded with reliable suggestions to write about with my relatives. I would point out that we website visitors actually are extremely fortunate to dwell in a notable site with very many wonderful people with very beneficial tricks. I feel very much happy to have come across your weblog and look forward to really more fun minutes reading here. Thanks a lot again for everything.

用户【27.150.243*****】说:After I initially commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get 4 emails with the identical comment. Is there any means you can take away me from that service? Thanks!

用户【27.150.243*****】说:After I initially commented I clicked the -Notify me when new comments are added- checkbox and now each time a remark is added I get 4 emails with the same comment. Is there any approach you possibly can take away me from that service? Thanks!

用户【27.150.243*****】说:I want to convey my affection for your generosity supporting persons who really need help on this one concern. Your very own dedication to getting the message all through had become exceptionally functional and has made people much like me to attain their objectives. Your own important useful information entails so much to me and far more to my peers. Many thanks; from all of us.

用户【27.150.243*****】说:Spot on with this write-up, I truly suppose this website needs rather more consideration. I抣l probably be once more to read far more, thanks for that info.

用户【27.150.243*****】说:This website is mostly a stroll-through for the entire info you needed about this and didn抰 know who to ask. Glimpse right here, and also you抣l positively discover it.

用户【27.150.245*****】说:very good publish, i definitely love this website, carry on it

用户【27.150.245*****】说:I would like to show my admiration for your kind-heartedness for those individuals that really want assistance with in this situation. Your real dedication to getting the message all through ended up being quite productive and has specifically made most people just like me to arrive at their ambitions. Your informative information entails this much a person like me and far more to my fellow workers. Warm regards; from each one of us.

用户【27.150.245*****】说:An impressive share, I just given this onto a colleague who was doing a little bit analysis on this. And he in truth bought me breakfast because I discovered it for him.. smile. So let me reword that: Thnx for the treat! However yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading extra on this topic. If attainable, as you turn into experience, would you thoughts updating your blog with more details? It is extremely useful for me. Massive thumb up for this weblog post!

用户【27.150.245*****】说:I wish to voice my respect for your kind-heartedness in support of those people who have the need for help on this area of interest. Your real commitment to getting the solution around had become extraordinarily powerful and have always empowered employees much like me to reach their desired goals. Your own insightful tips and hints entails a whole lot a person like me and even further to my mates. Best wishes; from all of us.

用户【27.150.245*****】说:Can I just say what a reduction to search out someone who actually knows what theyre talking about on the internet. You undoubtedly know how one can bring a problem to light and make it important. More people must learn this and understand this aspect of the story. I cant believe youre no more popular because you positively have the gift.

12下一页

发表评论

姓名:*
邮箱:*
网站:*