首先配置yum源
追加CentOS 6.5的epel及remi源
# rpm -Uvh http:
# rpm -Uvh http:
检查已安装的php包
# yum list --enablerepo=remi --enablerepo=remi-php56 | grep php
安装php5.6
# yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit
php-pecl-xdebug php-pecl-xhprof
查看php版本
# php --version 或 php -v
安装php-fpm
# yum install --enablerepo=remi --enablerepo=remi-php56 php-fpm
以上只是准备工作!!!
# wget http://pecl.php.net/get/xhprof-0.9.4.tgz
# tar zxvf xhprof-0.9.4.tgz
# cd xhprof-0.9.4
# whereis phpize
# /usr/bin/phpize #上一步得到的命令所在位置
如果报以下错误
Cannot find config.m4.
Make sure that you run '/usr/bin/phpize'in the top level source directory of the module
则
在当前目录下进入extension目录下:
在文件中添加一句:
extension = xhprof.so
重启php-fpm生效
# service php-fpm restart
安装xhprof扩展已经完成
如何使用xhprof
将文件拷贝到网站目录里面,然后添加监控代码到统计文件中,这一步是引入文件
# mkdir -p /
home/www/xhprof && cd /home/www/xhprof/
之前是在tmp目录下下载xhprof-0.9.4包
# cp -r /tmp/xhprof-0.9.4/xhprof_html/ ./
# cp -r /tmp/xhprof-0.9.4/examples/ ./
# cp -r /tmp/xhprof-0.9.4/xhprof_lib/ ./# mkdir logs
项目中的使用方法(打点):
1.开始标志
xhprof_enable();
2.被包含的函数体,也是项目中需要检测的函数
function();
3.结束标志
$data = xhprof_disable();
$xhprof_root = "/home/wwwroot/xhprof";
include_once $xhprof_root."/xhprof_lib/utils/xhprof_lib.php";
include_once $xhprof_root."/xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($data, "test");
访问路径:
xxx.
xxx.xxx.xxx 代表服务器主机
http://xxx.
xxx.xxx.xxx/xhprof/xhprof_html/index.php?run=$run_id&source=test
个人觉得打点到访问这个过程可以封装起来,方便使用
其中[View Full Callgraph]按钮可以查看耗时情况分布图,如果点击出现以下报错:
Error: either we can not find profile data for run_id 58b393370e400 or
the threshold 0.01 is too small or you do not have 'dot' image generation utility installed.
错误︰ 我们不能为 run_id 58b393370e400 找到配置文件数据或 0.01 阈值是太小或您没有安装的 '点' 图像生成实用程序。
xhprof绘制的是png图,绘图的dot拓展没装成功,也就是dot 不支持PNG
# yum install -y graphviz 即可
wget http://pecl.php.net/get/xhprof-0.9.4.tgz tar zxvf xhprof-0.9.4.tgz cd xhprof-0.9.4 whereis phpize /usr/bin/phpize