macOS 下 PHPStorm + Xdebug 調試 Docker 環境中的代碼

0x00 描述

宿主機是 mac mini,構建的項目在 docker 中,因此須要在 PHPStorm 上配置 Xdebug 進行遠程代碼調試。php

 

0x01 環境

宿主機:macOS High Sierra 10.13.6
Docker:Docker version 18.06.1-ce, build e68fc7a
容器環境:CentOS Linux release 7.5.1804 (Core) + PHP 7.0.32 + nginx/1.12.2 + Xdebug 2.6.1
PhpStorm:2018.2.5

 

0x03 下載安裝 Xdebug

下載 Xdebug

首先要肯定 Xdebug 的版本要和環境中的 PHP 版本相對應。nginx

進入 docker 環境,打印 phpinfo() 的輸出頁面,或者在命令行輸入docker

php -r "phpinfo();"

獲得的結果,全選,複製,把結果粘貼到 https://xdebug.org/wizard.php 的文本框中,查看對應的 Xdebug 版本。瀏覽器

 

安裝Xdebug

把獲得的 Xdebug 版本下載到 docker 環境中,按照 https://xdebug.org/wizard.php 的說明進行安裝:ide

Download xdebug-2.6.1.tgz
Unpack the downloaded file with tar -xvzf xdebug-2.6.1.tgz
Run: cd xdebug-2.6.1
Run: phpize (See the FAQ if you don't have phpize.

As part of its output it should show:

Configuring for:
...
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
If it does not, you are using the wrong phpize. Please follow this FAQ entry and skip the next step.

Run: ./configure
Run: make
Run: cp modules/xdebug.so /usr/lib64/php/modules
Update /etc/php.ini and change the line
zend_extension = /usr/lib64/php/modules/xdebug.so

 

0x04 配置 PHP 與 Xdebug

在 docker 中,根據 phpinfo 的 php.ini 路徑,打開 php.ini,加入(編輯)如下代碼:php-fpm

[xdebug]
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=10.203.17.92
xdebug.remote_port=9101
xdebug.idekey=PHPSTORM
xdebug.auto_trace=1
xdebug.auto_exception_trace=1
xdebug.remote_autostart=1
xdebug.collect_vars=1
xdebug.collect_return=1
xdebug.collect_params=1
xdebug.show_local_vars=1
xdebug.profiler_enable=1
xdebug.trace_enable_trigger=1
xdebug.remote_log="/var/log/php.xdebug.log"

有幾個地方要注意一下:測試

1. xdebug.remote_host

這是宿主機的 ip,在 docker 外(mac 環境中),輸入命令:ui

ifconfig

找到 en0 中的 inet:this

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
    ether 68:fe:f7:0a:81:ff 
    inet6 fe80::4f0:bb3b:4a49:d7f9%en0 prefixlen 64 secured scopeid 0x5 
    inet 10.203.17.92 netmask 0xffffff00 broadcast 10.203.17.255
    nd6 options=201<PERFORMNUD,DAD>
    media: autoselect (100baseTX <full-duplex>)
    status: active

xdebug.remote_host 就是上面例子中的 10.203.17.92google

 

2. xdebug.remote_port

這裏默認是 9000 端口,改一下避免和 php-fpm 的 9000 端口衝突

 

3. xdebug.remote_log

配置一下,方便排錯

 

另外附上 /etc/php.d/xdebug.ini:

[xdebug]
; repeated defind at /etc/php.d/15-xdebug.ini file
; zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable = on
xdebug.remote_host = 10.203.17.92
xdebug.remote_port = 9101
xdebug.idekey = "PHPSTORM"

這裏和 php.ini 保持一致。

 

 

0x05 配置 PHPStorm

1. 進入 preferences

 

2. 進入 Languages & Frameworks - PHP -Debug ,找到 Xdebug,編輯監聽端口和 php.ini 保持一致。 

 

3. 進入 DBGp Proxy,端口和 php.ini 中配置保持一致,配置以下:

 

4. 進入 Servers,點 「+」 添加一個測試項目,例子中是 admin,Name 隨便填寫,Host 寫項目的域名,端口 80,Debugger 選 Xdebug,勾選下面的路徑映射,在右側填寫與本地項目對應的遠程目錄

 

5. 保存。

 

6. 編輯配置,選擇 Edit Configurations

 

添加一個 PHP Web Page,有的版本是 PHP Web Application,是同樣的:

 

填寫配置信息:

Server 選擇上兩步在 Servers 中添加的 Server,這裏是 admin,另外 Start URL 選擇項目的首頁,即便是 https 的 URL,在上述配置端口的地方同樣配置成 80:

 

7. 保存。

 

8. 調試:

 在代碼中設置斷點(「1」處),打開「2」 處的電話圖標,點擊「3」處的 debug 選項,會自動跳到瀏覽器中以前設置的項目首頁,當觸發到斷點時會自動跳到 PHPStorm 中,「4」處能夠跳過斷點,「5」處顯示變量的值:

 

 

0x06 安裝瀏覽器 Xdebug 插件

在 google 商店中找到並安裝 Xdebug helper,安裝以後能夠在任意頁面啓動調試:

 

如下是 Xdebug helper 的設置頁面:

在一些 https 的項目中,PHPStorm -> Preference -> Languages & Frameworks -> PHP -> Servers 處的端口能夠配置成 443,可是這個時候經過 PHPStorm 的 Debug 按鈕從 Start Url 處開始沒法調試,緣由是 Xdebug 會把調試的地址認爲是 http://xxxx.com:443 而不是 https://xxxx.com,此時就能夠用瀏覽器的 Xdebug helper 插件來直接 debug,選擇下拉菜單中的第一個 Debug 選項就能夠開始 debug 了。

相關文章
相關標籤/搜索