三款免費的PHP加速器:APC、eAccelerator、XCache比較

來源:互聯網
上載者:User

標籤:伺服器   加速器   訪問量   原始碼   

大綱:

1、php加速器原理  

2、幾種流行的php加速器的安裝與配置

3、比較


一、PHP加速器原理

    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加速器就這樣誕生了。

    每一次的請求都會反覆執行Parse-Compile-Execute,而在實際中,服務端的php代碼一般都不會發生變化,我們每次請求都要反覆執行一些沒有必要的操作,這直接影響了PHP的效能,我們到這裡肯定會想到為什麼不用緩衝了?目前已經有很多成熟的緩衝機制,用在PHP代碼的執行方面應該綽綽有餘,就算我們的PHP代碼會發生變化,我們也可以用一種成熟的演算法來保證代碼改變後重新緩衝,這一切都不是問題。是的,這就是PHP加速器的工作原理:


      緩衝前                                         緩衝後

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/41/43/wKiom1PR--uhbyuDAAClKSsQkDg313.jpg" title="1.png" alt="wKiom1PR--uhbyuDAAClKSsQkDg313.jpg" />                   650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/41/43/wKioL1PR_XHxoftlAAGqDAz8hHw197.jpg" title="9.png" alt="wKioL1PR_XHxoftlAAGqDAz8hHw197.jpg" />


二、幾種流行的php加速器的安裝與配置

1、安裝配置APC

    APC全稱是Alternative PHP Cache,它是PHP PECL中的一個擴充。

$ wget http://pecl.php.net/get/APC-3.0.19.tgz$ tar xvzf APC-3.0.19.tgz$ cd APC-3.0.19/APC-3.0.19$ /usr/local/php/bin/phpize$ ./configure --enable-apc --enable-apc-mmap --with-php-config=/usr/local/php/bin/php-config$ make$ make install

下面我們再配置APC,因為我的PECL擴充路徑改變了,所以我得移動下編譯好的檔案:

$sudo mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/apc.so /usr/local/php/lib/php/extensions/PECL

然後我們再編輯php.ini檔案進行配置,請把下面的代碼加入到php.ini中即可:

extension_dir = "/usr/local/php/lib/php/extensions/PECL"extension = apc.so; APCapc.enabled = 1apc.shm_segments = 1apc.shm_size = 64apc.optimization = 1apc.num_files_hint = 0apc.ttl = 0apc.gc_ttl = 3600apc.cache_by_default = on

這樣重啟apache就會在phpinfo()資訊中顯示。

       

2、安裝配置eAccelerator

   eAccelerator的前身其實是truck-mmcache,因為開發truk-mmcache的人被Zend給招安了,所以開發eAccelerator的人繼承了truk-mmcache的一些特性,設計出eAccelerator加速器。安裝如下:

$wget http://jaist.dl.sourceforge.net/sourceforge/eaccelerator/eaccelerator-0.9.5.tar.bz2$ tar -jxf eaccelerator-0.9.5.tar.bz2$ cd eaccelerator-0.9.5$ /usr/local/php/bin/phpize$./configure –enable-eaccelerator=shared –with-php-config=/usr/local/php/bin/php-config$ make$ make install$ mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so /usr/local/php/lib/php/extensions/PECL

將下面代碼加入php.ini檔案中

extension = eaccelerator.so; eAcceleratoreaccelerator.shm_size = "16"eaccelerator.cache_dir = "/tmp/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.prune_period = "0"eaccelerator.shm_only = "0"eaccelerator.compress = "1"eaccelerator.compress_level = "9"建立緩衝目錄,重啟apache$mkdir /tmp/eaccelerator$chmod 777 /tmp/eaccelerator$/usr/local/apache/apachectl restart

在phpinfo()檢查是否安裝成功.


3、安裝配置XCache

    XCache作為國人自己開發的東西,做小菜鳥的我也感到驕傲,而且XCache無論在速度還是效能上都做的不錯。下面就趕緊讓我們品嘗它吧!

$wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz$tar xvzf xcache-1.2.2.tar.gz$cd xcache-1.2.2$/usr/local/php/bin/phpize$./configure –enable-xcache –enable-xcache-coverager –with-php-config=/usr/local/php/php-config$make$sudo make install$sudo mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so /usr/local/php/lib/php/extensions/PECL

在php.ini添加配置資訊:

extension = xcache.so; xcachexcache.admin.user = "admin"xcache.admin.pass = "(執行) echo ’(你的密碼)’|md5sum(得出的密文)";xcache.size = 24Mxcache.shm_scheme = "mmap"xcache.count = 2xcache.slots = 8kxcache.ttl = 0xcache.gc_interval = 0xcache.var_size = 8Mxcache.var_count = 1xcache.var_slots = 8kxcache.var_ttl = 0xcache.var_maxttl = 0xcache.var_gc_interval = 300xcache.test = Offxcache.readonly_protection = Onxcache.mmap_path = "/tmp/xcache"xcache.coredump_directory = ""xcache.cacher = Onxcache.stat = Onxcache.optimizer = Off;xcache.coverager = Onxcache.coveragedump_directory = ""建立緩衝目錄,重啟apache$mkdir /tmp/xcache$chmod 777 /tmp/xcache$/usr/local/apache/bin/apachectl restart


三、比較

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/41/43/wKiom1PR_33we3c2AAOnbhWEdYI574.jpg" title="cache.png" alt="wKiom1PR_33we3c2AAOnbhWEdYI574.jpg" />

轉自:http://www.vpser.net/opt/apc-eaccelerator-xcache.html



本文出自 “Share your knowledge” 部落格,請務必保留此出處http://skypegnu1.blog.51cto.com/8991766/1530095

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.