---
title: "【汇总】PHP扩展【xhprof，eaccelerator】"
author: "crizy"
date: "2011-10-09T13:26:11Z"
tags: ["eaccelerator", "xhprof"]
categories: ["开源软件"]
slug: "php-xhprof-eaccelerator"
draft: false
lastmod: "2011-10-09T13:51:50Z"
id: 1186630
url: "https://crizy.iteye.com/blog/1186630"
---

### 环境

1.PHP 5.3.8，使用fastcgi方式安装，安装目录/usr/local/php

2.CentOS 5.5

 

### xhprof

生成xhprof.so

```
#unzip -o xhprof-0.9.2.zip
#cd xhprof-0.9.2
#cd xhprof-0.9.2/xhprof-0.9.2/extension/
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make
#make install
```

将生成的xhprof.so放入php的extension\_dir下，配置php.ini后，重启php

```
// 放入php的extension_dir目录下
...

// 配置php.ini
#vi /usr/local/php/lib/php.ini
---
[xhprof]
extension="xhprof.so";
xhprof.output_dir="/var/cache/xhprof"
---

// 重启php
#pkill -o php-fpm
#/usr/local/php/sbin/php-fpm

// 查看phpinfo，检查xhprof是否安装成功
... 
```

将xhprof\_lib，xhprof\_html移动到网站目录下

...

写php代码，使用xhprof模块

```
<?php

// 开启xhprof
if(function_exists('xhprof_enable')) { 
    xhprof_enable(); 
}

// 你的php代码

// 生成报告
if(function_exists('xhprof_disable')) {
    $xhprof_data = xhprof_disable();
    include_once "xhprof_lib/utils/xhprof_lib.php";
    include_once "xhprof_lib/utils/xhprof_runs.php";

    $xhprof_runs = new XHProfRuns_Default();
    $identifier = rand(0,9999);
    $run_id = $xhprof_runs->save_run($xhprof_data, $identifier);

    echo "<a target=\"_blank\" href=\"xhprof_html/index.php?run={$run_id}&source={$identifier}\">view xhprof </a>";
}
```

 

### eaccelerator

生成eaccelerator.so

```
#tar -zxvf eaccelerator-0.9.6.1.zip
#cd eaccelerator-0.9.6.1
#/usr/local/php/bin/phpize
#./configure --with-php-config=/usr/local/php/bin/php-config
#make
#make install
```

 

将生成的eaccelerator.so放入php的extension\_dir下，配置php.ini后，重启php

```
// 放入php的extension_dir目录下  
...  
  
// 配置php.ini  
#vi /usr/local/php/lib/php.ini  
---  
[eaccelerator]
extension="eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"
---  
  
// 重启php  
#pkill -o php-fpm
#/usr/local/php/sbin/php-fpm
  
// 查看phpinfo，检查eaccelerator是否安装成功
...
```

 

eaccelerator详细配置说明

【**此为转载，特此说明，转载地址：[http://www.21andy.com/blog/20100207/1646.html](http://www.21andy.com/blog/20100207/1646.html)** 】

\---

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

**eaccelerator.cache\_dir="/home/php/tmp"**  
这个目录是给磁盘缓存使用. eAccelerator 在这里储存预先编译好的代码, 进程数据, 内容以及用户的自定义内容. 同样的数据也能被储存在共享内存中 (这样可以提高访问速度). 默认的设置是 "/tmp/eaccelerator".

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

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

**eaccelerator.check\_mtime="1"**  
打开或者关闭 PHP 的文件修改检查. "1" 是指打开, "0" 是指关闭. 如果您在修改以后重新编译 PHP 的文件,那么您应当设置为 "1". 默认值是 "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="0"**  
当 eAccelerator 获取新脚本的共享内存大小失败时，它将从共享内存中删除所有在最后 "shm\_ttl" 秒内没有存取的脚本缓存。默认值为 "0"，即：不从共享内春中删除任何缓存文件。

**eaccelerator.shm\_prune\_period="0"**  
当 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.session = "disk\_only"**  
**eaccelerator.content = "disk\_only"**  
设置内容缓存的存放的地方，可以设置为：  
**shm\_and\_disk** 在共享缓存和硬盘(默认值)  
**shm** 默认存在共享内存，如果共享内存已满或大小超过 "eaccelerator.shm\_max" 的值，就存到硬盘  
**shm\_only** 只存放在共享内存  
**disk\_only** 只存放在硬盘  
**none** 不缓存数据

**eaccelerator.allowed\_admin\_path = "/var/www/html/21andy.com/eaccelerator"**  
这是控制面板的地址  
安装包里有个control.php，你把它复制到网站的任意目录，可以用它查看和管理，这个必须指定，否则查看缓存内容的时候会出错

\---