WordPress 很強大,但是強大之餘也有一堆爛攤子要使用者收拾,那就是較高的伺服器佔用,和速度實在不咋滴。如果你用 WordPress 來做 CMS 站點那麼你如果用主機託管那麼一定杯具了,用 VPS 記憶體小了寬頻小了也麻煩。其他人都是教了一招,這次我彙集百家所長,十招齊發,十管齊下,勇者無敵!
大家多說時間就是生命,可能幾秒鐘的等待就會讓網站失去一個訪客,對於網站的速度你不能等!
1.Cookie 的靜態化製作
約有 80%至 90%的時間,訪客要花費大量的時間等你的 WordPress 載入靜態內容。這意味著,有大部分的時間,使用者瀏覽您的網站,他們正在等待載入,如:影像,CSS,JS 指令碼,flash 等等。所以你可以最佳化內容,更快捷地讓 WordPress 載入一個 cookie 域。消除了負載時的一些珍貴的時間,當然了這可能看起來並不多,但新增其他拖延問題時,才能真正減少時間損失。
我不知道其他原始碼的網站應該怎麼做,但是 WordPress 很簡單隻要修改一下 wp-config.php 就好了!
在合適的位置加入:
define("WP_CONTENT_URL", "http://static.***.com");
define("COOKIE_DOMAIN", "www.***.com");
然後用 「bloginfo(template_directory')」 這段程式碼加入你要實現的靜態內容中即可!
2. 設定靜態內容的快取
靜態的內容快取,我就不多介紹了,你用相關的快取外掛也需要這個步驟,那就是修改.htaccess 檔案
程式碼如下:
<IfModule mod_expires.c>
ExpiresActive on
# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault "access plus 1 month"
# cache.appcache needs re-requests
# in FF 3.6 (thx Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Your document html
ExpiresByType text/html "access plus 0 seconds"
# Data
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
# RSS feed
ExpiresByType application/rss+xml "access plus 1 hour"
# Favicon (cannot be renamed)
ExpiresByType image/x-icon "access plus 1 week"
# Media: images, video, audio
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# HTC files (css3pie)
ExpiresByType text/x-component "access plus 1 month"
# Webfonts
ExpiresByType font/truetype "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
# CSS and JavaScript
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
3. 最佳化資料庫
有一個方法來為的伺服器減壓,怎樣才能最佳化 WordPress 的資料庫呢?自然是用程式碼,用程式碼,嘿嘿,你可能編輯了幾個小時都沒有效果!WordPress 的免費外掛使得這個問題變得簡單,個人推薦 Yoast Optimize DB 和 WP DB Manager 。牢記大多數快取外掛還幫助了很多本,所以你會發現大多數正在採取照顧您已經現存的快取軟體的工作量。
4. 儘量快取媒體
建立快取可以很簡單地透過外掛完成。從本質上講,這些新增轉成靜態頁面,所有這些方式,既方便載入,又節省您的瀏覽器前端的滯後。那麼,什麼外掛,聽我的建議吧。
我最喜歡的快取工具:WP Super Cache, Hyper Cache,和 W3 Total Cache 。用這些生成 HTML 檔案,可以節省更多的時間。
5. 使用 CDN
CDN 加速目前很火,那麼自然火是有一定的道理的,因為 CDN 可以加速呀。呵呵,cdn 一般是收費,不過免費的 CDN 也有。
下面為大家推薦一些實用的免費 CDN:
免費 CDN:Webluker,cloudflare
6. 壓縮 JS 和 CSS 檔案
你可能只聽過說 JS 和 CSS 檔案的庫轉移,總沒有聽說過壓縮吧。呵呵,這個方法是很異想天開,但是就是有人做到了,並且開發了壓縮的實用工具,當然機器的壓縮並不盡人意,你也可以手工壓縮。
JS 壓縮工具推薦:Closure Compiler ,Minify JavaScript. CSS 壓縮工具推薦:Minify CSS ,CSS Compressor
7. 壓縮影像
影像的載入也是比較耗時的,想你如果試用 BMP 或者 PSD 作為影像那麼速度一定和蝸牛爬一樣,比較高質量儲存的 PNG 檔案我也不怎麼推薦。建議試用 JPG 壓縮,但是也不要過度!不然圖片會失真。
透過 WP Smush.it 外掛你可以比較簡單的來壓縮圖片!
8.GZIP 壓縮
GZIP 的壓縮在一定限度內是很不錯的!而且透過 gzip 壓縮不僅能夠加速而且還可以遮蔽廣告 (免費空間的福音啊)GZIP 的壓縮程式碼交給你!修改.htaccess 檔案
<IfModule mod_deflate.c>
# force deflate for mangled headers
# developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)s*,?s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
<IfModule filter_module>
FilterDeclare COMPRESS
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/html
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/css
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/plain
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $text/x-component
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/javascript
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/json
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/xhtml+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/rss+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/atom+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/vnd.ms-fontobject
FilterProvider COMPRESS DEFLATE resp=Content-Type $image/svg+xml
FilterProvider COMPRESS DEFLATE resp=Content-Type $application/x-font-ttf
FilterProvider COMPRESS DEFLATE resp=Content-Type $font/opentype
FilterChain COMPRESS
FilterProtocol COMPRESS DEFLATE change=yes;byteranges=no
</IfModule>
<IfModule !mod_filter.c>
# Legacy versions of Apache
AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE image/svg+xml
application/vnd.ms-fontobject application/x-font-ttf font/opentype
</IfModule>
</IfModule>
9. 停用 ETAGS
ETAGS 可能我也搞不懂,但是你僅用了它就可以加速了,所以你管他神馬 ETAGS 你說是不?
在.htaccess 檔案中+這一行!
File ETag none
10. 伺服器環境
使用 LAMP 環境或者 LNMP 環境,可以更好的支援 WordPress 並且風險係數低,也很安全,估計你的伺服器或者 VPS 幾年都不會重啟一次,所以網上總是我自己的 linux 密碼忘記了重啟怎麼辦! 這就是免費惹的禍,真是躺著也中槍啊!
總結:
這些方法中大多數只適合在 LAMP 或者 LNMP 的環境下,所以使用 WordPress 就必須要在 linux 環境下,win 環境下的 WordPress 你會遇到一些列的問題!