安装xdebug

macos环境下,默认你已经安装了php和pecl,pecl是php下安装php扩展的一种工具,这里我使用的php7.3

  1. 执行以下命令安装xdebug扩展:
    pecl install xdebug
  2. 安装完成后执行:
    php -v
    然后你会看到类似以下返回信息:

    PHP 7.3.20 (cli) (built: Jun  4 2021 03:32:07) ( NTS )
      Copyright (c) The PHP Group
      Zend Engine v3.4.0, Copyright (c) Zend Technologies
       with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans
       with Zend OPcache v7.3.20, Copyright (c), by Zend Technologies

    看到xdebug说明安装成功了,安装成功路径类似是:
    /usr/local/lib/php/pecl/20180731/xdebug.so

  3. 更新php.ini文件,路径类似:
    /usr/local/etc/php/7.3/php.ini
    复制如下内容到文件里我使用的是末尾,这8001端口,如果你使用其他端口,记得修改,否则可能无法监听到变化:

    ; Make sure this is the only line which assign `zend_extension` to `xdebug.so`
    zend_extension = /usr/local/lib/php/pecl/20180731/xdebug.so
    [xdebug]
    xdebug.start_with_request=yes
    xdebug.mode=debug
    xdebug.client_host = 127.0.0.1
    xdebug.client_port = 8001
    
    xdebug.show_exception_trace = On
    xdebug.remote_handler = dbgp

VSCode设置

  1. 安装php debug扩展
    image.png
  2. 设置php debug配置文件,这里端口要和php.ini里的端口一致

    {
      "version": "0.2.0",
      "configurations": [
     {
       "name": "Listen for Xdebug",
       "type": "php",
       "log": true,
       "request": "launch",
       "port": 8001,
       "pathMappings": {
         "/程序所在目录": "${workspaceRoot}"
       },
       "ignore": [
         "**/vendor/**/*.php"
       ]
     }
      ]
    }
  3. 开始调试吧
    image.png

坑的地方

laravel中php artisan serve的端口号不要上面的两个端口号相同,否则serve毫无反应,我一直以为这三个端口要一样才行,结果就是调不通


JasonLiang
1 声望0 粉丝

10年全栈程序猿~