search
HomeBackend DevelopmentPHP TutorialDetailed explanation of the configuration and usage steps of PHP accelerator eAccelerator

This time I will bring you a detailed explanation of the configuration and use steps of the PHP accelerator eAccelerator. What are the precautions for the configuration and use of the PHP accelerator eAccelerator? The following is a practical case, let's take a look.

In order to better improve the performance of the system, consider optimizing PHP. I have been exposed to MMCache and eAccelerator in the past two years, and I especially like eAccelerator very much. I also chose it for this optimization.

Share some eAccelerator configuration and optimization documents.

1. Introduction to eAccelerator

1. Background

eAccelerator is a free and open source PHP It is a project of acceleration, optimization, compilation and dynamic caching, which can improve the performance of PHP scripts by caching the results of PHP code compilation, completely solving the PHP script compilation problem that has always been complex and far away from us. By using eAccelerator, you can optimize the execution speed of your PHP code, reduce the server load, and increase the execution speed of PHP applications up to 10 times.

The eAccelerator project was born in 2004, when it was proposed and developed as a branch of the Turck MMCache project. Turck MMCache was developed by Dmitry Stogov. It is a very excellent PHP memory cache acceleration system. Today, a large part of the eAccelerator code is still applied to this project. Currently, the project has not been updated for a long time. For the latest PHP5.x Support is not yet available.

2. Principle

eAccelerator caches the compiled PHP code into shared memory and calls it directly when the user accesses it to achieve efficient acceleration. effect. It is very efficient. From creating shared memory to searching for compiled code, it is completed in a very short time. For files and codes that cannot be cached in shared memory, eAccelerator can also cache them on the system disk.

eAccelerator also supports the compilation and interpretation of PHP code. You can compile the PHP code through the encoder.php script to protect the code. The compiled code must be run in an environment where eAccelerator is installed. Down. The code compiled by eAccelerator cannot be decompiled, unlike some other compilation tools that can be decompiled, which will make the code safe and efficient.

2. eAccelerator installation and configuration

1. Support platform

Because aAccelerator provides a large Part of the API is based on shared memory, so it will be better supported on the *nix platform. Although a binary version based on the windows platform has also been released, I will only provide configuration and instructions based on the *nix platform here. Currently, it can Supported platforms include Linux, FreeBSD, OpenBSD, Mac OS X, Solaris, AIX en HP-UX.

2. System requirements

  • php4 or php5

  • ##autoconf

  • automake

  • libtool

  • m4

eAccelerator only supports the use of mod_php Or fastcgi mode installed PHP

3, install

First go to eAccelerator official website to download the latest version of the source code package: 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 file configuration

After the installation is complete, let’s start configuring the php.ini file. eAccelerator provides two configuration and calling methods, as follows.

Installation as Zend extension mode:

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"
If you use PHP installed in thread safe mode, you must use "zend_extension_ts" to replace "zend_extension" in the first line.

Installation as PHP extension mode: (This is the method used by most)


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"
For detailed configuration instructions of the php.ini file, please refer to the README document in the source code directory or visit the official document: ini setting

After completing the installation configuration, we finally need to create the cache directory

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

5. Verify the installation results

Access your phpinfo() through the browser Page or run php -i to get the PHP configuration information. If you see information similar to the following, the installation is successful.

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 is also installed on my machine, so the information I see is as follows:

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

If you turn on the debug option of eAccelerator, you can see something like this from the log Information below


#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找出链表中环入口节点步骤详解

The above is the detailed content of Detailed explanation of the configuration and usage steps of PHP accelerator eAccelerator. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function