突然发现是可以使用 VSCode 调试单个 PHP 文件的,今天之前一直没有弄成功,还以为 VSCode 是不能调试单文件呢。这里记录一下今天这个“突然发现”的过程。

开始,是在看 Modern PHP 这本书,看到 "Built-in HTTP Server" 一节,自己测试了启动PHP内置服务器软件的命令:php -S localhost:4000,成功看到浏览器页面显示出相关页面。与 Apache 设置的 Web 网站的效果是一样的。

然后我突然就想到,能不能调试在内置服务器中运行的PHP代码呢?此时,我并没有意识到相关的东西。只是在网络上搜索 xdebug php build-in server 等关键词,找到了别人已经在 stackoverflow 上提过的问题,这很正常,通常你能提出的问题,别人可能早已经提过了。这个问题下面有个人给出了一个链接:vim-xdebug-and-php-54s-development-server ,点进去看了一下,部分原文引用如下:

I recently began using xdebug and vim for debugging and breakpoints. I had been using Eclipse and xdebug, and missed this feature when I switched back to vim.

I found a few articles on the subject, but most were fairly old, missed some key points and didn't mention the 5.4 development server. I used this article as my starting point, but I had to fill in a few gaps.

  1. First off, make sure xdebug is installed by checking phpinfo for the section on xdebug. I use the ppa/ondrej repository for managing PHP installations and was able to install xdebug through apt. Installation was as simple as apt-get install php5-xdebug.
  2. Once you have xdebug installed, you'll need to install the vim remote debugger interface. It is a straightforward install - just copy the files into your plugin directory. For me, this was ~/.vim/plugin.
  3. Now, you have to modify your php.ini file in order to allow remote debugging. I also turned on remote autostart so I don't have to deal with the ?XDEBUG_SESSION_START query string on my development server. Since I was using the PHP 5.4 development server, > I had to do some extra work to find out where my php.ini file was located.

    Open up phpinfo again and check for the "Loaded Configuration File" entry. When you do this, make sure you access phpinfo as served by the 5.4 development server. If you access a phpinfo served through Apache2, it may not be the same file. For me, the correct file > was the default /etc/php5/cli/php.ini.

Add these lines to your php.ini to turn on remote debugging and remote autostart.

xdebug.remote_enable=On

xdebug.remote_autostart=On

  1. Now that you've got all of that set up, you can give vim and xdebug a try. Open a file in vim and press F5 to initiate the connection. Then go to your browser and navigate to a page that is being served by the PHP 5.4 development server and go back to vim. If all works well, you should see that a connection has been established. Press enter to begin debugging.

按照他的步骤,第一步安装了 php-xdebug 模块,第二步我使用的是 VSCode 的 xdebug 插件,该插件与 xdebug 进行通信,第三步要修改 php.ini,我打开内置服务器上的 phpinfo() 输出页面,找到加载的 php.ini 文件路径:/etc/php/7.0/cli/php.ini,打开 php.ini 添加了两行配置:

  1. xdebug.remote_enable=1
  2. xdebug.remote_autostart=1

保存了之后重启内置PHP服务器。xDebug 就算配置好了。

然后在 VSCode 添加一个调试配置,就像之前调试 Apache Web 网站一样,打开对 xDebug 端口的监听:

test.php 页面设置断点后,打开浏览器,访问:http://localhost:4000/test.php ,就能自动运行到断点处了。

至此,已经成功地调试内置PHP服务器中的代码。

但是我自然地想到,这里没有 Apache Web 站点,也能成功调试,原因很显然是安装了 php-xdebug 扩展,修改了 php.ini 的配置,等等!,修改了 php.ini 的配置!这个 php.ini 的配置是 CLI 下PHP的配置,那么,也就是说,直接命令行执行PHP脚本文件应该也能调试。

于是在 VSCode 中增加了调试单个文件的配置:在 .vscode/launch.json 中增加下面代码:

  1. "configurations": [
  2. {
  3. "name": "Launch currently open script",
  4. "type": "php",
  5. "request": "launch",
  6. "program": "${file}",
  7. "cwd": "${fileDirname}",
  8. "port": 9000
  9. }
  10. ]

这个配置就是 VSCode 的 xDebug 插件的默认生成的调试PHP单文件的配置,其插件文档里面有说明,早就看到了,但是并不能调试,一点调试按钮就运行完了。也就是说,中间缺少了某些东西,这些东西能够让 xDebug 插件捕获到 xDebug 调试进程。现在,增加了 CLI版本的 php.ini 的配置以后,一点调试,果真成功运行到断点。

所以最终实现了,打开一个PHP文件,直接 F5 开启调试,脚本就能运行到断点了。实现了一键调试PHP单文件脚本。

PS - 个人博客原文:使用 VSCode 调试单个 PHP 文件

使用VSCode调试单个PHP文件的更多相关文章

  1. vscode调试单个PHP脚本文件

    1.安装完vscode里的debug插件后, 在WorkSpace setting:添加上php的可执行文件路径: 2.下载适合自己PHP版本的Xdebug 3.在PHP目录下的php.ini配置文件 ...

  2. vscode断点调试本地客户端文件

    一.安装chrome,安装vscode,打开vscode编辑器,安装插件Debugger for Chrome 二.新建文件 1.目录结构 . ├── index.html ├── index.js ...

  3. Vscode调试C的多文件工程配置

    关于Vscode的C语言的单文件调试,可以参见VScode调试C语言的设置(win10,Linux),里面已经说明基本的配置和使用. 下面说明一下如何调试多个文件的工程,首先写一个简单的工程,其中工程 ...

  4. vscode调试html文件

    1. vscode调试html文件 1.1. 使用Debugger for Chrome进行调试 1.1.1. 基于本地file配置方式调试 1.1.2. 基于服务端配置方式调试 1.1.2.1. 启 ...

  5. C# 使用命令行编译单个CS文件

    编译单个CS文件. 1.编译   File.cs   以产生   File.exe:       csc   File.cs     2.编译   File.cs   以产生   File.dll:  ...

  6. VSCode调试go

    VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%   1.问题描述 由于安装VS15 Preview ...

  7. 【转】vscode调试运行c#详细操作过程

    [转]vscode调试运行c#详细操作过程 主要命令: //路径跳转cd //新建项目dotnet new console -o 路径 //运行dotnet run //用于发布exe<Runt ...

  8. vscode 调试 TypeScript

    安装 typescript 依赖 npm install typescript --save-dev 目录结构: 添加 tsconfig.json 主要是将 sourceMap 设置为true. { ...

  9. vscode 调试node.js

    在开发的过程中,几乎不可能一次性就能写出毫无破绽的程序,断点调试代码是一个普遍的需求. 作为前端开发工程师,以往我们开发的JavaScript程序都运行在浏览器端,利用Chrome提供的开发者工具就可 ...

随机推荐

  1. [Errno 256] No more mirrors to try 解决方法

    安装tree时遇到问题yum [Errno 256] No more mirrors to try 解决方法: 1.yum clean all 2.yum makecache 3.yum update ...

  2. Git Flow 工作模型与使用

    一. Git Flow 工作模型的原理 无规矩不成方圆,但是规矩太多了,则感觉到束缚.我们一个人工作的时候喜欢无拘无束,想怎么干就怎么干,没有人评判,没有人检验.时间久了就会盲目自大,以为增删改查熟悉 ...

  3. Azure DevKit(AZ3166)源码找不到头文件问题

    Get “Error Presented: #include errors detected” when opening a project The error message is Error Pr ...

  4. Beta阶段第三次冲刺

    Beta阶段第三次冲刺 严格按照Git标准来,组员有上传Git的才有贡献分没有的为0 代码签入图 1.part1 -站立式会议照片 2.part2 -项目燃尽图 3.part3 -项目进展 1.正在进 ...

  5. Windows Server 2008远程桌面端口更改方法

    win2008远程桌面端口默认是用的是3389端口,但是由于安全考虑,经常我们安装好系统后一般都会考虑把原来的3389端口更改为另外的端口.本文以改为端口为25608商品为例,讲解一下具体操作过程. ...

  6. volatile和synchronized的区别与联系[转]

    volatile是一个变量修饰符,而synchronized是一个方法或块的修饰符.所以我们使用这两种关键字来指定三种简单的存取变量的方式. int i1;                       ...

  7. 一些node模块的学习思考

    12月14日清单 1 readline模块 var readline = require("readline"); // input 是必须的,output是可选的 rl = re ...

  8. HTTP协议请求方式: 中GET、POST和HEAD的介绍_孤帆一叶

    HTTP协议中GET.POST和HEAD的介绍 2008-05-10 14:15 GET: 请求指定的页面信息,并返回实体主体.HEAD: 只请求页面的首部.POST: 请求服务器接受所指定的文档作为 ...

  9. Linux 平台下的漏洞扫描器 Vuls

    导读 Vuls 是一款适用于 Linux/FreeBSD 的漏洞扫描程序,无代理,采用 Go 语言编写,对于系统管理员来说,每天必须执行安全漏洞分析和软件更新都是一个负担. 为避免生产环境宕机,系统管 ...

  10. ViewData、ViewBag、TempData、Session的区别与联系

    简介 这篇文章是我在学习ASP.NET MVC程序传值方式梳理总结的笔记.在ASP.NET MVC中,页面间和Controller与View之间主要有以下几种小量数据传值方式, ViewData.Vi ...