PHP安装eAccelerator
发布日期:2021-06-30 19:32:20 浏览次数:3 分类:技术文章

本文共 6973 字,大约阅读时间需要 23 分钟。

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

 

最新的PHP5.4.8安装eAccelerator,

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

官网:

 

注意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));

php_register_info_logo在PHP5.5中已经移除了

 

1.下载安装eAccelerator

#wget

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

#tar zxvf eaccelerator-42067ac.tar.gz

需要phpize安装php扩展,如果不明白phpize可以看这个:

需要注意的是需要写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要用到

/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()

 

4.查看缓存文件夹

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

这说明成功生成了缓存

 

5.更好的监控方式

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

 

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

eaccelerator.allowed_admin_path=/usr/www/

 

重启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.测试性能

快速排序代码

'; print_r(quickSort($arr)); ?>

 

测试方法

#ab -n 1000

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

 

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

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

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

可以看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) 来释放这个锁或等待程序请求结束时自动释放这个锁。
   例如:
   <?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(‘test’, ‘echo time(); phpinfo();’, 30); ?>
 
eaccelerator_cache_result($key, $eval_code, $ttl=0)
   将 $eval_code 代码的执行结果缓存 $ttl 秒,($ttl参数同 eacclerator_put),类似 cache_output
   例如:
   <?php eaccelerator_cache_result(‘test’, ‘ time() . “Hello”;’, 30); ?>
 
eaccelerator_cache_page($key, $ttl=0)
   将当前整页缓存 $ttl 秒。
   例如:
   <?php
     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();    }}

 

 

转载地址:https://linuxstyle.blog.csdn.net/article/details/8194030 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PHP新的垃圾回收机制:Zend GC详解
下一篇:phpize

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年05月03日 13时39分43秒