phpstorm 調試
by Ray Naldo
雷·納爾多(Ray Naldo)
PhpStorm中的多用戶調試 (Multi-User Debugging in PhpStorm)
使用Xdebug和DBGp代理 (Using Xdebug and DBGp Proxy)
“Er, wait a minute… Don’t you just use xdebug.remote_connect_back
which has been introduced since Xdebug 2.1?"
“嗯,請稍等...您不只是使用Xdebug 2.1之后引入的xdebug.remote_connect_back
嗎?”
Sure if the web server is only accessible by the developers (e.g. private development server), and if it’s not running behind a NATted firewall, and if you want this guide to end here. See those IFs? Personally, I don’t like IF in programming or in life. So this guide will take the longer way which doesn’t need an IF to start (or at least fewer IFs), that is by using Xdebug’s DBGp proxy.
確保Web服務器僅可由開發人員訪問(例如,私有開發服務器),并且它不在NATted防火墻后面運行,以及是否希望本指南在此處結束。 看到那些IF嗎? 就個人而言,我在編程或生活中都不喜歡IF。 因此,本指南將采用更長的方式,即不需要使用IF(或者至少需要更少的IF)來啟動,即使用Xdebug的DBGp代理。
When a proxy is used, the PHP Xdebug extension no longer connects to PhpStorm directly, but instead connects to the DBGp proxy server. All developers in the team, in turn, then connect to that proxy. Each developer has a separate debugging session running over this proxy, which makes it possible to do multi-user debugging of the same code on the same server.
使用代理時,PHP Xdebug擴展不再直接連接到PhpStorm,而是連接到DBGp代理服務器。 然后,團隊中的所有開發人員都將連接到該代理。 每個開發人員都在此代理上運行單獨的調試會話,這使得可以對同一服務器上的相同代碼進行多用戶調試。
So, with DBGp proxy you can limit who can connect to the proxy, and you may have multiple developers debugging the same web server running behind a NATted firewall.
因此,使用DBGp代理,您可以限制誰可以連接到代理,并且可能有多個開發人員調試在NATted防火墻后運行的同一Web服務器。
Running a DBGp proxy also allows you to avoid NAT issues where (as seen from PHP+Xdebug on the server) all connections seem to come from the same IP (because your internal network is NATted). In this case, you can simply run the dbgp proxy on your NAT machine, configure
xdebug.remote_host
setting to the IP address of your NAT machine, and configure the IDEs to connect to the proxy running at<NAT-machine>
;:9001.運行DBGp代理還可以避免NAT問題(如從服務器上PHP + Xdebug看到的),所有連接似乎都來自同一IP(因為您的內部網絡已被NAT)。 在這種情況下,您只需在NAT計算機上運行dbgp代理,將
xdebug.remote_host
設置配置為NAT計算機的IP地址,然后將IDE配置為連接到運行在<NAT-machine>
上的代理; :9001。
— https://derickrethans.nl/debugging-with-multiple-users.html
— https://derickrethans.nl/debugging-with-multiple-users.html
建立 (Setup)
Mappings between the project folders and the folders on the server should be done correctly in PhpStorm first for debugging to work.
應該首先在PhpStorm中正確完成項目文件夾和服務器上的文件夾之間的映射,然后才能進行調試。
設置Web服務器 (Setup Web Server)
Although this guide assumes the web server is running on Linux, the guide could also be used on non-Linux web servers with slight modifications.
盡管本指南假定Web服務器在Linux上運行,但該指南也可以在非Linux Web服務器上使用,并稍加修改。
1. Install Xdebug.
1.安裝Xdebug。
# PHP 7+pecl install xdebug # PHP 5.6.xpecl install xdebug-2.5.5
2. Enable Xdebug extension, then add the following Xdebug configuration to php.ini:
2.啟用Xdebug擴展,然后將以下Xdebug配置添加到php.ini:
[xdebug]zend_extension="<full_path_to_xdebug.so>"
; debugger settingsxdebug.remote_enable=1xdebug.remote_host=127.0.0.1xdebug.remote_port=9000
For this guide, DBGp proxy will run on the same machine as the web server and will use Xdebug’s default port, hence 127.0.0.1:9000
.
對于本指南,DBGp代理將與Web服務器在同一臺計算機上運行,??并將使用Xdebug的默認端口,即127.0.0.1:9000
。
3. Download and install DBGp proxy for remote debugging from Komodo Remote Debugging Package, specifically for your web server’s operating system. This guide will be using 64-bit Linux and PHP Remote Debugging client v11.1.0. Extract the archive; for simplicity I extract all the contents to my home directory i.e. /home/ray/
.
3.從Komodo Remote Debugging Package中下載并安裝DBGp代理,以進行遠程調試,特別是針對Web服務器的操作系統。 本指南將使用64位Linux和PHP遠程調試客戶端v11.1.0。 提取檔案; 為簡單起見,我將所有內容提取到我的主目錄(即/home/ray/
。
4. Run DBGp proxy by executing pydbgpproxy
file with parameters:
4.通過執行帶有參數的pydbgpproxy
文件來運行DBGp代理:
-d <ip_address:po
rt> to set IP address and port of the machine which will be receiving debugger connection from the web server-d <ip_address:po
rt>設置將要從Web服務器接收調試器連接的計算機的IP地址和端口-i <ip_address:po
rt> to set IP address and port of the machine which will be receiving debugging connection from developer computer-i <ip_address:po
rt>設置將要從開發人員計算機接收調試連接的計算機的IP地址和端口
In this guide, web server and DBGp proxy will be run on the same machine. If the IP address is 10.211.1.32
and we want to run the proxy on port 9001
then the command will be:
在本指南中,Web服務器和DBGp代理將在同一臺計算機上運行。 如果IP地址為10.211.1.32
并且我們想在端口9001
上運行代理,則命令將為:
pydbgpproxy -d 127.0.0.1:9000 -i 10.211.1.32:9001
For convenience we could use this script, saved as start-dbgp-proxy.sh
.I placed it on the same directory as pydbgpproxy
i.e. /home/ray/start-dbgp-proxy.sh
) :
為方便起見,我們可以使用這個腳本,保存為start-dbgp-proxy.sh
。我把它放在同一個目錄中pydbgpproxy
即/home/ray/start-dbgp-proxy.sh
):
ip=$(hostname -I | awk '{$1=$1};1')pydbgpproxy -d 127.0.0.1:9000 -i $ip:9001
5. Make sure to allow connection from localhost on port 9000
, and from developer machines on port 9001
.
5.確保允許從端口9000
上的localhost和端口9001
上的開發人員機器進行連接。
6. Run start-dbgp-proxy.sh
. Add execute file permission if you can't run it.
6.運行start-dbgp-proxy.sh
。 添加執行文件權限(如果無法運行)。
start-dbgp-proxy.sh
Make sure it can be run without a problem.
確保它可以正常運行。
INFO: dbgp.proxy: starting proxy listeners. appid: 30430INFO: dbgp.proxy: dbgp listener on 127.0.0.1:9000INFO: dbgp.proxy: IDE listener on 10.211.1.32:9001
7. (Optional) Auto-start start-dbgp-proxy.sh
on each machine startup by using crontab.
7.(可選)使用crontab在每次啟動計算機時自動啟動start-dbgp-proxy.sh
。
Edit crontab:
編輯crontab:
crontab -e
Add cron job to auto-start start-dbgp-proxy.sh
on each startup:
在每次啟動時將cron作業添加到自動啟動start-dbgp-proxy.sh
:
@reboot /home/ray/start-dbgp-proxy.sh
設置客戶端 (Setup Client)
1. Access menu Tools > DBGp Proxy > Regist
er IDE on PhpStorm.
1.在PhpStorm上訪問菜單Tools > DBGp Proxy > Regist
IDE。
2. Fill IDE key
with unique string between developers. FillHost
andPort
with DBGp proxy's IP address and port (parameter -i
on Setup Server #4).
2.用開發人員之間的唯一字符串填充IDE key
。 用DBGp代理的IP地址和端口(在Setup Server#4上的參數-i
)填充Host
和Port
。
3. Click OK. You should see a success notification popup. If you don’t see it, re-register IDE via Tools > DBGp Proxy > Regist
er IDE. If it's failed or you want to change the configuration, do it via Tools > DBGp Proxy > Conf
iguration...
3.單擊確定。 您應該看到一個成功通知彈出窗口。 如果看不到,請通過Tools > DBGp Proxy > Regist
IDE重新注冊IDE。 如果失敗或要更改配置,請不要t via Tools > DBGp Proxy > Conf
配置” ...
4. (Optional) If you want to initiate debugging connection from the web browser, it’s recommended to install a debugging extension on your browser: Xdebug Helper Firefox or Xdebug Helper Chrome. Then configure your Xdebug Helper.
4.(可選)如果要通過Web瀏覽器啟動調試連接,建議在瀏覽器上安裝調試擴展程序: Xdebug Helper Firefox或Xdebug Helper Chrome 。 然后配置您的Xdebug助手。
On Firefox, right click Xdebug Helper icon > Manage Extension… > Options On Chrome, right click Xdebug Helper icon > Options
在Firefox上,右鍵單擊Xdebug Helper圖標>管理擴展…>選項在Chrome上,右鍵單擊Xdebug Helper圖標>選項
Fill and save IDE key
with the same unique string as when you're registering the IDE (Step #2).
使用與注冊IDE時相同的唯一字符串填充并保存IDE key
( 第2步 )。
開始調試 (Start Debugging)
1. Set breakpoint(s) on PhpStorm.
1.在PhpStorm上設置斷點。
2. Start listening to debug connection in PhpStorm by clicking the ‘phone’ button on the upper right toolbar or from menu Run > Start Listening for PHP Debug Connecti
ons. This will enable PhpStorm to react and opens the Debug window automatically when a debugging session is started.
2.單擊右上方工具欄上的“電話”按鈕,或從菜單中的Run > Start Listening for PHP Debug Connecti
PhpStorm中的Run > Start Listening for PHP Debug Connecti
。 啟動調試會話時,這將使PhpStorm作出React并自動打開“調試”窗口。
3. Activate Xdebug’s debugger when doing a request. According to Xdebug Documentation there’re 3 ways to do this. But IMHO, the best way which works for all kinds of HTTP methods is by setting a cookie named XDEBUG_SESSION
with value <IDE_k
ey> which is the same unique string when we register our IDE to DBGp proxy (Setup Client #2).
3.在執行請求時激活Xdebug的調試器。 根據Xdebug文檔,有3種方法可以做到這一點。 但是恕我直言,適用于所有HTTP方法的最佳方法是設置一個名為XDEBUG_SESSION
的cookie,其值為<IDE_k
ey>,當我們將IDE注冊到DBGp poxy (安裝程序 #2)時,該cookie是相同的唯一字符串。
- In the web browser, the cookie will be set automatically by Xdebug Helper extension 在Web瀏覽器中,將通過Xdebug Helper擴展自動設置cookie。
- In Postman, the cookie can be set in Request Headers 在郵遞員中,可以在“請求標題”中設置Cookie
4. Run the script with the cookie already set.
4.在已設置cookie的情況下運行腳本。
5. On success, PhpStorm will show the Debug window automatically.
5.成功后,PhpStorm將自動顯示“調試”窗口。
6. Make sure to unset/don’t send the cookie to disable debug and stop listening debug connection in PhpStorm if you’re not doing any debugging. If you fail to do so, it will make the DBGp proxy hang when there’s too many hanging connections.
6.如果您不進行任何調試,請確保取消設置/不發送cookie來禁用調試并停止監聽PhpStorm中的調試連接。 如果您這樣做失敗,則當掛起的連接過多時,它將使DBGp代理掛起。
Hope this guide works for you.
希望本指南對您有用。
Thank you for reading!
感謝您的閱讀!
參考文獻 (References)
https://www.jetbrains.com/help/phpstorm/multiuser-debugging-via-xdebug-proxies.html
https://www.jetbrains.com/help/phpstorm/multiuser-debugging-via-xdebug-proxies.html
https://www.jetbrains.com/help/phpstorm/browser-debugging-extensions.html
https://www.jetbrains.com/help/phpstorm/browser-debugging-extensions.html
https://xdebug.org/docs/remote#starting
https://xdebug.org/docs/remote#starting
翻譯自: https://www.freecodecamp.org/news/multi-user-debugging-in-phpstorm-75ef628ed50f/
phpstorm 調試