版權聲明:可以在網上任意轉載,轉載時請務必以超連結形式标明文章原始出處、作者資訊及本聲明文字。
最近,幫一個朋友優化一個擁有20萬主題,100萬文章,3萬多會員,平均線上人數2000人的Discuz!論壇,采用Linux2.6+Apache2+mod_php5+MySQL5,伺服器配置為雙至強+4G記憶體,優化前,系統平均負載(load average)基本維持在10以上,MySQL的CPU占用率基本在90%以上,優化後,系統平均負載降到0.5以下,MySQL的CPU占用率很少有超過10%的時候。優化前YSlow得分隻有35分,優化後YSlow得分86分。現将優化的過程和經驗做一個記錄:
首先,對Apache進行優化,編輯httpd.conf,設定HostnameLookups、KeepAlive、MaxKeepAliveRequests以及KeepAliveTimeout四個參數,調整MaxSpareServers、ServerLimit、MaxClients以及MaxRequestsPerChild參數,還可以考慮棄用prefork而采用worker MPM。設定mod_deflate及mod_expires子產品,不過注意Discuz!不能對PHP檔案開啟Expires,否則會出現問題。另外還可以考慮開啟mod_cache和mod_mem_cache子產品。另外利用cronolog按天對日志進行輪循截斷,如果日志特别大,也可以按小時截斷。另外再加上Awstats對日志進行分析,并用gzip對日志進行壓縮,自動删除1個月前的日志。
其次,對PHP進行優化,編輯php.ini,調整output_buffering、zlib.output_compression及max_execution_time、max_input_time、memory_limit等參數,并安裝Xcache和Zend Optimizer。
然後對MySQL進行優化。首先重新靜态編譯MySQL,使其隻支援MyISAM和Memory兩種引擎,并按Discuz!編碼選擇隻支援UTF-8或者GBK字元集。編輯my.cnf,設定skip-locking、skip-external-locking、skip-networking和skip-name-resolve,根據記憶體和資料庫狀态具體調整key_buffer_size、query_cache_size、query_cache_limit、max_allowed_packet、table_cache、thread_cache_size、sort_buffer_size、read_buffer_size、read_rnd_buffer_size、join_buffer_size、tmp_table_size、max_tmp_tables、back_log、max_connections、wait_timeout的參數。
對資料庫進行優化,将threads和posts表中部分未索引的字段增加索引,并将supersite資料庫表從bbs資料庫獨立出去。修改discuz!配置檔案,設定開啟pconnect。
對Discuz!設定進行優化。進入Discuz!系統設定,修改頁面緩存設定中的緩存有效期和緩存系數,修改伺服器優化中的禁止浏覽器緩沖和頁面Gzip壓縮,修改防盜鍊設定中下載下傳附件來路檢查,用JSMin自動對js檔案進行縮減(Discuz! 6.1的common.js原檔案29.3k,經JSMin縮減後為24.1k,再經deflate後為7.3k),修改attachments.php檔案,将:
//dheader('Cache-control: max-age=31536000');//dheader('Expires: '.gmdate('D, d M Y H:i:s', $timestamp + 31536000).' GMT');前的注釋去掉。修改模闆目錄下adv.htm,去掉與Insenz有關的代碼。
通過檢視MySQL的status,可以看出優化後,長時間運作的Key_read_ratio基本保持在0.05%以下,Threads_cache_hitrate保持在99.9%以上。個人感覺,Discuz!将Session儲存在資料庫中,極大地降低了Query Cache的命中率,如果需要進一步優化,可以考慮修改Discuz!源碼,将Session儲存到Memcache中。
優化之後用Siege做并發壓力測試,在200并發下,基本沒有任何錯誤。如果将來人數更多,可以考慮将平台遷移到Ngix+PHP FastCGI上。
下面是用Siege在300并發下的測試結果:
#siege -c 300 -b -r 35 -f bbs.url
** SIEGE 2.67
** Preparing 300 concurrent users for battle.
The server is now under siege.. done.
Transactions: 10500 hits
Availability: 100.00 %
Elapsed time: 52.68 secs
Data transferred: 65.67 MB
Response time: 1.27 secs
Transaction rate: 199.32 trans/sec
Throughput: 1.25 MB/sec
Concurrency: 253.07
Successful transactions: 10500
Failed transactions: 0
Longest transaction: 24.88
Shortest transaction: 0.00
500并發下的測試結果:
#siege -c 500 -b -r 20 -f bbs.url
Transactions: 9979 hits
Availability: 99.79 %
Elapsed time: 86.52 secs
Data transferred: 82.66 MB
Response time: 3.30 secs
Transaction rate: 115.34 trans/sec
Throughput: 0.96 MB/sec
Concurrency: 381.07
Successful transactions: 9979
Failed transactions: 21
Longest transaction: 34.80
昨天,将伺服器遷移到了Nginx+php-fpm,以下是遷移後測試,相差真的很明顯,回頭再寫Nginx+php-fpm的經驗:
[root@bbs ~]# siege -c 300 -r 50 -f bbs.url
Transactions: 15000 hits
Elapsed time: 64.12 secs
Data transferred: 67.79 MB
Response time: 0.55 secs
Transaction rate: 233.94 trans/sec
Throughput: 1.06 MB/sec
Concurrency: 128.95
Successful transactions: 14976
Longest transaction: 3.96
[root@bbs ~]# siege -c 500 -r 50 -f bbs.url
** Preparing 500 concurrent users for battle.
Transactions: 25000 hits
Elapsed time: 65.52 secs
Data transferred: 83.65 MB
Response time: 0.57 secs
Transaction rate: 381.56 trans/sec
Throughput: 1.28 MB/sec
Concurrency: 216.02
Successful transactions: 21707
Longest transaction: 5.83
=======