突然发现是可以使用 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 添加了两行配置:

xdebug.remote_enable=1
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 中增加下面代码:

    "configurations": [
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]

这个配置就是 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. centos7 安装ldap

    ldap首先我们要知道这个ldap的概念, LDAP是轻量目录访问协议(Lightweight Directory Access Protocol)的缩写 目录是一个为查询.浏览和搜索而优化的专业分布 ...

  2. 监控文件内容变化,即时写入到新文件(tail)

    监控文件a,如有新内容写入,即时将新内容写入到新文件aa中: fw=open('e:\\aa.txt','ab') with open('e:\\a.txt','rb') as fo: while T ...

  3. python的学习之路day7-socket网络编程

    python基础部分学习完了,时间也已经过了两个月左右,感觉没学到什么,可能是我学习之后忘记的太多了. 由于没钱买书,要是去培训就更没钱了,所以在网上找了一本书,感觉还不错,讲的比较好,比较详细. P ...

  4. StringBuffer&StringBuilder类

    0. 说明 1. 总体说明 当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类. 和 String 类不同的是,StringBuffer 和 String ...

  5. python中判断实例可迭代地几种方式

    1. 利用 __iter__内建属性 if hasattr(obj, '__iter__') : print 'iterable' 这种方法不能检测字符串,如:hasattr('', '__iter_ ...

  6. Django商城项目笔记No.17用户部分-用户中心用户地址管理

    收货地址管理 首先就是新增地址 看图分析所需要保存的字段 因为是用户的地址,所以在users应用中的models创建模型类 class Address(BaseModel): "" ...

  7. 解决The valid characters are defined in RFC 7230 and RFC 3986错误问题

    分析原因: 导致上述问题是因为tomcat自tomcat 8.0.35版本之后对URL参数做了比较规范的限制,必须遵循RFC 7230 and RFC 3986规范,对于非保留字字符(json格式的请 ...

  8. 开发jQuery插件的基本步骤

    在进行开发jQuery插件前,首先要了解一些知识: 1.闭包 1.1.闭包的作用: · 避免全局依赖 · 避免第三方破坏 · 兼容jQuery操作符'$'和jQuery 1.2.闭包的形式 (func ...

  9. Test传送门(更新中)

    一.Codeforces传送门: Avito Code Challenge 2018 题解传送门 Codeforces Round #485 (Div. 2)     题解传送门 二.hihocode ...

  10. Python高级网络编程系列之基础篇

    一.Socket简介 1.不同电脑上的进程如何通信? 进程间通信的首要问题是如何找到目标进程,也就是操作系统是如何唯一标识一个进程的! 在一台电脑上是只通过进程号PID,但在网络中是行不通的,因为每台 ...