开发环境说明
IDE:Visual Studio Code
DNMP:https://github.com/yeszao/dnmp
项目:laravel
开启xdebug
修改dnmp/php/php72/Dockerfile,添加以下两行(插在带\的任一一行后面即可,查看dockerfile编写规范):
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
修改dnmp/config/php.ini,在文件末尾添加以下内容:
[xdebug]
zend_extension=xdebug.so
xdebug.remote_port=9000
xdebug.remote_enable=1
xdebug.remote_host=192.168.0.102
xdebug.remote_autostart=1
xdebug.remote_log="/var/log/dnmp/xdebug.log"
其中192.168.0.102就是你的电脑的IP,也就是docker宿主机的IP,docker的端口都挂到宿主机IP上。xdebug默认是localhost,不改就访问不到了。也不要打开
xdebug.remote_connect_back=1,这会使得remote_host配置失效。
在vscode中安装PHP Debug插件,在调试选项卡中打开齿轮配置,即当前项目的.vscode/launch.json,对照以下内容配置:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"pathMappings":{
"/var/www/html": "${workspaceRoot}"
}
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
其中,/var/www/html是你在dnmp/www中的site的位置,如果有子目录则填到子目录级别,这里的pathMappings是最新的写法。
在dnmp目录启动:docker-compose up
在vscode按F5启动调试,记得打上断点,祝你成功。
如果已经生成过容器,则需要删除容器,删除镜像。重新生成dnmp-php命令如下:
docker rm dnmp-php
docker rmi dnmp_php
docker-compose up