前言
突然发现我的vscode居然没给php加调试。。。
环境:windows11,phpstudy,php7.4.3,xdebug2.9.4,vscode
phpstudy
软件管理-系统环境-php里面下载一个php版本,我这里用的是php7.4.3nts
php
把下载的php.exe添加到系统环境变量
在设置-文件位置-PHP可以找到下载的php的位置
我这里是”D:/phpstudy_pro/Extensions/php/php7.4.3nts”,把这个路径添加到系统的path环境变量里
然后打开终端输入php -v
查看是否是我们的版本,注:更改完系统变量要重新打开终端
php.ini配置
[Xdebug]
zend_extension=D:/phpstudy_pro/Extensions/php/php7.4.3nts/ext/php_xdebug.dll
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.auto_trace=Off
xdebug.trace_output_dir=D:/phpstudy_pro/Extensions/php_log/php7.4.3nts.xdebug.trace
xdebug.profiler_enable=Off
xdebug.profiler_output_dir=D:/phpstudy_pro/Extensions/php_log/php7.4.3nts.xdebug.profiler
xdebug.remote_enable=On
xdebug.remote_autostart=On
xdebug.remote_host=localhost
xdebug.remote_port=9003
xdebug.remote_handler=dbgp
重点好像就是xdebug.remote_enable
和xdebug.remote_autostart
,然后就是设置xdebug.remote_port
端口
vscode
扩展下载: 具体我也不知道哪个是不必要的(
在setting.json添加以下配置
"php.executablePath": "D:/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe",
"php.debug.executablePath": "D:/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe",
"php.validate.executablePath": "D:/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe",
点击左侧运行与调试的按钮,根据提示,先用vscode打开我们项目所在的文件夹,然后添加launch.json,一般launch.json都会生成在.vscode文件夹下
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9003,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9003,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}
运行并调试
随便写一个脚本
<?php
for($i=1;$i<=5;$i++){
echo $i;
}
然后f5运行调试功能,接下来在网页中打开,可以右键PHP Server:open file in browser,也可以用phpstudy开
这样就能调试了
多版本调试
在项目文件夹下新建一个工作区
.code-workspace文件如下(以php5.6.9为例)
{
"folders": [
{
"path": ".."
}
],
"settings": {
//指定在项目中使用的php
"intelephense.environment.phpVersion": "5.6.9",
"php.debug.executablePath": "D:/phpstudy_pro/Extensions/php/php5.6.9nts/php.exe",
"php.validate.executablePath": "D:/phpstudy_pro/Extensions/php/php5.6.9nts/php.exe",
//指定运行terminal 时使用的php,这点很重要,不然项目用对了,但是terminal执行命令时还是会用系统环境变量
"terminal.integrated.env.windows": {
"PATH": "D:/phpstudy_pro/Extensions/php/php5.6.9nts"
}
}
}
检验:
然后设置一下对应的php.ini
[Xdebug]
zend_extension=D:/phpstudy_pro/Extensions/php/php5.6.9nts/ext/php_xdebug.dll
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.auto_trace=On
xdebug.trace_output_dir=D:/phpstudy_pro/Extensions/php_log/php5.6.9nts.xdebug.trace
xdebug.profiler_enable=On
xdebug.profiler_output_dir="D:\phpstudy_pro\Extensions\tmp\xdebug"
xdebug.remote_enable=On
xdebug.remote_autostart=On
xdebug.remote_host=localhost
xdebug.remote_port=9001
xdebug.remote_handler=dbgp
launch.json也改一下端口即可
解决调试时报错500
发现一个问题:调试过程中老是会突然断掉并且报500错误
原因:apache默认的连接时间过短,调试时超过了默认的连接时间所以就报错了
解决方法:打开对应网站的 vhosts.conf 配置文件,加入代码
FcgidIOTimeout 3000 #程序响应超时时间
FcgidConnectTimeout 3000 #与程序通讯的最长时间
重启apache即可