搜尋
首頁後端開發php教程PHP加速器eAccelerator配置使用步驟詳解

這次帶給大家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) 来释放这个锁或等待程序请求结束时自动释放这个锁。
<?php    
eaccelerator_lock("count");    
eaccelerator_put("count",eaccelerator_get("count")+1));  
?>

eaccelerator_unlock($key)

  根据 $key 释放锁

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

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

<?php 
eaccelerator_cache_output(&#39;test&#39;, &#39;echo time(); phpinfo();&#39;, 30); 
?>


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

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

<?php 
eaccelerator_cache_result(&#39;test&#39;, &#39; time() . "Hello";&#39;, 30); 
?>

eaccelerator_cache_page($key, $ttl=0)

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

<?php    
eaccelerator_cache_page($_SERVER[&#39;PHP_SELF&#39;].&#39;?GET=&#39;.serialize($_GET),30);
echo time();
phpinfo();
?>


eaccelerator_rm_page($key)

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

2、PHP代码中使用eAccelerator加速

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

<?php class test_cache {
  var $pro = &#39;hello&#39;;
 
  function test_cache() {
    echo "Object Created!<br>\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!<br>\n";
}
else {
  echo "cached<br>\n";
}
 
echo $tt->pro;
$tt->func();
$tt->now(time() + 86400);
?>

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

一段来自vBulletin里面的代码

<?php // #############################################################################
// eAccelerator
 
/**
* Class for fetching and initializing the vBulletin datastore from eAccelerator
*
* @package    vBulletin
* @version    $Revision: 0.1 $
* @date        $Date: 2005/06/12 13:14:18 $
*/
class vB_Datastore_eAccelerator extends vB_Datastore
{
    /**
    * Fetches the contents of the datastore from eAccelerator
    *
    * @param    array    Array of items to fetch from the datastore
    *
    * @return    void
    */
    function fetch($itemarray)
    {
        if (!function_exists(&#39;eaccelerator_get&#39;))
        {
            trigger_error("eAccelerator not installed", E_USER_ERROR);
        }
 
        foreach ($this->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);
    }
}
?>

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

推荐阅读:

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

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

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

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何檢查PHP會話是否已經開始?如何檢查PHP會話是否已經開始?Apr 30, 2025 am 12:20 AM

在PHP中,可以使用session_status()或session_id()來檢查會話是否已啟動。 1)使用session_status()函數,如果返回PHP_SESSION_ACTIVE,則會話已啟動。 2)使用session_id()函數,如果返回非空字符串,則會話已啟動。這兩種方法都能有效地檢查會話狀態,選擇使用哪種方法取決於PHP版本和個人偏好。

描述一個場景,其中使用會話在Web應用程序中至關重要。描述一個場景,其中使用會話在Web應用程序中至關重要。Apr 30, 2025 am 12:16 AM

sessionsarevitalinwebapplications,尤其是在commercePlatform之前。

如何管理PHP中的並發會話訪問?如何管理PHP中的並發會話訪問?Apr 30, 2025 am 12:11 AM

在PHP中管理並發會話訪問可以通過以下方法:1.使用數據庫存儲會話數據,2.採用Redis或Memcached,3.實施會話鎖定策略。這些方法有助於確保數據一致性和提高並發性能。

使用PHP會話的局限性是什麼?使用PHP會話的局限性是什麼?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

解釋負載平衡如何影響會話管理以及如何解決。解釋負載平衡如何影響會話管理以及如何解決。Apr 29, 2025 am 12:42 AM

負載均衡會影響會話管理,但可以通過會話複製、會話粘性和集中式會話存儲解決。 1.會話複製在服務器間複製會話數據。 2.會話粘性將用戶請求定向到同一服務器。 3.集中式會話存儲使用獨立服務器如Redis存儲會話數據,確保數據共享。

說明會話鎖定的概念。說明會話鎖定的概念。Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

有其他PHP會議的選擇嗎?有其他PHP會議的選擇嗎?Apr 29, 2025 am 12:36 AM

PHP會話的替代方案包括Cookies、Token-basedAuthentication、Database-basedSessions和Redis/Memcached。 1.Cookies通過在客戶端存儲數據來管理會話,簡單但安全性低。 2.Token-basedAuthentication使用令牌驗證用戶,安全性高但需額外邏輯。 3.Database-basedSessions將數據存儲在數據庫中,擴展性好但可能影響性能。 4.Redis/Memcached使用分佈式緩存提高性能和擴展性,但需額外配

在PHP的上下文中定義'會話劫持”一詞。在PHP的上下文中定義'會話劫持”一詞。Apr 29, 2025 am 12:33 AM

Sessionhijacking是指攻擊者通過獲取用戶的sessionID來冒充用戶。防範方法包括:1)使用HTTPS加密通信;2)驗證sessionID的來源;3)使用安全的sessionID生成算法;4)定期更新sessionID。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!