XhProf安装教程–详细教程

  • wget
    去这里找最新的包下载,我现在最新的是xhprof-0.9.4.tgz

     

    cd /tmp
    mkdir xhprof && cd xhprof
    wget http://pecl.php.net/get/xhprof-0.9.4.tgz
    tar -xvf xhprof-0.9.4.tgz
    cd xhprof-0.9.4/extension/
    phpize
    

    我在进行这步的时候,提示我没有安装phpize,下一步就是安装phpize,如果已经安装了phpize,可以跳过下一步

  • phpize
    因为phpize在php-devel包里面,所以先需要安装php-devel

     

    yum search php-devel
    

    如果你和我一样,找不到这个包的话,建议添加电子科技大学的源

    cd /tmp
    wget http://mirrors.stuhome.net/files/centos_repo/6/CentOS-Base.repo
    cp CentOS-Base.repo /etc/yum.repos.d/CentOS-Base-Uestc.repo -v
    yum makecache
    yum install php-devel -y
    

    装好了phpize就可以进行安装了

    phpize
    
  • 接下来是源码安装三部曲:
    whereis php-config
    /*看看你的php-config在什么路径,然后放到后面的path-to php-config,一般是/usr/bin/php-config */
    ./configure --with-php-config=<path to php-config >
    make
    make install
    mkdir -p /var/tmp/xhprof
    
  • 编译添加了xhprof.so文件,现在需要配置php启用这个so文件,去/etc/php.d/文件夹下面添加文件xhprof.ini
    extension=xhprof.so
    xhprof.outout_dir=/var/tmp/xhprof
    /*请确定上面配置的/var/tmp/xhprof目录是存在的*/
    

    然后就是重新启动你的php,之后用命令查看

    php -ini | grep xhprof -i
    

    看看是否已经安装上了,安装好了之后,就可以开始设置监控了

  • 将文件拷贝到网站目录里面,然后添加监控代码到统计文件中
    mkdir -p /var/www/xhprof && cd /var/www/xhprof/
    cp -r /tmp/xhprof/xhprof-0.9.4/xhprof_html/ ./
    cp -r /tmp/xhprof/xhprof-0.9.4/examples/ ./
    cp -r /tmp/xhprof/xhprof-0.9.4/xhprof_lib/ ./
    mkdir logs
    

    配置nginx的虚拟主机目录,在/etc/nginx/conf.d/目录下面新建xhprof.conf
    然后修改文件内容为:

    server{
      listen 80;
      server_name effect.vpsbuy.info;
      root /var/www/xhprof;
      access_log /var/www/xhprof/logs/access.log;
      error_log /var/www/xhprof/logs/error.log;
      index index.php;
    
      location = favicon.ico {
          log_not_found off;
          access_log off;
      }    
    
      location = /robots.txt{
          allow all;
          log_not_found off;
          access_log off;
      }
      location / {
          root /var/www/xhprof;
      }
      location ~ \.php$ {
          include fastcgi_params;
          fastcgi_pass php;
          fastcgi_index xhprof_html/index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
    }
    

    重新启动nginx

    nginx -t
    nginx -s reload
    

    修改examples目录下面的sample.php文件,将最后一行的echo改成这样

    echo "---------------\n".
       "Assuming you have set up the http based UI for \n".
       "XHProf at some address, you can view run at \n".
       "http://effect.vpsbuy.info/xhprof_html/index.php?run=$run_id&source=xhprof_foo\n".
       "---------------\n";
    

    将这个http://effect.vpsbuy.info/xhprof_html/改成你自己的url路径。
    然后就是配置监控

    $xhprof_enable = false;
    if(mt_rand(1,10)==1){ //这里设置监控的比例
      xhprof_enable();
      $xhprof_enable = true;
    }
    //这里写上你要监控的函数
    if($xhprof_enable ){
      $xhprof_data = xhprof_disable();
      //$XHPROF_ROOT = realpath(dirname(__FILE__) .'/..');           
      $XHPROF_ROOT = /var/www/xhprof;
      include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
      include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
      // save raw data for this profiler run using default        
      // implementation of iXHProfRuns.
      $xhprof_runs = new XHProfRuns_Default();
      // save the run under a namespace "xhprof_foo" 
      $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
    }
    

    现在就可以统计了,然后就是安装生成访问图像的

    yum install graphviz

    如果你觉得写得不错,请捐赠作者:

    作者:

     

posted @ 2013-11-01 19:47  I'm Hurley  阅读(2793)  评论(0编辑  收藏  举报