目录

文章 AI 生成,仅供测试 AI 工具能力。内容也经过我们确认调整,是可以使用的,对初学者有一次帮助,后续都可以问 AI 去搭建自己更趁手的环境。

要在 VSCode 中配置 PHP 代码的远程调试,可以通过以下步骤实现:

1. 安装并配置 SFTP 插件

如果你使用远程服务器,则需要安装并配置 SFTP 插件。

在 VSCode 的扩展市场中搜索并安装 SFTP 插件(由 Natizyskunk 或 liximomo 开发的版本均可)。

安装完成后,按照以下步骤配置:

  1. 在 VSCode 中打开项目文件夹。
  2. 通过快捷键 Ctrl+Shift+P 打开命令面板,输入 SFTP: Config
  3. 这将在项目根目录创建 .vscode/sftp.json 配置文件,内容类似如下:
{
    "name": "远程服务器",
    "host": "服务器 IP 地址",
    "protocol": "sftp",
    "port": 22,
    "username": "用户名",
    "password": "密码",
    "remotePath": "/path/to/remote/project",
    "uploadOnSave": true,
    "ignore": [
        ".vscode",
        ".git",
        ".DS_Store",
        "node_modules"
    ]
}
  1. 根据实际情况修改配置:
    • host: 远程服务器的 IP 地址
    • usernamepassword: 登录凭据(也可使用privateKeyPath指定 SSH 密钥文件)
    • remotePath: 远程服务器上的项目路径
    • uploadOnSave: 设为true可在保存文件时自动上传

常用的 SFTP 操作:

  • 右键单击文件/文件夹,选择 SFTP: Upload 可将文件上传到服务器
  • 右键单击,选择 SFTP: Download 可从服务器下载文件
  • 右键单击,选择 SFTP: Sync Local -> Remote 同步本地到远程

2. 安装 PHP 调试插件

在远程服务器上,通过 VSCode 的扩展市场安装 PHP Debug 插件。

3. 配置 Xdebug

确保远程服务器上的 Xdebug 已正确配置,可以在 PHP 配置文件(通常是 php.ini)中添加以下内容:

[xdebug]
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=你的本地IP
xdebug.client_port=9003
  • xdebug.client_host 是本地机器的 IP 地址,Xdebug 会将调试信息发送到此地址。
  • xdebug.client_port 是调试端口,默认为 9003。提供给 IDE 监听的端口,接收 xdebug 发送的调试信息。

比如:

zend_extension=xdebug.so
 
xdebug.client_discovery_header = 
xdebug.client_host = 192.168.40.61
xdebug.client_port = 9003
xdebug.connect_timeout_ms = 200
xdebug.discover_client_host = false
xdebug.log =
xdebug.log_level = 7
xdebug.mode=develop,debug,gcstats,profile,trace
xdebug.start_upon_error = default
xdebug.start_with_request = default
xdebug.trigger_value = ""
xdebug.start_with_request = yes

4. 配置 launch.json

用于接收 xdebug 发送的调试信息。

在 VSCode 的 .vscode/launch.json 文件中添加以下配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "/path/to/remote/project": "${workspaceFolder}"
            }
        }
    ]
}
  • port 是 Xdebug 的调试端口,应与服务器上配置的端口一致。
  • pathMappings 用于映射本地项目路径和远程服务器上的项目路径。

比如:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "/var/www/html/lumen-kid": "${workspaceFolder}"
            }
        }          
    ]
}

5. 启动调试

  1. 在 VSCode 中打开远程项目文件夹。
  2. 在代码中设置断点。
  3. 点击 VSCode 的调试按钮,选择 Listen for Xdebug 配置并启动调试。
  4. 在远程服务器上运行 PHP 脚本,调试器将自动连接到 VSCode。

6. 测试调试

访问远程服务器上的 PHP 脚本,触发断点,VSCode 应该会自动暂停在断点处,允许你进行调试。vscode 的断点调试都差不多。

通过以上步骤,你可以在 VSCode 中实现对远程 Linux 服务器上 PHP 代码的调试。如果遇到问题,可以检查网络连接、Xdebug 配置以及 VSCode 的调试配置是否正确。


9ong@TsingChan 文章内容由 AI 辅助生成。