標籤:apache 伺服器 ubuntu 最佳化 效能
apache2最佳化配置總結
運行和測試環境
Ubuntu server 12.04 LTS
配置參數調整
設定檔 一般在/etc/apache2/下 apache2.conf conf.d/secrity default.conf 下面我寫到的配置參數都存在在這幾個檔案中
1.hostnamelookups off
網域名稱尋找 開啟這個會增加AP的負擔, 減慢訪問速度 建議關閉
2. sethandler server-status
關閉這個否則暴露資訊.
3.allowoverride none
如果你不需要.htaccess 不需要重寫 什麼偽靜態 請設定none 否則 All
4.option -indexes
這個是禁止暴露目錄結構
5.timeout 5
推薦5 這個是 AP接受請求或者發出相應的時間超過這個時間 斷開
6. keepalive on
這個一定要開 意思是保持串連 因為HTTP 1.1後支援長串連 開啟後減少AP 每次請求資源檔啥的..再次新開進程 增加了效率
7.maxkeepaliverequests 50
保持長串連的最大個數
8.keepalivetimeout 5
長串連超過5秒後沒反應的 斷掉 這個數值不能太大 因為你一直保持的浪費系統資源
9.servertokens
回應標頭包含的資訊, 這個設定 servertokens Prod
10.serversignature off
這個是產生404頁面的時候 伺服器的資訊..如果設定off那麼只會顯示Apache 不會顯示版本資訊
11.MPM
這個比較關鍵是影響並發效率的主要因素
一般預設Ubuntu 安裝後 是 prefork模式 如果想使用 work模式 請在編譯的時候 設定設定檔
下面就講解 prefork 這個模式是開始 進程 相對來說 比較安全 當你某個請求掛掉的時候不會影響到其他的 .
下面是我的配置
<IfModule mpm_prefork_module>
ServerLimit 1000
StartServers 10
MinSpareServers 30
MaxSpareServers 45
MaxClients 1000
MaxRequestsPerChild 3000
</IfModule>
第一個ServerLimit 這個是必須的....因為預設的MaxClients 256最大 你必須加上limit才會提高最大服務量 並且記住要放到第一個
StartServers 10 開啟的AP個數
MinSpareServers 30最小空閑進程
MaxSpareServers 45最大空閑進程 這2個要根據自己伺服器的承載和服務量 調節
MaxClients 1000最大的服務量同時 這個比較關鍵.....如果 網站並發比較大 請增加這個數 但是注意, 如果太大超過了..實體記憶體 會崩潰
MaxRequestsPerChild 3000最大子進程的數量
以上配置的 數字 是我自己的個人機器最優 這寫數字 請大家要根據自己實際情況 親自通過ab 或者jmeter 然後通過top 觀察後 調節到最適合自己的
開啟deflate
將下面3個模組ln -s 到 mods-enable
mod_deflate.so mod_expires.so mod_header.so
然後在http.conf中添加
<ifmodule mod_deflate.c>AddOutputFilterByType DEFLATE text/html text/plain text/css application/x-httpd-php text/javascript application/x-javascript text/cssAddOutputFilter DEFLATE js css #壓縮js,css檔案# Don't compress images #對照片檔案不進行壓縮處理SetEnvIfNoCase Request_URI (?:gif|jpe?g|png)$ no-gzip dont-varySetEnvIfNoCase Request_URI (?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-varySetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary#SetEnvIfNoCase Request_URI .(css|js)$ no-gzip dont-vary# Make sure proxies don't deliver the wrong contentHeader append Vary User-Agent env=!dont-varyDeflateCompressionLevel 6SetOutputFilter DEFLATE</ifmodule><IfModule mod_expires.c>ExpiresActive OnExpiresByType text/css "now plus 1 month"ExpiresByType application/x-javascript "now plus 5 day"ExpiresByType image/jpeg "access plus 1 month"ExpiresByType image/gif "access plus 1 month"ExpiresByType image/bmp "access plus 1 month"ExpiresByType image/x-icon "access plus 1 month"ExpiresByType image/png "access plus 1 month"ExpiresByType application/x-shockwave-flash "access plus 1 month"</IfModule>
開啟GZIP後傳輸內容都被壓縮了..所以速度變快了..節省了頻寬
通過以上配置後.本人用ab測試 效能提高了...百倍...
apache2最佳化配置總結