Table of Contents
使用strace查看C语言级别的php源码       
2、PHP代码中使用eAccelerator加速
Home php教程 php手册 PHP安装eAccelerator

PHP安装eAccelerator

Jun 06, 2016 pm 07:53 PM
eaccelerator php Install

eAccelerator是一个非常优秀的PHP加速器,通过缓存编译之后的PHP代码文件,使得PHP编译对服务器的开销几乎为零。eAccelerator本身的开销也是极少的 最新的PHP5.4.8安装eAccelerator, 需要注意的是要安装高版本的,低了编译不过去。 官网:http://eaccelerat

eAccelerator是一个非常优秀的PHP加速器,通过缓存编译之后的PHP代码文件,使得PHP编译对服务器的开销几乎为零。eAccelerator本身的开销也是极少的

 

最新的PHP5.4.8安装eAccelerator,

需要注意的是要安装高版本的,低了编译不过去。

官网:http://eaccelerator.net/

 

注意PHP5.5以后暂时不能使用eAccelerator

原因:其中一个我知道的是eAccelerator中使用了

    php_register_info_logo(EACCELERATOR_VERSION_GUID, "text/plain", (unsigned char*)EACCELERATOR_VERSION_STRING, sizeof(EACCELERATOR_VERSION_STRING));
    php_register_info_logo(EACCELERATOR_LOGO_GUID,    "image/gif",  (unsigned char*)eaccelerator_logo, sizeof(eaccelerator_logo));
Copy after login


php_register_info_logo在PHP5.5中已经移除了

 

1.下载安装eAccelerator

#wget https://github.com/eaccelerator/eaccelerator/tarball/master

最新的版本是eaccelerator-42067ac.tar.gz

#tar zxvf eaccelerator-42067ac.tar.gz

需要phpize安装php扩展,如果不明白phpize可以看这个:http://blog.csdn.net/21aspnet/article/details/8193482

需要注意的是需要写php的安装路径对应的phpize下面的php-config也是

/usr/local/webserver/php/bin/phpize

#./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config

# make

需要注意的是要安装高版本的,低了编译不过去。

# make install

PHP安装eAccelerator

 

需要记住生成的提示信息下面配置php要用到

/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-

20100525/

2.配置php.ini配置文件

#这个是我机器的so位置
extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so"
#这个是设置一个缓存大小
eaccelerator.shm_size="64"
#缓存目录
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"

#网站目录,把解开的eAccelerator 的源码包里的control.php 文件复制到你的WEB 服务器中的一个目录下,使其它以通过http 访问,如果不加那么看不到缓存的页面列表,但是还是可以看到内存信息

eaccelerator.allowed_admin_path=/usr/www/

eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
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"

eaccelerator.keys     = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content  = "disk_only"

设置内容缓存的存放的地方,可以设置为:
shm_and_disk 在共享缓存和硬盘(默认值)
shm      默认存在共享内存,如果共享内存已满或大小超过 "eaccelerator.shm_max" 的值,就存到硬盘
shm_only    只存放在共享内存
disk_only    只存放在硬盘
none      不缓存数据

这里为了查看效果设置为存在硬盘

 

增加权限

#mkdir /usr/local/webserver/eaccelerator_cache
#chmod 777 /usr/local/webserver/eaccelerator_cache

 

3.测试查看phpinfo()

PHP安装eAccelerator

PHP安装eAccelerator

 

4.查看缓存文件夹

为了直观所以截图图形界面

PHP安装eAccelerator

这说明成功生成了缓存

 

5.更好的监控方式

eAccelerator控制面板的地址,安装包里有一个control.php文件,把它复制到网站的任意目录,可以用它查看和管理,这个必须指定,否则查看缓存内容的时候会出错,访问时候默认的用户名是:admin,密码:eAccelerator

PHP安装eAccelerator

 

注意,之前的php.ini配置文件加上这个配置就会看到明细信息

eaccelerator.allowed_admin_path=/usr/www/

PHP安装eAccelerator

 

重启apache则会清空缓存

 

文档:

eaccelerator.shm_size="8"
eAccelerator 可以使用的共享内存的数量 (以MB为单位) . "0" 是指操作系统的默认值,默认值是 "0",可根据服务器的实际情况来调整:16、32、64、128都是可以的。

eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"
这个目录是给磁盘缓存使用,eAccelerator 在这里储存预先编译好的代码,进程数据,内容以及用户的自定义内容。同样的数据也能被储存在共享内存中 (这样可以提高访问速度)

eaccelerator.enable="1"
开启或关闭 eAccelerator,"1" 为开启,"0" 为关闭。默认值为 "1"。

eaccelerator.optimizer="1"
启或关闭内部优化器,可以提升代码执行速度。"1" 为开启,"0" 为关闭。默认值为 "1"。

eaccelerator.check_mtime="1"
打开或者关闭 PHP 的文件修改检查,"1" 是指打开,"0" 是指关闭。默认值是 "1"。

eaccelerator.debug="0"
开启或关闭调试日志记录。"1" 为开启,"0" 为关闭。默认值为 "0"。会将缓存命中得记录写入日志。

eaccelerator.filter=""
判断哪些 PHP 文件必须缓存。可以指定缓存和不缓存的文件类型(如 "*.php *.phtml"等)如果参数以 "!" 开头,则匹配这些参数的文件被忽略缓存。默认值为 "",即,所有 PHP 文件都将被缓存。

eaccelerator.shm_max="0"
当使用 " eaccelerator_put() " 函数时禁止其向共享内存中存储过大的文件。该参数指定允许存储的最大值,单位:字节 (10240, 10K, 1M)。"0" 为不限制。默认值为 "0"。

eaccelerator.shm_ttl="3600"
当 eAccelerator 获取新脚本的共享内存大小失败时,它将从共享内存中删除所有在最后 "shm_ttl" 秒内没有存取的脚本缓存。默认值为"0",为不从共享内存中删除任何缓存文件。

eaccelerator.shm_prune_period="3600"
当 eAccelerator 获取新脚本的共享内存大小失败时,他将试图从共享内存中删除早于"shm_prune_period" 秒的缓存脚本。默认值为 "0",为不从共享内存中删除任何缓存文件。

eaccelerator.shm_only="0"
允许或禁止将已编译脚本缓存在磁盘上。该选项对 session 数据和内容缓存无效。默认值为 "0",为使用磁盘和共享内存进行缓存。

eaccelerator.compress="1"
允许或禁止压缩内容缓存。默认值为 "1",为允许压缩。

eaccelerator.compress_level="9"
指定内容缓存的压缩等级。默认值为 "9",为最高等级。

eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"
设置内容缓存的存放的地方,可以设置为:
shm_and_disk 在共享缓存和硬盘(默认值)
shm      默认存在共享内存,如果共享内存已满或大小超过 "eaccelerator.shm_max" 的值,就存到硬盘
shm_only    只存放在共享内存
disk_only    只存放在硬盘
none      不缓存数据

 

6.测试性能

快速排序代码

       <?php function quickSort($arr)
       {
           $len    = count($arr);
           if($len <= 1) {
               return $arr;
           }
           $key = $arr[0];
           $left_arr    = array();
           $right_arr    = array();

           for($i=1; $i<$len; $i++){
               if($arr[$i] <= $key){
                   $left_arr[] = $arr[$i];
               } else {
                   $right_arr[] = $arr[$i];
               }
           }

           $left_arr    = quickSort($left_arr);
           $right_arr    = quickSort($right_arr);
           return array_merge($left_arr, array($key), $right_arr);
       }


       $arr = array(6,3,8,5,9,2,10);
       echo '<pre class="brush:php;toolbar:false">';
       print_r(quickSort($arr));
       ?>
Copy after login


 

测试方法

#ab -n 1000 http://192.168.2.107/quicksort.php

连续测试几次,主要的性能指标是Requests per second

 

左边是没有使用eAccelerator 时的情况,右边是使用了的

PHP安装eAccelerator

连续测试10次,基本数据还是比较稳定的。

使用了eAccelerator 时比没使用的要好!

使用strace查看C语言级别的php源码       

可以看eAccelerator缓存是怎么被读取的完成过程

 

扩展阅读:

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)

有关上述文档详细说明请参考官方文档

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

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_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)
   例如:
  
 
eaccelerator_cache_result($key, $eval_code, $ttl=0)
   将 $eval_code 代码的执行结果缓存 $ttl 秒,($ttl参数同 eacclerator_put),类似 cache_output
   例如:
  
 
eaccelerator_cache_page($key, $ttl=0)
   将当前整页缓存 $ttl 秒。
   例如:
        eaccelerator_cache_page($_SERVER['PHP_SELF'].’?GET=’.serialize($_GET),30);
     echo time();
     phpinfo();
   ?>
 
eaccelerator_rm_page($key)
   删除由  eaccelerator_cache_page() 执行的缓存,参数也是 $key

2、PHP代码中使用eAccelerator加速

另外,在PHPCMS里面已经集成了对eAccelerator的支持,下面是一段来自PHPCMS里面的代码

class cache
{
    function __construct()
    {
    }

    function cache()
    {
		$this->__construct();
    }

    function get($name)
    {
        return eaccelerator_get($name);
    }

    function set($name, $value, $ttl = 0)
    {
        eaccelerator_lock($name);
        return eaccelerator_put($name, $value, $ttl);
    }

    function rm($name)
    {
        return eaccelerator_rm($name);
    }

    function clear()
    {
        return eaccelerator_gc();
    }
}
Copy after login

 

 

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles