使用 ubuntu、php、sublime 和 vagrant 设置 xdebug

Setting up xdebug with ubuntu, php, sublime and vagrant

我的 mac 本地有一个 vagrant box 运行 以及 PHP 7.4 和 xdebug。我可以看到 xdebug 加载了:

vagrant@vagrant:~$ php -v
PHP 7.4.28 (cli) (built: Feb 17 2022 16:06:19) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.28, Copyright (c), by Zend Technologies
    with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans

...和 ​​/etc/php/7.4/mods-available/xdebug.ini 具有:

zend_extension = /usr/lib/php/20190902/xdebug.so
xdebug.remote_autostart = 1
xdebug.remote_enable = 1
xdebug.remote_handler = dbgp
xdebug.remote_host = 127.0.0.1
xdebug.remote_log = /tmp/xdebug_remote.log
xdebug.remote_mode = req
xdebug.remote_port = 9000 #if you want to change the port you can change

我的 Sublime Text 3/Packages/User/Xdebug.sublime-settings 有:

...
    "host": "127.0.0.1",

    // Which port number Sublime Text should listen
    // to connect with debugger engine.
    "port": 9000,
...

我尝试将端口 9000 从 vagrant 转发到主机,但我收到一条错误消息,指出该端口已被使用。我猜这是因为 sublime 在主机端使用它。

我遇到了一些问题。首先,我需要 xdebug v.3(不是 2)的 xdebug 配置方法。此外,client_host 需要设置为 VM 的网关,即 10.0.2.2 而不是 127.0.0.1。您可以使用 route -nee

查看 VM 的网关

这是我在 /etc/php/7.4/mods-available/xdebug.ini

中的设置
zend_extension = /usr/lib/php/20190902/xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host= 10.0.2.2
xdebug.client_port = 9000
xdebug.log = /var/log/xdebug.log