Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> 電腦軟件教程 >> 服務器技術 >> 關於服務器 >> Apache中配置最大並發用戶數 tcp連接設置方法

Apache中配置最大並發用戶數 tcp連接設置方法

日期:2017/2/8 10:13:20      編輯:關於服務器

Apache在配置編譯時可以自主的選擇想要使用的MPM模塊,使用./configure --with-mpm=MPM命令。我們主要了解prefork和worker這兩種MPM模塊。

Prefork
如果不用“--with-mpm”顯式指定某種MPM,prefork就是Unix平台上缺省的MPM。它所采用的預派生子進程方式,用單獨的子進程來處理不同的請求,進程之間彼此獨立。在make編譯和make install安裝後,使用httpd -l來確定當前使用的
MPM是prefork.c。查看httpd-mpm.conf配置文件,裡面包含如下默認的配置段:

 代碼如下
<IfModule prefork.c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
 

prefork控制進程在最初建立“StartServers”個子進程後,為了滿足MinSpareServers設置的需要創建一個進程,等待一秒鐘,繼續創建兩個,再等待一秒鐘,繼續創建四個……如此按指數級增加創建的進程數,最多達到每秒32個,直到滿足MinSpareServers設置的值為止。這種模式可以不必在請求到來時再產生新的進程,從而減小了系統開銷以增加性能。MaxSpareServers設置了最大的空閒進程數,如果空閒進程數大於這個值,Apache會自動kill掉一些多余進程。這個值不要設得過大,但如果設的值比MinSpareServers小,Apache會自動把其調整為MinSpareServers+1。如果站點負載較大,可考慮同時加大MinSpareServers和MaxSpareServers。MaxRequestsPerChild設置的是每個子進程可處理的請求數。每個子進程在處理了“MaxRequestsPerChild”個請求後將自動銷毀。0意味著無限,即子進程永不銷毀。雖然缺省設為0可以使每個子進程處理更多的請求,但如果設成非零值也有兩點重要的好處:1、可防止意外的內存洩漏。2、在服務器負載下降的時侯會自動減少子進程數。因此,可根據服務器的負載來調整這個值。MaxClients是這些指令中最為重要的一個,設定的是Apache可以同時處理的請求,是對Apache性能影響最大的參數。其缺省值150是遠遠不夠的,如果請求總數已達到這個值(可通過ps -ef|grep http|wc -l來確認),那麼後面的請求就要排隊,直到某個已處理請求完畢。這就是系統資源還剩下很多而HTTP訪問卻很慢的主要原因。雖然理論上這個值越大,可以處理的請求就越多,但Apache默認的限制不能大於256。ServerLimit指令無須重編譯Apache就可以加大MaxClients。ServerLimt應該放在第一個位置,放在其他指令之間不起作用(不明白原因)。

 代碼如下
<IfModule prefork.c>
ServerLimit  10000
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 10000
MaxRequestsPerChild 0
</IfModule>

Worker
相對於prefork,worker全新的支持多線程和多進程混合模型的MPM。由於使用線程來處理,所以可以處理相對海量的請求,而系統資源的開銷要小於基於進程的服務器。但是,worker也使用了多進程,每個進程又生成多個線程,以獲得基於進程服務器的穩定性。在configure --with-mpm=worker後,進行make編譯、make install安裝。在缺省生成的httpd-mpm.conf中有以下默認配置段:

 代碼如下
<IfModule worker.c>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
 

Worker由主控制進程生成“StartServers”個子進程,每個子進程中包含固定的ThreadsPerChild線程數,各個線程獨立地處理請求。同樣,為了不在請求到來時再生成線程,MinSpareThreads和MaxSpareThreads設置了最少和最多的空閒線程數;而MaxClients設置了同時連入的clients最大總數。如果現有子進程中的線程總數不能滿足負載,控制進程將派生新的子進程。MinSpareThreads和MaxSpareThreads的最大缺省值分別是75和250。這兩個參數對Apache的性能影響並不大,可以按照實際情況相應調節。ThreadsPerChild是worker MPM中與性能相關最密切的指令。ThreadsPerChild的最大缺省值是64,如果負載較大,64也是不夠的。這時要顯式使用ThreadLimit指令,它的最大缺省值是20000。Worker模式下所能同時處理的請求總數是由子進程總數乘以ThreadsPerChild值決定的,應該大於等於MaxClients。如果負載很大,現有的子進程數不能滿足時,控制進程會派生新的子進程。默認最大的子進程總數是16,加大時也需要顯式聲明ServerLimit(最大值是20000)。需要注意的是,如果顯式聲明了ServerLimit,那麼它乘以ThreadsPerChild的值必須大於等於MaxClients,而且MaxClients必須是ThreadsPerChild的整數倍,否則Apache將會自動調節到一個相應值。

 代碼如下
<IfModule worker.c>
ServerLimit 25
ThreadLimit 200
StartServers 3
MaxClients 2000
MinSpareThreads 50
MaxSpareThreads 200
ThreadsPerChild 100
MaxRequestsPerChild 0
</IfModule>

下面是利用Apache自帶的測試工具ab對Server進行測試的情況(設定請求的index頁面為6bytes,Apache Server配置2cpu 2G memory),cpu%為cpu占用率,mem為內存使用量(M為單位),RequestsPerSecond為每秒處理的請求數。
1、Prefor方式
  (ServerLimit,StartServer,MinSpareServers,MaxSpareServers,MaxClients,MaxRequestPerChild)          

-n/-c(ab參數) Cpu% Mem Requestspersecond
(-,5,5,10,150,0)
100000/100 28.8 285 8434
100000/200 29.2 304 8032
100000/500 25.3 323 7348
100000/1000 24.4 330 5886
(10000,5,5,10,500,0)
100000/100 28.7 371 8345
100000/200 27.4 389 7929
100000/500 24.9 417 7229
100000/1000 23.4 437 6676
(10000,5,5,10,1000,0)
100000/100 28.8 408 8517
100000/200 27.0 422 8045
100000/500 24.2 455 7236
100000/1000 22.5 470 6570
(10000,5,5,10,1500,0)
100000/100 29.6 330 8407
100000/200 28.1 349 8014
100000/500 26.4 380 7290
100000/1000 24.0 400 6686
 

2、Worker方式
(ServerLimt,Threadlimt,Startservers,MaxClients,MinspareThread,MaxspareThread,ThreadperChild,MaxRequestPerChild)
                 

-n/-c(ab參數) cpu% mem RequestsperSecond
(50,500,5,10000,50,200,200,0)
100000/100  18.6 188 6020
100000/200 20.1 195 5892
100000/500 19.8 209 5708
100000/1000 22.2 218 6081
(100,500,5,10000,50,200,100,0)
100000/100  24.5 240 6919
100000/200 23.6 247 6798
100000/500 24.6 254 6827
100000/1000 22.3 271 6114
(200,500,5,10000,50,200,50,0)
100000/100  27.3 301 7781
100000/200 27.4 307 7789
100000/500 26.0 320 7141
100000/1000 21.8 344 6110

相對來說,prefork方式速度要稍高於worker,然而它需要的cpu和memory資源也稍多於woker。

下面貼出我的系統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>

Copyright © Windows教程網 All Rights Reserved