前言
最近
FreeBSD 系統常常會當機,加上
Varnish cache +
APC 一直給我出包,所以這次就換了一套加速 PHP 執行的套件
XCache,這是一套由華人所開發的加速器,可以參考篇
台灣PHP聯盟論壇所發表
PHP 加速器 – xcache,裡面可以看到
這篇裡面就有中文的對話,非常好玩,這次也順便把 PHP 升級到 5.3.8 版本,還有 Varnish 一次升級到 3.0.1 版本。
各家 PHP 加速器
大家都知道目前網路上知名的 PHP 加速器
apc,
eaccelerator,
phpa,
truck-mmcache,這幾套網路上資料很多,大家都可以試著玩看看,尤其是前面兩套 APC 及 eaccelerator,phpa 目前已經不再維護了,truck-mmcache 版本好像也沒啥在更新,距離上次更新是 2009-07-17,Xcache 作者研究 truck-mmcache 跟 APC 已經很長的時間,他發現 APC 的程式碼比起 truck-mmcache 還要簡單更容易瞭解,所以大家也可以研究 APC 相關程式碼。
Xcache 的由來
大家一定很想知道作者為什麼想要開發一套新的 PHP 加速器 Xcache 呢?早期作者還沒開啟此專案以前,都是在使用 APC 跟 eaccelerator,如果碰到任何 bug,作者都會第一時間告訴官方,作者也跟 eaccelerator 其中一位開發者
zoeloelip 有互動,zoeloelip 是第一位看到 XCache 程式碼,也因為他看過了 Xcache,所以他參考 Xcache 作者 Disassembler idea 來改寫 ea_dasm.c 大部分程式碼。這部份作者也是猜測的,最後作者有提到他為什麼寫了這專案,其實很簡單。
- 作者有太多 idea 無法實做在 ea/apc
- ea/apc 架構過於龐大,所以無法實際用在裡面
- 作者用來練功
最後作者也認同 ea/apc 是不錯的加速器,大家可以實際用看看,感受一下加速過後的 PHP。也可以參考
FeatureList 看看 Xcache 有哪些功能。
安裝 Xcache
這邊環境是 FreeBSD 7.1 Release ports 安裝方式:
$ cd /usr/ports/www/xcache/; make install clean
安裝完成之後要進行設定,設定檔放在
/usr/local/share/examples/xcache/,裡面也包含了 admin 管理介面。
$ cp /usr/local/share/examples/xcache/xcache.ini /usr/local/etc/php/
接著附上我機器的設定檔
[xcache-common]
extension = xcache.so
[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "admin"
xcache.admin.pass = "xxxxxxx"
[xcache]
xcache.shm_scheme = "mmap"
xcache.size = 16M
xcache.count = 1
xcache.slots = 8K
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 2M
xcache.var_count = 1
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.test = Off
xcache.readonly_protection = On
xcache.mmap_path = "/tmp/xcache"
xcache.coredump_directory = ""
xcache.cacher = On
xcache.stat = On
xcache.optimizer = Off
[xcache.coverager]
xcache.coverager = Off
xcache.coveragedump_directory = ""
這邊有要注意的地方就是在 Unix 系統設定 xcache.mmap_path 的時候,這是
檔案而並非是目錄,這邊設定好之後,請先建立 /tmp/xcache 檔案
$ touch /tmp/cache && chmod 777 /tmp/cache
安裝完成之後請重新啟動 apache 或 lighttpd:
$ /usr/local/etc/rc.d/apache22 restart
$ /usr/local/etc/rc.d/lighttpd restart
最後直接可以看到 phpinfo 的相關訊息: 補個圖

或者是打看看 php -v 也可以看到訊息:
PHP 5.3.8 (cli) (built: Sep 20 2011 14:37:25)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
with XCache v1.3.2, Copyright (c) 2005-2011, by mOo
安裝 Xcache 管理介面
設定管理者帳號密碼 (
/usr/local/etc/php/xcache.ini 裡面) 請注意
xcache.admin.enable_auth = On
xcache.admin.user = "admin"
xcache.admin.pass = "xxxxxxxxxxxx"
密碼該如何產生呢?可以透過 php 指令去產生您要的 md5 密碼
php -r "echo md5('admin');"
或者是透過網頁
最後將 admin 目錄放到您網站根目錄底下即可
$ cp -r /usr/local/share/examples/xcache/admin /usr/local/www/apache22/data/xcache
網址列打入 http://xxxxx/xcache 並且輸入上面所設定好的帳號密碼即可

參考網站:
http://xcache.lighttpd.net/wiki/InstallAdministration
Related