說起來慚愧,自從開始使用Sublime Text之后,再也沒有debug過PHP的代碼,最近把debug的環境搭建了一下,在這里記錄一下。
安裝XDebug
sudo apt-get install php5-xdebug
編輯xdebug.ini文件,添加如下配置
xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_host=127.0.0.1 xdebug.remote_port=9000 xdebug.remote_log="/var/log/xdebug/xdebug.log"
其中參數代表的含義如下:
[Xdebug] ;load xdebug extension zend_extension_ts = path/tp/xdebug ;是否開啟自動跟蹤 xdebug.auto_trace = On ;是否開啟異常跟蹤 xdebug.show_exception_trace = On ;是否開啟遠程調試自動啟動 xdebug.remote_autostart = Off ;是否開啟遠程調試 xdebug.remote_enable = On ;允許調試的客戶端IP xdebug.remote_host=127.0.0.1 ;遠程調試的端口(默認9000) ;xdebug.remote_port=9000 ;調試插件dbgp xdebug.remote_handler=dbgp ;是否收集變量 xdebug.collect_vars = On ;是否收集返回值 xdebug.collect_return = On ;是否收集參數 xdebug.collect_params = On ;跟蹤輸出路徑 xdebug.trace_output_dir="path/to/xdebug/trace" ;是否開啟調試內容 xdebug.profiler_enable=On ;調試輸出路徑 xdebug.profiler_output_dir="path/to/xdebug/profiler"
重啟nginx和php
sudo /etc/init.d/nginx restart sudo /etc/init.d/php-fpm restart
然后在Sublime Text使用package control安裝xdebug client,
用ctrl+shift+p調出搜索框,輸入Package Control: 選中其中的Package Control: Install Package,輸入Xdebug client,找到xdebug client,安裝,安裝完成后要重啟Sublime。其操作如下:
要調試某一個項目,首先得把這個項目在sublime下保存成一個project。
sublime->project->save project as ...
接下來配置項目
sublime->project->edit poject
配置文件類似以下內容:
{ "folders": [ { "follow_symlinks": true, "path": "." } ], "settings": { "xdebug": { "url": "http://my.local.website/", } } }
再在chrome中安裝Chrome Xdebug Helper擴展。在下載和安裝Chrome擴展后,你必須重新啟動瀏覽器。重新啟動后,你將看到在Chrome的地址欄的新圖標:
點擊它,將啟用/禁用調試。但是,我們首先需要調整擴展中使用 Sublime Text 的會話密鑰。
在Chrome中Tools > Extensions
打開Xdebug helper options:
開啟調試方式也比較簡單,在想要加斷點的地方右鍵
xdebug->Add/Remove breakpoint
這樣項目在運行到本行的時候就會停止下來
然后開始調試,在菜單欄選擇
tools->xdebug->start debugging(launch browser)
sublime會自動打開瀏覽器,進入配置時寫的網站鏈接,進行調試。
調試中所用的功能可以在調試文件中右鍵查看之。
快捷鍵說明如下
Start/Stop debugging session
- Start Debugging - Ctrl+Shift+F9 or ⌘+Shift+F9
- Start Debugging (Launch Browser)
- Restart Session
- Stop Debugging - Ctrl+Shift+F10 or ⌘+Shift+F10
- Stop Debugging (Launch Browser)
- Stop Debugging (Close Windows)
Breakpoints
- Add/Remove Breakpoint - Ctrl+F8 or ⌘+F8
- Set Conditional Breakpoint - Shift+F8
- Clear Breakpoints
- Clear All Breakpoints
Watch expressions
- Set Watch Expression
- Edit Watch Expression
- Remove Watch Expression
- Clear Watch Expressions
Session commands
- Evaluate
- Execute
- Status
Continuation commands
- Run - Ctrl+Shift+F5 or ⌘+Shift+F5
- Run To Line
- Step Over - Ctrl+Shift+F6 or ⌘+Shift+F6
- Step Into - Ctrl+Shift+F7 or ⌘+Shift+F7
- Step Out - Ctrl+Shift+F8 or ⌘+Shift+F8
- Stop
- Detach
Other
- Restore Layout / Close Windows - Ctrl+Shift+F11 or ⌘+Shift+F11
- Settings - Default
- Settings - User
問題無法跟蹤斷點
這可能是xdebug端口被占用,按Ctrl+`或者菜單欄View->show Console查看錯誤信息,有可能是xdebug端口已經被占用的緣故。
在sublime xdebug中關閉調試,或者重啟sublime可以解決這個問題,如果還不行,可以修改端口號,如xdebug.ini中的端口號修改為為1000,在perferences->package settings->xdebug->setting-user文件中加入如下內容:
{ "port": 10000 }
弄好這個還是費了些時間~~
php環境在server上,在本地調試
可以再在perferences->package settings->xdebug->setting-user文件中加入如下內容:
"path_mapping": { "/absolute/path/to/file/on/server" : "/path/to/file/on/computer", "/var/www/htdocs/example/" : "C:/git/websites/example/" }