Objective
I used to analyze the xhprof+xhgui
performance of the online environment, but PHP
after the upgrade to the version is PHP 7
xhprof
not available, so instead tideways+xhgui
, this is actually the PHP7
only option for the next open source solution, interested in the resources can be seen, detailed description.
This article is mainly based on the reference [1]
configuration, so there will be a lot of duplication of places, I mainly based on the requirements of the actual production environment to add the following additional configuration:
mongodb
Bind only to Local
xhgui
Turn on HTTP Basic
authentication
xhgui
mongodb
Keep only 14
the last days of data in
System environment
CentOS 7.3 + nginx + mysql + php71
This article assumes that your lnmp
environment is already working, and that it is installed through the source code PHP
, and now you just need to add the functionality of the performance test. If you are unfamiliar with the configuration of the lnmp
environment, we recommend using the one-click installation package provided by https://lnmp.org/, which is based on the default configuration of the package.
Installation and Configuration
Into the following sections:
Mongodb
Tideways
Xhgui
Application Configuration
1.mongodb
Installation
#yum Install mongodb-server mongodb-y#pecl Install MongoDB
Start the mongodb
service
#mongod--bind_ip 127.0.0.1
2.tideways
Installation
git clone https://github.com/tideways/php-profiler-extension.gitcd php-profiler-extensionphpize./configure-- with-php-config= ' which php-config ' Makesudo make install
Configuration
php.ini
to edit a file, add:
Extension=tideways.sotideways.auto_prepend_library=0
Restart php-fpm
, execute the following command to see tideways
the output indicated to be effective:
#php-M | grep tidetideways
3.xhgui
xhgui
Also a website that ultimately needs to be web
accessed through. The official version of the English version, has not been updated, there are many BUG
, it is recommended to use the Chinese version: https://github.com/maxincai/xhgui
.
Install (assuming that the /home/wwwroot/
following commands are executed in the directory)
$ git clone https://github.com/maxincai/xhgui.git$ cd xhgui$ php install.php
Configuration
1. Adding an index to a database is not required, but strongly recommended:
$ MONGO > Use xhprof > Db.results.ensureIndex ({' Meta. SERVER. Request_time ':-1}) > Db.results.ensureIndex ({' Profile.main (). WT ':-1}) > Db.results.ensureIndex ({' Profile . Main (). Mu ':-1}) > Db.results.ensureIndex ({' Profile.main (). CPU ':-1}) > Db.results.ensureIndex ({' Meta.url ': 1})
2. nginx
configuration ( xhgui
There is no security mechanism in itself, it captures sensitive data in the data, so you must turn on HTTP Basic authentication after opening to the outside network)
Create /usr/local/nginx/conf/vhost/xhgui.conf
the file with the following content:
server{ listen 8888; # change to its own port server_name 127.0.0.1 according to the actual situation ; #根据实际情况改成自己的域名 index index.html index.htm index.php; Root /home/wwwroot/xhgui/webroot/; Location ~ \.php { auth_basic "Xhgui needs Authentication"; # Open HTTP Basic authentication auth_basic_user_file htpasswd; # password file try_files $uri =404; Fastcgi_pass Unix:/tmp/php-cgi.sock; Fastcgi_index index.php; Fastcgi_param script_filename $document _root$fastcgi_script_name; Include Fastcgi_params; } Location/{ try_files $uri $uri//index.php? $uri & $args; } Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $ { expires 30d; } Location ~. *\. (JS|CSS)? $ { expires 30d; } Access_log /home/wwwlogs/xhgui.access.log; Error_log /home/wwwlogs/xhgui.error.log;}
Open the HTTP Basic认证
need to generate a password file htpasswd
. Assuming that you generate a tester
user with a password of 123456
, execute the following command:
printf "tester:$ (OpenSSL passwd-crypt 123456) \ n" >>/usr/local/nginx/conf/htpasswd
After the build, remember to check the contents of the file, the format should resemble the following:
$cat/usr/local/nginx/conf/htpasswd Tester:1qe8kan82ioyo
Complete the configuration reboot, enter in the browser http://127.0.0.1:8888
, should be able to see the interface, but there is no data at this time.
3. Further optimization of the configuration
xhgui
The default is to collect by 1% , but if it is to troubleshoot the problem still want to be able to 100% acquisition will be more convenient. Enter the xhgui
source directory, modify the config/config.default.php
file, usually still according to 1% sampling rate sampling, to prevent data growth too fast, when you want to debug, the parameters added in the URL
debug=1
.
In the config/config.default.php
, find profiler.enable
here, modify as follows:
' Profiler.enable ' = function () { //URL contains debug=1 if (!empty ($_get[' Debug ')}) { return true; } else { //1% sample return rand (1, +) = = =; }},
If you do not delete the collected data, you will soon find that the mongo
database becomes very large. Therefore, it is recommended to configure the mongo
database to keep only the last 14 days of data.
#mongo > Use xhprof> db.results.ensureIndex ({"Meta.request_ts": 1}, {expireafterseconds:3600*24*14})
If you want to delete all manually, execute the following command:
$ mongo$ Use xhprof;$ db.dropdatabase ();
4. Application Configuration
To allow the application to implement the acquisition, you need to modify the corresponding nginx
configuration file, add:
Fastcgi_param tideways_samplerate "100"; #是否采样取决于xhgui的随机数配置和这里的采样率配置, both must be satisfied at the same time, here simple set to 100, by Xhgui to control Fastcgi_param php_value "auto_prepend_file=/home/ Wwwroot/xhgui/external/header.php ";
The complete nginx
sample configuration file is as follows:
server{ Listen, #根据实际情况修改 server_name test.dev; #根据实际情况修改 index index.html index.htm index.php; Root /home/wwwroot/test/web/; Location ~ \.php { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index/index.php; Fastcgi_param script_filename $document _root$fastcgi_script_name; Fastcgi_param tideways_samplerate "100"; # Here is the focus fastcgi_param php_value "auto_prepend_file=/home/wwwroot/xhgui/external/header.php"; # Here's the point Include Fastcgi_params; } Try_files $uri $uri/@rewrite; Location @rewrite { rewrite ^/(. *) $/index.php?_url=/$1; } Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $ { expires 30d; } Location ~. *\. (JS|CSS)? $ { expires 30d; } Access_log /home/wwwlogs/test.access.log; Error_log /home/wwwlogs/test.error.log;}
Finally, the interface to successfully configure and capture data