php加速器 eAccelerator配備和使用指南

來源:互聯網
上載者:User
php加速器 eAccelerator配置和使用指南

一、eAccelerator介紹

1、背景
eAccelerator 是一個免費開源的PHP加速、最佳化、編譯和動態緩衝的項目,它可以通過緩衝PHP代碼編譯後的結果來提高PHP指令碼的效能,使得一向很複雜和離我們很遠的 PHP指令碼編譯問題完全得到解決。通過使用eAccelerator,可以最佳化你的PHP代碼執行速度,降低伺服器負載,可以提高PHP應用執行速度最高 達10倍。

eAccelerator 項目誕生於2004年,當時它是作為 Turck MMCache 項 目的一個分支提出並投入開發的。 Turck MMCache 由 Dmitry Stogov 開發,是個非常優秀的PHP記憶體緩衝加速系統,如今仍然有很大部分 eAccelerator 的代碼應用到該項目中,目前該項目有很長時間沒有更新了,對於最新的PHP5.x的支援還未推出。

2、原理
eAccelerator 通過把經過編譯後的PHP代碼緩衝到共用記憶體中,並在使用者訪問的時候直接調用從而起到高效的加速作用。它的效率非常高,從建立共用記憶體到尋找編譯後的代碼 都在非常短的時間內完成,對於不能緩衝到共用記憶體中的檔案和代碼,eAccelerator還可以把他們緩衝到系統磁碟上。

eAccelerator 同樣還支援PHP代碼的編譯和解釋執行,你可以通過encoder.php指令碼來對php代碼進行編譯達到保護代碼的目的,經過編譯後的代碼必須運行在安 裝了eAccelerator的環境下。eAccelerator編譯後的代碼不能被反編譯,它不象其他一些編譯工具那樣可以進行反編譯,這將使得代碼更 加安全和高效。

二、eAccelerator安裝配置

1、支援平台
由於aAccelerator提供了大部分基於共用記憶體的API,所以在*nix的平台上將得到更好的支援,雖然也發布了基於windows平台的 binary版本,但我在這裡就只提供基於*nix平台的配置和說明,目前可以支援的平台包括Linux, FreeBSD, OpenBSD, Mac OS X, Solaris, AIX en HP-UX。

2、系統要求
php4 or php5
autoconf
automake
libtool
m4
eAccelerator 只支援使用 mod_php 或者 fastcgi mode 安裝的PHP

?

3、安裝
先去eAccelerator官方下載最新版的源碼包:http://sourceforge.net/projects/eaccelerator/

#tar -zxvf ./eaccelerator-0.9.5-beta2.tar.bz2#cd eaccelerator-0.9.5-beta2#export PHP_PREFIX="/usr/local" (把PHP安裝目錄匯入到環境變數,FreeBSD預設是/usr/local)#$PHP_PREFIX/bin/phpize#./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config#make#make install 

?

4、ini檔案配置
安裝完成,下面開始配置php.ini檔案,eAccelerator提供了兩種配置和調用方式,分別如下。

安裝為 Zend extension 模式:

zend_extension="/usr/local/lib/php/20050922/eaccelerator.so"eaccelerator.shm_size="16"eaccelerator.cache_dir="/tmp/eaccelerator"eaccelerator.enable="1"eaccelerator.optimizer="1"eaccelerator.check_mtime="1"eaccelerator.debug="0"eaccelerator.log_file = "/var/log/httpd/eaccelerator_log"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"

?

如果你使用了thread safe模式安裝的PHP,你必須使用 “zend_extension_ts” 替換第一行的 “zend_extension”.

安裝為 PHP extension 模式:(這是大部分採用的方式)

?

extension="eaccelerator.so"eaccelerator.shm_size="16"eaccelerator.cache_dir="/tmp/eaccelerator"eaccelerator.enable="1"eaccelerator.optimizer="1"eaccelerator.check_mtime="1"eaccelerator.debug="0"eaccelerator.log_file = "/var/log/httpd/eaccelerator_log"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"

?

有關php.ini檔案的詳細配置說明,請參照源碼目錄的README文檔或者訪問官方文檔:ini setting

完成安裝配置後,我們最後要建立緩衝目錄

?

#mkdir /tmp/eaccelerator#chmod 777 /tmp/eaccelerator

?5、驗證安裝結果
通過瀏覽器訪問您的phpinfo()頁面或者運行 php -i 得到php配置資訊,裡面如果看到類似下面的資訊就表示安裝成功了。

This program makes use of the Zend Scripting Language Engine:Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies    with eAccelerator v0.9.5-beta2, Copyright (c) 2004-2006 eAccelerator, by eAccelerator

?

如果你開啟了eAccelerator的debug選項,可以從日誌中看到類似下面的資訊

#tail /var/log/httpd/eAccelerator_logEACCELERATOR hit: "/var/www/toplee.com/blog/index.php"EACCELERATOR hit: "/var/www/toplee.com/blog/wp-blog-header.php"EACCELERATOR hit: "/var/www/toplee.com/blog/wp-config.php"EACCELERATOR hit: "/var/www/toplee.com/blog/wp-settings.php"EACCELERATOR hit: "/var/www/toplee.com/blog/wp-content/plugins/wp-cache/wp-cache-phase1.php"...

?

以上資訊表示檔案都得到了緩衝和命中。

至此,我們就完成了全部的安裝和配置,好好享受eAccelerator帶給你的驚喜吧,根據Michael的測試,效果的確相當的好。

?

三、在PHP中可以使用eAccelerator的API開發

1、API和文檔說明:

eAccelerator提供了便捷便捷而又穩定的本機緩衝實現方式,由於大部分代碼實現基於共用記憶體,所以只能在*nix平台中使用,Windows平台Michael就暫時不知道何時有這方面的支援了。
eAccelerator提供如下的API介面和檔案:(下述檔案均在源碼包的doc/php/目錄下)

檔案清單:

?

cache.phpdasm.phpencoder.phpinfo.phploader.phpsession.phpshared_memory.php

?

介面列表:

array eaccelerator_cached_scripts ()void eaccelerator_cache_output (string $key, string $eval_code, [int $ttl = 0])void eaccelerator_cache_page (string $key, [int $ttl = 0])void eaccelerator_cache_result (string $key, string $code, [int $ttl = 0])void eaccelerator_caching (boolean $flag)void eaccelerator_clean ()void eaccelerator_clear ()array eaccelerator_dasm_file (mixed $filename)mixed eaccelerator_encode (mixed $src, [mixed $prefix = ''], [string $pre_content = ''], [string $post_content = '']) void eaccelerator_gc ()mixed eaccelerator_get (string $key) array eaccelerator_info ()array eaccelerator_list_keys ()void eaccelerator_load ()boolean eaccelerator_lock (string $key)void eaccelerator_optimizer (boolean $flag) void eaccelerator_purge ()boolean eaccelerator_put (string $key, mixed $value, [int $ttl = 0])array eaccelerator_removed_scripts ()boolean eaccelerator_rm (string $key)void eaccelerator_rm_page (string $key) boolean eaccelerator_set_session_handlers ()boolean eaccelerator_unlock (string $key)

?有關上述文檔詳細說明請參考官方文檔:API Documents

?

2、PHP代碼中使用eAccelerator加速

下面有一個測試的代碼,你可以測試一下eAccelerator強大的威力:(該代碼在 cli 模式下可能無效)

?

\n";  }  function func() {    echo ', the world!';  }  function now($t) {    echo date('Y-m-d H:i:s', $t);  }} $tt = eaccelerator_get("test_tt");if (!$tt){  $tt = new test_cache;  eaccelerator_put("test_tt", $tt);  echo "no cached!
\n";}else { echo "cached
\n";} echo $tt->pro;$tt->func();$tt->now(time() + 86400);?>

?

另外,據說在著名的vBulletin 3.60Beta版裡面已經整合了對eAccelerator的支援,下面是一段來自vBulletin裡面的代碼

defaultitems AS $item)        {            $this->do_fetch($item);        }         if (is_array($itemarray))        {            foreach ($itemarray AS $item)            {                $this->do_fetch($item);            }        }         $this->check_options();         // set the version number variable        $this->registry->versionnumber =& $this->registry->options['templateversion'];    }     /**    * Fetches the data from shared memory and detects errors    *    * @param    string    title of the datastore item    *    * @return    void    */    function do_fetch($title)    {        $data = eaccelerator_get($title);        if ($data === null)        { // appears its not there, lets grab the data, lock the shared memory and put it in            $data = '';            $dataitem = $this->dbobject->query_first("                SELECT title, data FROM " . TABLE_PREFIX . "datastore                WHERE title = '" . $this->dbobject->escape_string($title) ."'            ");            if (!empty($dataitem['title']))            {                $data =& $dataitem['data'];                $this->build($dataitem['title'], $dataitem['data']);            }        }        $this->register($title, $data);    }        /**    * Updates the appropriate cache file    *    * @param    string    title of the datastore item    *    * @return    void    */    function build($title, $data)    {        if (!function_exists('eaccelerator_put'))        {            trigger_error("eAccelerator not installed", E_USER_ERROR);        }        eaccelerator_lock($title);        eaccelerator_put($title, $data);        eaccelerator_unlock($title);    }}?>

?

四、附錄和參考資料
eAccelerator 官方網站 :http://eaccelerator.net

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.