PHP - xhprof 安装使用
Max.Bai
2016-09-02
0. Prepare
Get below php releate config value, like:/usr/local/webserver/php-5.6.12/etc/php.ini
/usr/local/webserver/php-5.6.12/bin/php-config
/usr/local/webserver/php-5.6.12/bin/phpize
Create xhprof outdir for result, default in /tmp
/data/xhprof/result
1. download xhprof
mkdir /opt/xhprof & cd /opt/xhprof
wget xxxxxxx/xhprof-0.9.4.tar.gz
tar -xzf xhprof-0.9.4.tar.gz
cd /opt/xhprof/xhprof-0.9.4/extension
2. build xhprof
phpize./configure --with-php-config=/usr/local/webserver/php-5.6.12/bin/php-config
make and make install
after install, will show the extension path like :
/usr/local/webserver/php-5.6.12/lib/php/extensions/no-debug-non-zts-20131226/
!!!this path will be used next step!!!
3. config php.ini
vim /YOUR_PHP_INI/php.iniAdd below config in your php.ini
[xhprof]
extension=xhprof.so
xhprof.output_dir=/data/xhprof/result
extension_dir=/usr/local/webserver/php-5.6.12/lib/php/extensions/no-debug-non-zts-20131226/
4. Restart php
/etc/init.d/php-form restartMaybe this command is not for you, please check your restart command.
Check your xhprof is installed:
php -m | grep xhprof
or use phpinfo check.
like :
xhprof 0.9.4
cpunum 4
5. Copy xhprof to your webserver dir
Copy examples xhprof_html xhprof_lib folder to your webserver dir6. Config Nginx
Make sure your path is correct.
server
{
listen 80;
server_name xhprof.xxxx.com;
root /data/xhprof;
access_log /data/xhprof/logs/access.log;
error_log /data/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 /data/xhprof;
}
location ~ \.php$ {
fastcgi_pass webserver_0;
fastcgi_index xhprof_html/index.php;
include fastcgi.conf;
set $script $uri;
set $path_info "/";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
fastcgi_param PHP_VALUE "session.cookie_domain=xxxx.com";
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root/$script;
fastcgi_param SCRIPT_NAME $script;
access_log off;
}
}
7. Restart Nginx
Restart you rnginx8. Now you can access xhprof page
like :xhprof.xxxx.com/xhprof_html/index.php
9. How to use?
// start xhprof enable
if(extension_loaded('xhprof')){
include_once '/data/xhprof/xhprof_lib/utils/xhprof_lib.php';
include_once '/data/xhprof/xhprof_lib/utils/xhprof_runs.php';
//xhprof_enable();
//xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
//fix for nginx response with 502
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
}
// this place is your program code
if(extension_loaded('xhprof')){
$ns = 'myXhprof';
$xhprofData = xhprof_disable();
$xhprofRuns = new XHProfRuns_Default();
$runId = $xhprofRuns->save_run($xhprofData, $ns);
}
10. View your result
Your all results wil be list on this page.http://xhprof.xxx.com/xhprof_html/index.php