Apache在配置編譯時(shí)可以自主的選擇想要使用的MPM模塊,使用./configure --with-mpm=MPM命令。我們主要了解prefork和worker這兩種MPM模塊。
Prefork如果不用“--with-mpm”顯式指定某種MPM,prefork就是Unix平臺上缺省的MPM。它所采用的預(yù)派生子進(jìn)程方式,用單獨(dú)的子進(jìn)程來處理不同的請求,進(jìn)程之間彼此獨(dú)立。在make編譯和make install安裝后,使用httpd -l來確定當(dāng)前使用的MPM是prefork.c。查看httpd-mpm.conf配置文件,里面包含如下默認(rèn)的配置段:
代碼如下<IfModule prefork.c>StartServers 5MinSpareServers 5MaxSpareServers 10MaxClients 150MaxRequestsPerChild 0</IfModule>
prefork控制進(jìn)程在最初建立“StartServers”個(gè)子進(jìn)程后,為了滿足MinSpareServers設(shè)置的需要?jiǎng)?chuàng)建一個(gè)進(jìn)程,等待一秒鐘,繼續(xù)創(chuàng)建兩個(gè),再等待一秒鐘,繼續(xù)創(chuàng)建四個(gè)……如此按指數(shù)級增加創(chuàng)建的進(jìn)程數(shù),最多達(dá)到每秒32個(gè),直到滿足MinSpareServers設(shè)置的值為止。這種模式可以不必在請求到來時(shí)再產(chǎn)生新的進(jìn)程,從而減小了系統(tǒng)開銷以增加性能。MaxSpareServers設(shè)置了最大的空閑進(jìn)程數(shù),如果空閑進(jìn)程數(shù)大于這個(gè)值,Apache會(huì)自動(dòng)kill掉一些多余進(jìn)程。這個(gè)值不要設(shè)得過大,但如果設(shè)的值比MinSpareServers小,Apache會(huì)自動(dòng)把其調(diào)整為MinSpareServers+1。如果站點(diǎn)負(fù)載較大,可考慮同時(shí)加大MinSpareServers和MaxSpareServers。MaxRequestsPerChild設(shè)置的是每個(gè)子進(jìn)程可處理的請求數(shù)。每個(gè)子進(jìn)程在處理了“MaxRequestsPerChild”個(gè)請求后將自動(dòng)銷毀。0意味著無限,即子進(jìn)程永不銷毀。雖然缺省設(shè)為0可以使每個(gè)子進(jìn)程處理更多的請求,但如果設(shè)成非零值也有兩點(diǎn)重要的好處:1、可防止意外的內(nèi)存泄漏。2、在服務(wù)器負(fù)載下降的時(shí)侯會(huì)自動(dòng)減少子進(jìn)程數(shù)。因此,可根據(jù)服務(wù)器的負(fù)載來調(diào)整這個(gè)值。MaxClients是這些指令中最為重要的一個(gè),設(shè)定的是Apache可以同時(shí)處理的請求,是對Apache性能影響最大的參數(shù)。其缺省值150是遠(yuǎn)遠(yuǎn)不夠的,如果請求總數(shù)已達(dá)到這個(gè)值(可通過ps -ef|grep http|wc -l來確認(rèn)),那么后面的請求就要排隊(duì),直到某個(gè)已處理請求完畢。這就是系統(tǒng)資源還剩下很多而HTTP訪問卻很慢的主要原因。雖然理論上這個(gè)值越大,可以處理的請求就越多,但Apache默認(rèn)的限制不能大于256。ServerLimit指令無須重編譯Apache就可以加大MaxClients。ServerLimt應(yīng)該放在第一個(gè)位置,放在其他指令之間不起作用(不明白原因)。
代碼如下<IfModule prefork.c>ServerLimit 10000StartServers 5MinSpareServers 5MaxSpareServers 10MaxClients 10000MaxRequestsPerChild 0</IfModule>
Worker相對于prefork,worker全新的支持多線程和多進(jìn)程混合模型的MPM。由于使用線程來處理,所以可以處理相對海量的請求,而系統(tǒng)資源的開銷要小于基于進(jìn)程的服務(wù)器。但是,worker也使用了多進(jìn)程,每個(gè)進(jìn)程又生成多個(gè)線程,以獲得基于進(jìn)程服務(wù)器的穩(wěn)定性。在configure --with-mpm=worker后,進(jìn)行make編譯、make install安裝。在缺省生成的httpd-mpm.conf中有以下默認(rèn)配置段:
代碼如下<IfModule worker.c>StartServers 2MaxClients 150MinSpareThreads 25MaxSpareThreads 75ThreadsPerChild 25MaxRequestsPerChild 0</IfModule>
Worker由主控制進(jìn)程生成“StartServers”個(gè)子進(jìn)程,每個(gè)子進(jìn)程中包含固定的ThreadsPerChild線程數(shù),各個(gè)線程獨(dú)立地處理請求。同樣,為了不在請求到來時(shí)再生成線程,MinSpareThreads和MaxSpareThreads設(shè)置了最少和最多的空閑線程數(shù);而MaxClients設(shè)置了同時(shí)連入的clients最大總數(shù)。如果現(xiàn)有子進(jìn)程中的線程總數(shù)不能滿足負(fù)載,控制進(jìn)程將派生新的子進(jìn)程。MinSpareThreads和MaxSpareThreads的最大缺省值分別是75和250。這兩個(gè)參數(shù)對Apache的性能影響并不大,可以按照實(shí)際情況相應(yīng)調(diào)節(jié)。ThreadsPerChild是worker MPM中與性能相關(guān)最密切的指令。ThreadsPerChild的最大缺省值是64,如果負(fù)載較大,64也是不夠的。這時(shí)要顯式使用ThreadLimit指令,它的最大缺省值是20000。Worker模式下所能同時(shí)處理的請求總數(shù)是由子進(jìn)程總數(shù)乘以ThreadsPerChild值決定的,應(yīng)該大于等于MaxClients。如果負(fù)載很大,現(xiàn)有的子進(jìn)程數(shù)不能滿足時(shí),控制進(jìn)程會(huì)派生新的子進(jìn)程。默認(rèn)最大的子進(jìn)程總數(shù)是16,加大時(shí)也需要顯式聲明ServerLimit(最大值是20000)。需要注意的是,如果顯式聲明了ServerLimit,那么它乘以ThreadsPerChild的值必須大于等于MaxClients,而且MaxClients必須是ThreadsPerChild的整數(shù)倍,否則Apache將會(huì)自動(dòng)調(diào)節(jié)到一個(gè)相應(yīng)值。
代碼如下<IfModule worker.c>ServerLimit 25ThreadLimit 200StartServers 3MaxClients 2000MinSpareThreads 50MaxSpareThreads 200ThreadsPerChild 100MaxRequestsPerChild 0</IfModule>
下面是利用Apache自帶的測試工具ab對Server進(jìn)行測試的情況(設(shè)定請求的index頁面為6bytes,Apache Server配置2cpu 2G memory),cpu%為cpu占用率,mem為內(nèi)存使用量(M為單位),RequestsPerSecond為每秒處理的請求數(shù)。1、Prefor方式 (ServerLimit,StartServer,MinSpareServers,MaxSpareServers,MaxClients,MaxRequestPerChild)
-n/-c(ab參數(shù)) Cpu% Mem Requestspersecond(-,5,5,10,150,0)100000/100 28.8 285 8434100000/200 29.2 304 8032100000/500 25.3 323 7348100000/1000 24.4 330 5886(10000,5,5,10,500,0)100000/100 28.7 371 8345100000/200 27.4 389 7929100000/500 24.9 417 7229100000/1000 23.4 437 6676(10000,5,5,10,1000,0)100000/100 28.8 408 8517100000/200 27.0 422 8045100000/500 24.2 455 7236100000/1000 22.5 470 6570(10000,5,5,10,1500,0)100000/100 29.6 330 8407100000/200 28.1 349 8014100000/500 26.4 380 7290100000/1000 24.0 400 6686
2、Worker方式(ServerLimt,Threadlimt,Startservers,MaxClients,MinspareThread,MaxspareThread,ThreadperChild,MaxRequestPerChild)
-n/-c(ab參數(shù)) cpu% mem RequestsperSecond(50,500,5,10000,50,200,200,0)100000/100 18.6 188 6020100000/200 20.1 195 5892100000/500 19.8 209 5708100000/1000 22.2 218 6081(100,500,5,10000,50,200,100,0)100000/100 24.5 240 6919100000/200 23.6 247 6798100000/500 24.6 254 6827100000/1000 22.3 271 6114(200,500,5,10000,50,200,50,0)100000/100 27.3 301 7781100000/200 27.4 307 7789100000/500 26.0 320 7141100000/1000 21.8 344 6110
相對來說,prefork方式速度要稍高于worker,然而它需要的cpu和memory資源也稍多于woker。
下面貼出我的系統(tǒng)mpm文件
代碼如下## Server-Pool Management (MPM specific)#
## PidFile: The file in which the server should record its process# identification number when it starts.## Note that this is the default PidFile for most MPMs.#<IfModule !mpm_netware_module> PidFile "logs/httpd.pid"</IfModule>
## The accept serialization lock file MUST BE STORED ON A LOCAL DISK.#<IfModule !mpm_winnt_module><IfModule !mpm_netware_module>LockFile "logs/accept.lock"</IfModule></IfModule>
## Only one of the below sections will be relevant on your# installed httpd. Use "apachectl -l" to find out the# active mpm.#
# 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 MaxClients 150 MaxRequestsPerChild 0</IfModule>
# worker MPM# StartServers: initial number of server processes to start# MaxClients: maximum number of simultaneous client connections# MinSpareThreads: minimum number of worker threads which are kept spare# MaxSpareThreads: maximum number of worker threads which are kept spare# ThreadsPerChild: constant number of worker threads in each server process# MaxRequestsPerChild: maximum number of requests a server process serves<IfModule mpm_worker_module> StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0</IfModule>
# BeOS MPM# StartThreads: how many threads do we initially spawn?# MaxClients: max number of threads we can have (1 thread == 1 client)# MaxRequestsPerThread: maximum number of requests each thread will process<IfModule mpm_beos_module> StartThreads 10 MaxClients 50 MaxRequestsPerThread 10000</IfModule>
# NetWare MPM# ThreadStackSize: Stack size allocated for each worker thread# StartThreads: Number of worker threads launched at server startup# MinSpareThreads: Minimum number of idle threads, to handle request spikes# MaxSpareThreads: Maximum number of idle threads# MaxThreads: Maximum number of worker threads alive at the same time# MaxRequestsPerChild: Maximum number of requests a thread serves. It is# recommended that the default value of 0 be set for this# directive on NetWare. This will allow the thread to# continue to service requests indefinitely. <IfModule mpm_netware_module> ThreadStackSize 65536 StartThreads 250 MinSpareThreads 25 MaxSpareThreads 250 MaxThreads 1000 MaxRequestsPerChild 0 MaxMemFree 100</IfModule>
# OS/2 MPM# StartServers: Number of server processes to maintain# MinSpareThreads: Minimum number of idle threads per process,# to handle request spikes# MaxSpareThreads: Maximum number of idle threads per process# MaxRequestsPerChild: Maximum number of connections per server process<IfModule mpm_mpmt_os2_module> StartServers 2 MinSpareThreads 5 MaxSpareThreads 10 MaxRequestsPerChild 0</IfModule>
# WinNT MPM# ThreadsPerChild: constant number of worker threads in the server process# MaxRequestsPerChild: maximum number of requests a server process serves<IfModule mpm_winnt_module> ThreadsPerChild 150 MaxRequestsPerChild 0</IfModule>
新聞熱點(diǎn)
疑難解答
圖片精選