使用VSCode调试单个PHP文件
突然发现是可以使用 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.
- 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.
- 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.
- 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
- 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文件的更多相关文章
- vscode调试单个PHP脚本文件
1.安装完vscode里的debug插件后, 在WorkSpace setting:添加上php的可执行文件路径: 2.下载适合自己PHP版本的Xdebug 3.在PHP目录下的php.ini配置文件 ...
- vscode断点调试本地客户端文件
一.安装chrome,安装vscode,打开vscode编辑器,安装插件Debugger for Chrome 二.新建文件 1.目录结构 . ├── index.html ├── index.js ...
- Vscode调试C的多文件工程配置
关于Vscode的C语言的单文件调试,可以参见VScode调试C语言的设置(win10,Linux),里面已经说明基本的配置和使用. 下面说明一下如何调试多个文件的工程,首先写一个简单的工程,其中工程 ...
- vscode调试html文件
1. vscode调试html文件 1.1. 使用Debugger for Chrome进行调试 1.1.1. 基于本地file配置方式调试 1.1.2. 基于服务端配置方式调试 1.1.2.1. 启 ...
- C# 使用命令行编译单个CS文件
编译单个CS文件. 1.编译 File.cs 以产生 File.exe: csc File.cs 2.编译 File.cs 以产生 File.dll: ...
- VSCode调试go
VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH% 1.问题描述 由于安装VS15 Preview ...
- 【转】vscode调试运行c#详细操作过程
[转]vscode调试运行c#详细操作过程 主要命令: //路径跳转cd //新建项目dotnet new console -o 路径 //运行dotnet run //用于发布exe<Runt ...
- vscode 调试 TypeScript
安装 typescript 依赖 npm install typescript --save-dev 目录结构: 添加 tsconfig.json 主要是将 sourceMap 设置为true. { ...
- vscode 调试node.js
在开发的过程中,几乎不可能一次性就能写出毫无破绽的程序,断点调试代码是一个普遍的需求. 作为前端开发工程师,以往我们开发的JavaScript程序都运行在浏览器端,利用Chrome提供的开发者工具就可 ...
随机推荐
- String、StringBuffer与StringBuilder的区别-陈远波
String Stringbuffer StringBuilder的区别: 1.string是字符串常量,且长度是不可改变的,Stringbuffer.stringBuilder是字符串变量 2.S ...
- 题解 P2920 【[USACO08NOV]时间管理Time Management】
题面 作为一名忙碌的商人,约翰知道必须高效地安排他的时间.他有N工作要 做,比如给奶牛挤奶,清洗牛棚,修理栅栏之类的. 为了高效,列出了所有工作的清单.第i分工作需要T_i单位的时间来完成,而 且必须 ...
- unity StrangeIoc
已经很久没有写博客,主要原因还是自我荒废了太久,在学习上失去了动力.最近来新的公司实习,以前都是做项目的开发,现在被调到框架组,主要从事的是框架维护还有开发.学习了许多新的知识还有优秀的框架,今天就写 ...
- PyQt5--CloseWindow
# -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...
- 技术分享之AQS——内容提要
1. 背景 最近团队内部技术分享,我做了个关于AQS的分享.ppt中涵盖的部分要点内容,现在整理到博客上. 关于AQS本身的源码解读,可以参考我之前的博文. 2. 要点梳理 下面是一些技术分享的要点梳 ...
- iosclient发现_世界杯送流量活动项目总结
世界杯如火如荼的进行.视频站点相似于门户站点.须要高速依据外部环境更新内容. 产品经理须要策划活动,并安排实施.这个活动就是在这样背景下产生的,爱奇艺与运营商合作,实现双赢.爱奇艺能够通过运营商 ...
- 《阿里巴巴 Java 开发手册》划重点!
[强制]小数类型为 decimal,禁止使用 float 和 double. 说明:float 和 double 在存储的时候,存在精度损失的问题,很可能在值的比较时,得到不 正确的结果.如果存储的数 ...
- mysql row日志格式下 查看binlog sql语句
有时候我们需要使用row作为binlog的日志格式,即配置文件使用了binlog_format= row 参数 这样以来,我们在查看数据库binlog内容时候,就看不到增删改查的具体语句了,在数据库恢 ...
- WorldWind源码剖析系列:表面瓦片类SurfaceTile
表面瓦片类SurfaceTile描述星球类(如地球)表面纹理影像的瓦片模型.其类图如下. 表面瓦片类SurfaceTile包含的主要的字段.属性和方法如下: int m_Level;//该瓦片所属金字 ...
- Python2.7-argparse
argparse模块,用于方便地编写用户友好的命令行接口,可以自动生成帮助信息,用法信息和错误处理 1.模块对象 1.1 ArgumentParser 对象 1.1.1 初始化构建类:Argument ...