首頁  >  文章  >  後端開發  >  PHP加速器eAccelerator配置使用步驟詳解

PHP加速器eAccelerator配置使用步驟詳解

php中世界最好的语言
php中世界最好的语言原創
2018-05-21 09:28:556640瀏覽

這次帶給大家PHP加速器eAccelerator配置使用步驟詳解,PHP加速器eAccelerator配置使用的注意事項有哪些,下面就是實戰案例,一起來看一下。

為了更好的提高系統的性能考慮對PHP再進行一些優化,前兩年接觸過MMCache和eAccelerator,尤其對eAccelerator非常喜歡,這次優化也選擇了它。

分享一些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官方下載最新版的源碼包:eaccelerator-0.9.5-beta.tar.bz2


#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 instal

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

我的機器上同時也安裝了Zend Optimizer3.0.1,所以看到的資訊如下:

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
    with Zend Extension Manager v1.0.10, Copyright (c) 2003-2006, by Zend Technologies
    with Zend Optimizer v3.0.1, Copyright (c) 1998-2006, by Zend Technologies

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


#tail /var/log/httpd/eAccelerator_log
EACCELERATOR 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.php
dasm.php
encoder.php
info.php
loader.php
session.php
shared_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(http://bart.eaccelerator.net/doc/phpdoc/)

下面有部分网友翻译后的接口说明:

eaccelerator_put($key, $value, $ttl=0)

  将 $value 以 $key 为键名存进缓存(php4下支持对像类型,看源码好像zend2里不支持了),$ttl 是这个缓存的生命周期,单位是秒,省略该参数或指定为 0 表示不限时,直到服务器重启清空为止。

eaccelerator_get($key)  根据 $key 从缓存中返回相应的 eaccelerator_put() 存进去的数据,如果这项缓存已经过期或不存在那么返回值是 NULL
eaccelerator_rm($key)  根据 $key 移除缓存
eaccelerator_gc()  移除清理所有已过期的 key
eaccelerator_lock($key)  为 $key 加上锁定操作,以保证多进程多线程操作时数据的同步。需要调用 eaccelerator_unlock($key) 来释放这个锁或等待程序请求结束时自动释放这个锁。


eaccelerator_unlock($key)

  根据 $key 释放锁

eaccelerator_cache_output($key, $eval_code, $ttl=0)

  将 $eval_code 代码的输出缓存 $ttl 秒,($ttl参数同 eacclerator_put)
  例如:




eaccelerator_cache_result($key, $eval_code, $ttl=0)

  将 $eval_code 代码的执行结果缓存 $ttl 秒,($ttl参数同 eacclerator_put),类似 cache_output
  例如:



eaccelerator_cache_page($key, $ttl=0)

  将当前整页缓存 $ttl 秒。
  例如:




eaccelerator_rm_page($key)

删除由  eaccelerator_cache_page() 执行的缓存,参数也是 $key

2、PHP代码中使用eAccelerator加速

测试下eAccelerator强大的威力:(该代码在 cli 模式下可能无效)

abe162531fa68c29917dbd7eb948bd26\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!0c6dc11e160d3b678d68754cc175188a\n";
}
else {
  echo "cached0c6dc11e160d3b678d68754cc175188a\n";
}
 
echo $tt->pro;
$tt->func();
$tt->now(time() + 86400);
?>

另外,据说在著名的vBulletin 3.60Beta版里面已经集成了对eAccelerator的支持。

一段来自vBulletin里面的代码

a1b4def77a7d2348d09dbd178988d01ddefaultitems 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);
    }
}
?>

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

PHP7基于函数方式使用lib库案例代码分析

PHP找出链表中环入口节点步骤详解

以上是PHP加速器eAccelerator配置使用步驟詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn