PHP的學習--在sublime中使用XDebug(Ubuntu)

提及來慚愧,自從開始使用Sublime Text以後,再也沒有debug過PHP的代碼,最近把debug的環境搭建了一下,在這裏記錄一下。php

安裝XDebughtml

sudo apt-get install php5-xdebug

編輯xdebug.ini文件,添加以下配置nginx

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"

其中參數表明的含義以下:git

[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和phpweb

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。其操做以下:chrome

要調試某一個項目,首先得把這個項目在sublime下保存成一個project。express

sublime->project->save project as ...

接下來配置項目ubuntu

sublime->project->edit poject

配置文件相似如下內容:sublime-text

{
    "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/"
}

 

參考

Debug PHP with Sublime Text 2 and Xdebug on Ubuntu

用Xdebug和Sublime調試PHP代碼

Sublime配置XDebug

相關文章
相關標籤/搜索