2017年4月22日 星期六

Windows - CentOS 遠端XDebug調試PHP

服務器CentOS
本機Window 7 + XAMPP

0. 在CentOS上安裝XDebug

0.1 查nginx 跑的php安裝在哪

因網頁phpinfo()顯示PHP版本是5.5.36、但php --version卻顯示是5.3.3
$ ps aux | grep php-fpm
root     14028  0.0  0.1 179816  9556 ?        Ss   20:30   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)

0.2 安裝Xdebug

# yum install php-devel  #需先安裝php-devel、gcc、gcc-c++、autoconf、automake
# /usr/local/php/bin/pecl install Xdebug
勿直接 pecl install Xdebug,因為which pecl的位置在/usr/bin/pecl,非nginx跑的php

0.3 檢查是否安裝成功

重啟php-fpm
# service php-fpm restart
打開phpinfo(),發現安裝成功

1. 配置php.ini

#vim /usr/local/php/etc/php.ini
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "192.168.1.173" ;你Windows本機的IP
xdebug.remote_port = 9000
xdebug.remote_log="/tmp/xdebug_log"

remote_host  => Xdebug是服務器去連你本機client開的服務

2. 打開Windows的port

讓CentOS主機能連到Windows上XDebug的服務
開始 => 控制台 => 搜尋並點擊"Windows 防火牆" => 高級設置 => 入站規則 => 新建規則 => 端口 => (TCP) 特定端口:9000 => 允許連接 => (規則全勾)下一步 =>名稱:xdebug-client
如果不做這步CentOS上/tmp/xdebug_log 會一直報錯
Log opened at 2017-04-22 11:05:08
I: Connecting to configured address/port: 127.0.0.1:9500.
W: Creating socket for '127.0.0.1:9500', poll success, but error: Operation now in progress (25).
E: Could not connect to client. :-(
Log closed at 2017-04-22 11:05:08
在/tmp/下會產生大量的 cachegrind.out.xxxxx.xxxxxx 最後把你的硬碟吃完,讓Server CPU跑滿,所以要非常注意

3. 檢查Windows上端口是否有通

3.1 在Windows的Sublime 先打開XDebug服務

Sublime -> Tools -> Xdebug -> Start Debugging

3.2 檢查Sublime Xdebug開在windows上哪個port

打開XAMPP Control Panel => Netstat => 看有沒有9000的port被plugin_host.exe執行

3.3  在CentOS上執行

# telnet 192.168.1.173 9000
Trying 192.168.1.173...
Connected to 192.168.1.173.
Escape character is '^]'.
出現這個代表這個port有通

完成1.2.3後在/tmp/xdebug_log 中看到 下列類似XML的訊息,就代表快成功了
Log opened at 2017-04-22 13:04:58
<- feature_set -i 1 -v 1 -n show_hidden
-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="1" feature="show_hidden" success="1"></response>

4. 設定Sublime

與只在本機上使用XDebug不同的是,遠端使用必須加入path_mapping設定
Sublime -> Project -> Edit Project 打開這個專案的.sublime-project檔,並加入以下設定
{
"folders":
[
{
"path": "D:\\bear\\www\\admin168"
}
],
"settings": {
"xdebug": {
"url": "http://bear.admin.web.com/",
"path_mapping": {
"/var/wwwroot/bearadmin/web/" : "D:\\bear\\www\\admin168",
    "/www/wwwroot/codes/": D:\\bear\\www\\admin168\\codes"  // 可配置多個目錄(如果專案code不在同一個目錄下)
},
}
}
}
"/var/wwwroot/bearadmin/web/" : "D:\\bear\\www\\admin168" => "遠端專案位置":"本機專案位置"
本機專案位置為FTP地址也OK
"/var/www/bear/www/" : "\\\\192.168.1.xx\\linuxsir\\bear\\www"

https://github.com/martomo/SublimeTextXdebug
path_mapping
For remote debugging to resolve the file locations it is required to configure the path mapping with the server path as key and local path as value.
在遠端debug時,解決檔案路徑是必要的,使用path_mapping設定服務器路徑和本地路徑


How a debugging session works?
debugging session如何工作的?
The client software (Sublime with the Xdebug package in your case) starts listening on port 9000 of localhost, waiting for the server to start the connection. It probably doesn't listen on the port all the time but only when the developer tells it so.
Windows本機上的Xdebug client開始聽9000的port,等待服務器PHP Xdebug的連線。

多個開發者同時使用
1. php.ini 註解掉 xdebug.remote_host
2. 新配置xdebug.remote_connect_back = 1,當xdebug.remote_connect_back開啟時,xdebug.remote_host不作用。
3. 註解掉 xdebug.remote_autostart=1 ,避免/tmp/xdebug_log往死裡跑
4. (可略)因為我主機ip換過,所以在xdebug_log中會顯示
Log opened at 2018-03-13 10:51:18
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 192.168.1.125:9000.
E: Time-out connecting to client. :-(
Log closed at 2018-03-13 10:51:19
此時我ip的9000 port也通不了($ telnet 192.168.1.125 9000)
這時候要在Windows系統防火牆重新建立入站規則,Sublime也要關掉重新開你電腦上的Xdebug服務
5. 在CLI模式下斷點
$ export XDEBUG_CONFIG="remote_host=192.168.1.125 idekey=sublime.xdebug"

Xdebug v2.6.1 (PHP 7.1.7) Xdebug Context 和Xdebug Breakpoint空白
https://stackoverflow.com/questions/17070818/xdebug-not-showing-stack-and-trace-in-sublime-text-2-on-window
https://github.com/martomo/SublimeTextXdebug/issues/172
解法:
Sublime => Prefeerences => Package Settings => Xdebug => Settings - User ( Data/Packages/User/Xdebug.sublime-settings )
加入以下配置
"super_globals": false
"max_depth": 1  => 必須改回1
以上是用 Package Control 安裝的 Xdebug Client才需要這麼做
完美解法:
https://github.com/martomo/SublimeTextXdebug/issues/172#issuecomment-454400985
1. 移除 Package Control 安裝的 Xdebug Client
2. Preferences => Browse Packages.. =>  手動把XDebug Client安裝到這個目錄下(注意:必須使用meengit的SublimeTextXdebug )
$ git clone https://github.com/meengit/SublimeTextXdebug "Xdebug Client"
2.5 刪除 .git 和 .github 資料夾避免啟動時報錯
3. 重啟Sublime

參考資料:
https://github.com/martomo/SublimeTextXdebug  SublimeTextXdebug
https://wiki.mcneel.com/zh-tw/zoo/window7firewall  從Windows防火牆開啟TCP Port 80
https://gist.github.com/kramarama/9695033  install xdebug on centos
http://stackoverflow.com/questions/39027686/xdebug-unable-to-connect-to-client-where-do-i-start-debugging-the-debugger  Xdebug unable to connect to client, where do I start debugging the debugger?
https://xdebug.org/docs/all_settings#remote_connect_back  xdebug.remote_connect_back