WordPress Optimization 有很多方法,根據 Codex 裡面的 WordPress Optimization/Cheat Sheet 有幾個方向:
Server Optimization
- DNS onto a separate server
- Web Server optimization – !
- PHP acceleration / optimization – !
- MySQL tweaking (query cache, etc) – !
WordPress Performance
- Remove unused plugins – !
- Optimize plugins
- Optimize themes, hardcode static vars, etc – !
Offloading
- Offload static files to separate server
- Optimized web servers like publicfile, lighttpd, etc
- Offload feed traffic
Caching
- WP-Cache / Lightpress – !
- Browser caching
- Web server caching
Adding Database Servers
上面有 ! 的地方,是比較容易進行的。其他的選項都是執行難度比較高,或是效益不顯著。
首先,在 Server Optimization 的部份有提到 Web Server optimization ,我認為目前使用 Apache2 最佳的解決方案大概是用 mpm-worker + FastCGI 跑 PHP-CGI ,但是這個我弄不起來,因為在 FCGIWrapper 的設定過不去,所以我還是只好老實用 Prefork 。
接著是 Web server caching 和 Database Caching 這部份可以用 memcache 來做,之後專文介紹。接著下面說的就是比較容易做的作法了!
以下執行環境針對 Debian Linux r4.0
安裝 eAccelerator
首先安裝 php5-dev
apt-get install build-essential php5-dev
切換到暫存目錄,下載並且解壓縮
cd /tmp
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.zip
unzip eaccelerator-0.9.5.3.zip
cd eaccelerator-0.9.5.3
接著執行
phpize
會出現訊息
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
接下來
./configure
make
make install
make install 之後,會出現一個路徑像是: /usr/lib/php5/20060613+lfs/
在 /etc/php5/conf.d 裡面增加一個目錄,建立一個檔案叫做 eaccelerator.ini 檔案內寫設定:
extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/var/cache/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
接下來建立快取的目錄:
mkdir -p /var/cache/eaccelerator
chmod 0777 /var/cache/eaccelerator
重新啟動伺服器
/etc/init.d/apache2 restart
跑一下版本…
root@roga:/# /usr/bin/php5 -v
PHP 5.2.0-8+etch13 (cli) (built: Oct 2 2008 08:26:18)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend Technologies
with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
調整 MySQL 的 Query Cache
這個調整對WP的前台的幫助還好,但對後台幫助不少!找到 my.cnf,以下是我伺服器的設定,意思是當查詢超過 4M 的時候不快取,查詢快取總空間佔用 128M ,而且快取模式 = 啟動
query_cache_limit = 4M
query_cache_size = 128M
query_cache_type = 1
- 修改 /wp-config.php ,加入 define(‘WP_CACHE’, true); 表示啟動快取。
- 上傳 WP Super Cache 到 plugins 目錄
chown www-data.wwwdata wp-content
(將 wp-content 目錄的使用者暫時修改為 Apache2)chmod 777 .htaccess
將網站根目錄下的 .htaccess 暫時設定為可寫- 到後台啟動 WP Super Cache ,然後她會改寫 .htaccess 檔,並且新增 wp-content/plugins/wp-super-cache 目錄,以及新增 wp-content/cache/.htaccess 設定檔
附註:在 WebSiteOptimization.com 有提供 Web Page Analyzer 可以看如何最佳化。
例如:建議透過 HTTP compression 或是optimize JavaScript 和 CSS 來減少傳輸量。
很有深度的文章呀 学习了