一直没用过xDebug,习惯于用echo、log调试好多年。
今日研究Laravel,了解其原理、设计模式后,依旧看不懂调用顺序,所以还是装个xDebug,用单步跟一下来进一步了解。
一、mac系统,直接brew安装
Molsen$ brew install homebrew/php/php71-xdebug
安装后提示:
To launch php-fpm on startup:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist
...
brew services start homebrew/php/php71
// 看下版本
Molsen$ php -v
PHP 7.1.11 (cli) (built: Oct 27 2017 11:00:43) ( NTS )
// 看下 php.ini 文件的位置
Molsen$ php -i | grep php.ini
Cannot load Xdebug - it was already loaded
Configuration File (php.ini) Path => /usr/local/etc/php/7.1
Loaded Configuration File => /usr/local/etc/php/7.1/php.ini
// 查看xdebug安装目录
Molsen$ cat /usr/local/etc/php/7.1/conf.d/ext-xdebug.ini
[xdebug]
zend_extension="/usr/local/opt/php71-xdebug/xdebug.so"
// 编辑php.ini,增加配置
Molsen$ vim /usr/local/etc/php/7.1/php.ini
php.ini 增加以下配置
zend_extension="/usr/local/opt/php71-xdebug/xdebug.so"
xdebug.profiler_enable = on
xdebug.trace_output_dir = "/data/logs/xdebug"
xdebug.profile_output_dir = "/data/logs/xdebug"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = "localhost"
xdebug.remote_port = 9000
xdebug.idekey = "phpStorm"
保存并退出,重启php-fpm brew services restart homebrew/php/php71
// 查看 xdebug 的php扩展是否安装成功
Molsen$ php -i |grep xdebug
Cannot load Xdebug - it was already loaded
/usr/local/etc/php/7.1/conf.d/ext-xdebug.ini
xdebug
xdebug support => enabled
...
// 能看到这些就是装好了
二、VS Code 配置
- 安装 PHP Debug 扩展
- 左侧调试按钮 - 左侧顶部调试[Listen for XDebug],设置
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
查看9000端口的监听情况
Molsen$ lsof -i:9000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 70380 Molsen 6u IPv4 0xc7008b2dba54edf9 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 70383 Molsen 0u IPv4 0xc7008b2dba54edf9 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 70383 Molsen 3u IPv4 0xc7008b2dade35ad1 0t0 TCP localhost:cslistener->localhost:58760 (CLOSE_WAIT)
php-fpm 70383 Molsen 7u IPv6 0xc7008b2d9fe87689 0t0 TCP localhost:58761->localhost:cslistener (ESTABLISHED)
php-fpm 70384 Molsen 0u IPv4 0xc7008b2dba54edf9 0t0 TCP localhost:cslistener (LISTEN)
Code\x20H 72517 Molsen 27u IPv6 0xc7008b2d9fe87bc9 0t0 TCP *:cslistener (LISTEN)
Code\x20H 72517 Molsen 31u IPv6 0xc7008b2db38bdbc9 0t0 TCP localhost:cslistener->localhost:58761 (ESTABLISHED)
我是在配置 Visual Studio Code 多次失败的情况下,已经放弃了它,然后转向配置phpstrom的时候,突然发现VS Code已经可以用了。。。没找到原因。