phpstorm8連接xdebug來調試php

這是一個深深的坑。網上的教程要麼太老,要麼太多錯誤的地方,試過了很多步驟都沒成功,所以,這個坑我覺決定親自填一下。

1. 下載、安裝和配置xdebug

下載

去官網下載最新的source源碼,我寫這篇文章的時候,最新版是xdebug-2.3.2。

這是下載地址:http://www.xdebug.org/files/xdebug-2.3.2.tgz

安裝

源碼下載完成後,解壓,並安裝到php的擴展當中。

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cd</span> /xdebug
phpize
./configure
make
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> make install</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

配置php.ini

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[xdebug]
zend_extension = xdebug<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.so</span>
xdebug<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.remote</span>_enable = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>
xdebug<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.remote</span>_handler = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"dbgp"</span>
xdebug<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.remote</span>_host = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"localhost"</span>
xdebug<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.remote</span>_mode = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"req"</span>
xdebug<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.remote</span>_port = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9001</span>
xdebug<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.idekey</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"PHPSTORM"</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

重啓下php-fpm

kill -USR2 `cat /usr/local/var/run/php-fpm.pid`

2. 配置PHPstorm

這一步是最坑爹的,一定要小心配置,也有很多坑。分幾步:

配置xdebug

phpstorm -> preferences 打開設置頁面:

然後,再選擇 Languages -> php -> Debug 下,將Xdebug 下的Debug port 改成9001,和php.ini中設置的一致。

OK。xdebug端口配置好了,我們再配置一下Phpstorm下的2種運行模式webcli命令行這2種模式下的項目運行。

debug也只是針對某一個頁面在做的。我們以index.php頁面爲例,先打開index.php,在最右上角,點擊Edit Configurations 打開設置頁面:

第一種是在web網頁端運行的,我們就改名叫web吧,在javascript debug下,填入我們這個項目的運行URL,我這裏直接填的localhost

這樣子,我們點擊運行按鈕,就會用chrome瀏覽器打開http://localhost:8080/learnlaravel5/public/index.php頁面了。

還一種是命令行模式運行。在php script 下,我們就改名叫cliFile下,填入我們當前文件的絕對路徑。

安裝JetBrains IDE Support

好,2種模式都搞好了,一般我們是在web模式下調製,所以,我們先選擇web模式,再將最右邊的電話debug按鈕點擊打開,最後,點擊中間的蟲子按鈕:

會幫我們打開chrome瀏覽器到http://localhost:8080/learnlaravel5/public/index.php頁面,同時,phpstor底部彈層報錯了,提示我們要安裝JetBrains IDE Support擴展:

Waiting for connection from JetBrains IDE Support extension for Chrome (IDE port 63342).

ok, 我們點擊這個連接進去,是chrome 應用市場的下載頁面,估計國內的環境打不開,要開個代理:

安裝完成後,就會有個JB的logo了。

然後,我們再按照剛纔的步驟來一次,瀏覽器就會出現這樣子:

同時我們在index.php加了一個斷點後,現在就已經出現了斷點調試信息,表示我們已經成功了。注意:是在index.php這一欄。現實的。

同樣,由於我們也配置了cli的環境,我們當然也是可以在cli模式下運行和debug的:

注意:xdebug,只允許一個進程在跑,所以,當你打開另一個的時候,記得將起他的全部關掉。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章