XhProf是Facebook出品的一个PHP性能监控工具,只包含基本的界面和图形来分析数据。后来Paul Reinheimer在此基础上开发了Xhgui,提供了更好的界面和功能,其主页在https://github.com/perftools/xhgui,其实步骤说的很详细,但是在CentOS7上安装并不容易,因为很多程序需要编译,在这里记录一下
1、CentOS7最小安装, yum update
2、安装gcc, yum install net-tools gcc gcc-c++ wget vim
3、安装php, yum install php php-devel php-fpm git
4、下载nginx,
mkdir ~/download
cd ~/download
tar xvf nginx-1.99.tar.gz
cd nginx-1.99
yum install pcre-devel zlib-devel (否则configure会出现错误)
./configure
make
make install
nginx安装在/usr/local/nginx下
5、取消selinux,centos7确实很稳定安全,但是很麻烦,取消之后,避免出现一些奇怪的问题,
vim /etc/selinux/config
修改SELINUX=disabled
上面的方法需要重启,并且是长期有效的,临时性的方法是setenforce 0
6、创建nginx的80端口根目录,mkdir -p /data/html
7、修改nginx配置文件,vim /usr/local/nginx/conf/nginx.conf
修改http->server 80下的内容,如下
server {
listen 80;
server_name localhost;
location / {
root /data/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/html;
}
location ~ \.php$ {
root /data/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
需要修改的地方,黑体字做了标注,location ~ \.php$需要取消注释
8、ln /usr/local/nginx/sbin/nginx /usr/sbin/nginx
9、systemctl auto php-fpm
systemctl start php-fpm
10、vim /data/html/info.php
<?php
phpinfo();
curl localhost/info.php
说明php配置成功,需要注意,如果没有取消selinux,这里会出现File not found,如果一定要用selinux,需要赋予/data/html的www运行权限。
11、配置xhgui的步骤其实很简单,按照文档来就可以了
12、XHProf
cd ~/download
tar xvf xhprof-0.9.4.tgz
cd xhprof-0.9.4/extension
phpize
./configure
make
make install
vim /etc/php.ini
在最后加入
extension=xhprof.so
通过php -m可以看到xhprof.so已经安装成功
12、UpProfiler
cd ~/download
cd uprofiler/extension
phpize
./configure
make
make install
vim /etc/php.ini
最后加入
extension=uprofiler.so
用php -m验证
13、tideways
cd ~/download
cd php-profiler-extension/
phpize
./configure
make
make install
vim /etc/php.ini
最后加入
extension=tideways.so
用php -m验证结果
14、 MongoDB的PHP驱动
cd ~/download
tar xvf mongo-1.6.12.tgz
cd mongo-1.6.12
phpize
yum install openssl-devel
./configure
make && make install
vim /etc/php.ini
最后增加
extension=mongo.so
用php -m验证
15、安装MongoDB
vim /etc/yum.repose.d/mongodb-org-3.2.repo
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1
安装
yum install mongodb-org
systemctl enable mongod
systemctl start mongod
16、mcrypt
cd ~/download
wget http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
tar xvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
make install
cd ~/download
wget http://museum.php.net/php5/php-5.4.16.tar.gz
cd php-5.4.16/ext/mcrypt/
phpize
./configure
make
make install
vim /etc/php.ini
最后增加
extension=mcrypt.so
用php -m验证
17、Dom
cd ~/download/php-5.4.16/ext/dom/
yum install libxml2-devel
phpize
./configure
make
make install
vim /etc/php.ini
最后增加
extension=dom.so
用php -m验证
19、配置nginx,将xhgui放在88端口
vim /usr/local/nginx/conf/nginx.conf
增加
server{
listen 88;
server_name localhost;
root /data/xhgui/webroot;
index index.php;
location / {
try_files $uri $uri/ /index.php?$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
cd /data
git clone
https://github.com/perftools/xhgui.git
打开防火墙端口
firewall-cmd --add-service=http
firewall-cmd --permanent --add-service=http
firewall-cmd --add-port=88/tcp
firewall-cmd --permanent --add-port=88/tcp
19、对80端口注入xhgui文件
location ~ \.php$ {
root /data/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/data/xhgui/external/header.php";
}
nginx -s reload
20、初始化
cd /data/xhgui
php install.php
这一步包含下载composer,Slim,以及初始化mongodb
21、 验证
curl localhost/info.php
curl localhost:88
在这里88端口没有输出,是因为默认/etc/php.ini中设置了display_errors=Off,修改为On
重启php-fpm, systemctl restart php-fpm
可以看到错误,是没有设置时区,修改/etc/php.ini
修改date.timezone = Asia/Chongqing
重启php-fpm, systemctl restart php-fpm
curl localhost:88
如果配置正确的话,这里应该是正常的,没有错误
也可以在浏览器中访问,这台机器的ip为172.16.9.145,所以可以访问
22、测试程序
vim /data/html/test.php
<?php
$n=5000;
$a = createArray($n);
$a = sortArray($a);
echoArray($a);
function createArray($n){
$a;
for($i=0;$i<$n;$i++){
$a[$i]=rand(0,$n);
}
return $a;
}
function echoArray($a){
for($i=0;$i<count($a);$i++){
echo $a[$i]." ";
}
}
function sortArray($a){
for($i=0;$i<count($a);$i++){
for($j=$i+1;$j<count($a);$j++){
if($a[$i]<$a[$j]){
$tmp = $a[$i];
$a[$i]=$a[$j];
$a[$j]=$tmp;
}
}
}
return $a;}其实很简单,就是一个创建数组,进行排序的过程,可以在浏览器中运行http://172.16.9.145/test.php,理论上就可以在http://172.16.9.145:88看到结果了,但是,刷新88端口,一般是看不到结果的,这里是一个采样问题,xhgui是按照1%采样的,所以对于开发来说,需要100%采样,需要修改vim /data/xhgui/config/config.php'profiler.enable' => function() {//return rand(0,100) == 42return rand(0, 100) > 0 ;},黑体子是修改内容,这就是100%的采样,再运行http://172.16.9.145/test.php,就可以在http://172.16.9.145:88上看到结果了,23、其他可以看到采样的结果,包括程序的等待时间(wt)、cpu时间、内存等等信息,还可以通过view Graphy看到函数调用的过程,颜色越深,说明运行越慢,需要处理。xhgui使用mongodb保存性能数据,数据库连接信息保存在xhgui/config/config.php中,这意味着可以将性能信息保存到远程机器,还可以将多台机器的性能数据放在一起,我们可以看一下性能数据的结构,如下mongo>use xhprof>db.results.find().limit(1).pretty();查看一下键meta的信息,如下"meta" : {"url" : "/test.php","SERVER" : {"USER" : "apache","HOME" : "/usr/share/httpd","FCGI_ROLE" : "RESPONDER","SCRIPT_FILENAME" : "/data/html/test.php","QUERY_STRING" : "","REQUEST_METHOD" : "GET","CONTENT_TYPE" : "","CONTENT_LENGTH" : "","SCRIPT_NAME" : "/test.php","REQUEST_URI" : "/test.php","DOCUMENT_URI" : "/test.php","DOCUMENT_ROOT" : "/data/html","SERVER_PROTOCOL" : "HTTP/1.1","REQUEST_SCHEME" : "http","GATEWAY_INTERFACE" : "CGI/1.1","SERVER_SOFTWARE" : "nginx/1.9.9","REMOTE_ADDR" : "172.16.9.255","REMOTE_PORT" : "62433","SERVER_ADDR" : "172.16.9.145","SERVER_PORT" : "80","SERVER_NAME" : "localhost","REDIRECT_STATUS" : "200","PHP_VALUE" : "auto_prepend_file=/data/xhgui/external/header.php","HTTP_HOST" : "172.16.9.145","HTTP_CONNECTION" : "keep-alive","HTTP_CACHE_CONTROL" : "max-age=0","HTTP_ACCEPT" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","HTTP_UPGRADE_INSECURE_REQUESTS" : "1","HTTP_USER_AGENT" : "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36","HTTP_ACCEPT_ENCODING" : "gzip, deflate, sdch","HTTP_ACCEPT_LANGUAGE" : "zh-CN,zh;q=0.8","HTTP_COOKIE" : "slim_session=1450653227%7CBxhI8%2BKm%2B47xsnTB%2BOZVAv9qjPHQYC1oCIcfWhIblGI%3D%7C3a5b1cf4f47a77302d253b577949544f4ffe627b","PHP_SELF" : "/test.php","REQUEST_TIME_FLOAT" : 1450652599.176433,"REQUEST_TIME" : NumberLong(1450652599)},可以通过SERVER_*, HTTP_*看到服务器信息,可以用来区分不同的主机,所以可以通过这些信息来过滤,可以支持多个主机