PHP 加速器是一個為了提高 PHP 執行效率,從而緩存起 PHP 的操作碼,這樣 PHP 后面執行就不用解析轉換了,可以直接調用 PHP 操作碼,這樣速度上就提高了不少。
Apache 中使用 mod_php 的請求、響應執行流程:
1、Apache 接收請求
2、Apache 傳遞請求給 mod_php
3、mod_php 定位磁盤文件,並加載到內存中
4、mod_php 編譯源代碼成為 opcode 樹
5、mod_php 執行 opcode 樹PHP加速器相應的就是第四步,它的目的就是防止PHP每次請求都重復編譯PHP代碼,因為在高訪問量的網站上,大量的編譯往往沒有執行速度快呢?所以這里面有個瓶頸就是PHP的重復編譯既影響了速度又加載了服務器負載,為了解決此問題,PHP加速器就這樣誕生了。
APC:Alternative PHP Cache(APC)是 PHP 的一個免費公開的優化代碼緩存。它用來提供免費,公開並且強健的架構來緩存和優化 PHP 的中間代碼。
鏈接地址:http://php.net/manual/en/book.apc.php
eAccelerator:eAccelerator 是一個自由開放源碼php加速器,優化和動態內容緩存,提高了 php 腳本的緩存性能,使得PHP腳本在編譯的狀態下,對服務器的開銷幾乎完全消除。 它還有對腳本起優化作用,以加快其執行效率。使您的PHP程序代碼執效率能提高1-10倍(截取自:http://baike.baidu.com/view/1376127.htm)
XCache:XCache 是一個開源的 opcode 緩存器/優化器, 這意味着他能夠提高您服務器上的 PHP 性能. 他通過把編譯 PHP 后的數據緩沖到共享內存從而避免重復的編譯過程, 能夠直接使用緩沖區已編譯的代碼從而提高速度. 通常能夠提高您的頁面生成速率 2 到5 倍, 降低服務器負載(截取自:http://baike.baidu.com/view/1999371.htm)
APC VS eAccelerator VS XCache
速度比較
圖中可以看到:APC,XCache,eAccelerator 的測試數據,相對來說 XCache 表現最好,但是每個加速器都有各種微調參數,具體視部署環境而定。
版本存活狀態
(截取自:http://en.wikipedia.org/wiki/List_of_PHP_accelerators)
Alternative PHP Cache (APC)
Alternative PHP Cache is a free, open source (PHP license) framework that optimizes PHP intermediate code and caches data and compiled code from the PHP bytecode compiler in shared memory.
- Website: http://pecl.php.net/package/APC
- PHP version: works with all PHP versions including PHP 5.2-5.4 (3.1.10 - beta release) (not PHP 5.0)
- Latest stable version: 3.1.9 (released 2011-05-14)
- Status: stable, actively maintained.
- Download link: http://pecl.php.net/package/APC (source)
- Download link for Windows: http://downloads.php.net/pierre/ (provides some of the PECL extensions, previously available on pecl4win)
- Official installation help: http://php.net/apc.setup
eAccelerator
eAccelerator was born in December 2004 as a fork of the Turck MMCache project. Turck MMCache was created by Dmitry Stogov and much of the eAccelerator code is still based on his work. eAccelerator also contained a PHP encoder and loader, but the development staff discontinued the encoder and removed this feature after December 2006.
- Website: http://eaccelerator.net/
- PHP version: Supports PHP 4 and all PHP 5 thread-safe releases including 5.3 from version 0.9.6; breaks on 5.4. In older releases, the encoder will only work with PHP versions from the 4.x.x branch. eAccelerator will not work with any other versions of PHP. eAccelerator can only be used with the thread-safe version of PHP. (This seems not to be true, this version works for my php non-thread-safe installation: http://eac.qme.nl/eAccelerator_v1_0_svn427_for_v5_3_X-VC9.zip)
- Latest stable version: 0.9.6.1 (2010-05-31)
- Status: Potentially dead (no new releases since May 2010). UPDATE from eaccelerator.net: 25 Jul 2012 - Project was taken by Hans Rakers and moved to Github, master branch does support PHP 5.4
- Download link: (look for latest versions) http://eaccelerator.net/, download page on SourceForge
- Official installation help: https://eaccelerator.net/wiki/InstallFromSource
XCache
XCache is a fast, stable PHP opcode cacher that has been tested and is now running on production servers under high load. It is tested on Linux and FreeBSD and supported under Windows, for thread-safe and non-thread-safe versions of PHP. This relatively new opcode caching software has been developed by mOo, one of developers of Lighttpd, to overcome some of the limitations of the existing solutions at that time; such as being able to use it with new PHP versions as they arrive.
- Website: http://xcache.lighttpd.net/
- PHP version: full support for PHP 5.4
- Latest stable version: 2.0.1 (Released on 2012-07-14)
- Status: Actively maintained (different releases stable/unstable/devel)
- Download link: http://xcache.lighttpd.net/pub/Releases/2.0.1/xcache-2.0.1.tar.gz
- Official installation help: http://xcache.lighttpd.net/wiki/InstallFromSource
目前 APC 和 XCache 都有積極的維護和更新版本,eAccelerator 已經接近死亡狀態了。