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中添加 通過以上配置后.本人用ab測試 性能提高了...百倍... <ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/x-httpd-php text/javascript application/x-javascript text/css
AddOutputFilter DEFLATE js css #壓縮js,css文件
# Don't compress images #對照片文件不進行壓縮處理
SetEnvIfNoCase Request_URI (?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI (?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary
#SetEnvIfNoCase Request_URI .(css|js)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
DeflateCompressionLevel 6
SetOutputFilter DEFLATE
</ifmodule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType 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后傳輸內容都被壓縮了..所以速度變快了..節省了帶寬
來自:http://blog.csdn.net/atvance016/article/details/43116753