web服務器架構與Apache
WEB應用系統通常由WEB服務器統一處理客戶端的HTTP請求,WEB服務器負責處理靜態頁面,動態頁面轉發給應用服務器,應用服務器再將其中的數據訪問請求給數據庫服務器處理,
客戶機-->WEB服務器 --> APP服務器 —->數據庫服務器
MPM對Apache性能的影響
worker MPM:使用多個子進程,每一個子進程中又有多個線程,每一個線程處理一個請求。該MPM一般對高流量的服務器是一個不錯選擇,由於它比prefork MPM須要更少的內存並且更具備伸縮性
prefork MPM:使用多個子進程,但每一個子進程並不包含多線程,每一個進程只處理一個鏈接,在許多系統上它的速度和worker MPM同樣快,可是,須要更多的內存。這種無線程的設計在某些狀況下優於worker MPM,由於它能夠應用於不具有線程安全的第三方模塊上,且在不支持線程調試的平臺上易於調試,另爲還具備比worker MPM更高的穩定性
調整 Max Clients
內存是影響web服務器的重要因素之一,並且採用prefork MPM的話,因爲每一個進程會佔據必定的內存,所以須要注意調整Max Clients參數,以便在提供足夠的鏈接給客戶端的同時,不至於耗盡服務器的內存,Max Client 是指Apache最多能啓用的服務器進程數。
若是出下一下狀況,則能夠推斷有可能須要增長服務器內存,加到Apache的MaxClients數量
網站在線人數增多,訪問時很慢,初步認爲是服務器資源不足,但反覆測試,一旦鏈接上,不斷點擊同一個頁面上的鏈接,都能迅速打開,這種現象說明Apache最大鏈接數已經滿了,新的訪客只能排隊等待空閒的鏈接,而若是一旦鏈接上,KeepAlive的存活時間內都不用從新打開鏈接,所以解決的方法就是加大Apache的最大鏈接數。
vi httpd-mpm.conf
# 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
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>
# 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
# MaxRequestWorkers: maximum number of server processes allowed to start
# MaxConnectionsPerChild: maximum number of connections a server process serves
# before terminating
<IfModule mpm_prefork_module>
StartServers 5 #啓動Apache時就啓動的服務器進程
MinSpareServers 5 #最少保留的備用服務器進程
MaxSpareServers 10 #最大保留的備用服務器進程
MaxRequestWorkers 250
MaxConnectionsPerChild 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 worker.c>
ServerLimit 32
ThreadLimit 64 #必須放到最前面
StartServers 4
MaxClients 300
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
MaxSpareServers :是指Apache最大保留的備用服務器進程,若是你的系統內存不是很充足,或者運行有其餘的服務,把MaxSpareServrs設置小一些能夠爲其餘服務空出一些內存
mod_cache
mod_cache:爲了可以支持cache,須要在編譯的時候啓用它,默認caching是禁用的。mod_cache一共有3中mod_mem_cache,mom_disk_cache,mod_file_cache,
爲了可以編譯cache,須要在編譯時設置
--enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache
安裝後編譯httpd.conf,添加
<IfModule mod_cache.c>
CacheDefaultExpire 3600
CacheMaxExpire 86400
CacheLastModfiedFactor 0.1
<IfModule mod_disk_cache.c>
CacheRoot /usr/local/apache2/cache #建立讀寫權限
CacheMaxFileSize 10000000
CacheMinFileSize 1
CacheEnable disk / #
CacheDirLevels 5
CacheDirLength 3
</IfModule>
</IfModule>
<IfModule mod_mem_cache.c>
CacheEnable mem /
MCacheSize 4096 #緩存數據最多可以使用的內存
MCaheMaxObjectCount 2000 #內存中最多可以緩存對象的個數
MCaheMinObjectSize 1 #單個緩存對象的最小值
MCaheMaxObjectSize 2048 #單個緩存對象的最大值
</IfModule>web