Debug PHP with VSCode and Docker - php

I'm trying to debug a PHP app running on Docker with VSCode, but without success.
In the past I was able to easily debug my PHP apps with VSCode running WAMP Server, but since I started working with Docker I'm unable to get debug working. Searched for several tutorials online, checked some threads here on StackOverflow (ex.: Docker and XDebug not reading breakpoints VSCode), but I'm still not able to get this working.
Dockerfile:
FROM php:7.1.8-apache
COPY /cms /srv/app/cms
COPY .docker/cms/vhosts/vhost.conf /etc/apache2/sites-available/cms.conf
COPY .docker/cms/vhosts/vhost-ssl.conf /etc/apache2/sites-available/cms-ssl.conf
COPY .docker/cms/vhosts/certificate.conf /etc/ssl/certs/certificate.conf
COPY .docker/cms/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
WORKDIR /srv/app/cms
RUN docker-php-ext-install mbstring pdo pdo_mysql
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
RUN chown -R www-data:www-data /srv/app/cms
RUN openssl req -x509 -new -out /etc/ssl/certs/ssl-cert-cms.crt -config /etc/ssl/certs/certificate.conf
RUN a2ensite cms.conf
RUN a2ensite cms-ssl.conf
RUN a2enmod rewrite
RUN a2enmod ssl
xdebug.ini
[xdebug]
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_connect_back=0
xdebug.remote_host='host.docker.internal'
xdebug.idekey='VSCODE'
xdebug.remote_autostart=1
docker-compose.yml
version: '3.7'
services:
cms:
build:
context: .
dockerfile: .docker/cms/Dockerfile
image: php:7.1.8-apache
ports:
- 18080:80
- 14430:443
volumes:
- ./cms:/srv/app/cms
links:
- mysql
- redis
environment:
DB_HOST: mysql
VIRTUAL_HOST: my.app.localhost
PHP_EXTENSION_XDEBUG: 1
VSCode: launch.json
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/srv/app/cms": "${workspaceRoot}/my.app/cms",
},
"port": 9000
}, {
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
When I debug the app no breakpoint is being triggered. What am I doing wrong?
UPDATE:
Based on some suggestions i've updated my docker-compose.yml and my launch.json files but nothing changed.
docker-compose.yml
ports:
- 18080:80
- 14430:443
- 9000:9000 //added new xdebug default port
launch.json
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/srv/app/cms": "${workspaceRoot}/my.app/cms",
},
"port": 9000,
"log": true
}
]
VSCode Debug Console:
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
UPDATE #2:
Removed the Xdebug port (9000) from the docker-compose.yml settings. Here is the xdebug log result:
Log opened at 2018-09-30 22:21:09 I: Connecting to configured
address/port: host.docker.internal:9000. E: Time-out connecting to
client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:09
Log opened at 2018-09-30 22:21:17 I: Connecting to configured
address/port: host.docker.internal:9000. E: Time-out connecting to
client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:17
Log opened at 2018-09-30 22:21:18 I: Connecting to configured
address/port: host.docker.internal:9000. E: Time-out connecting to
client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:18
Log opened at 2018-09-30 22:21:18 I: Connecting to configured
address/port: host.docker.internal:9000. E: Time-out connecting to
client (Waited: 200 ms). :-( Log closed at 2018-09-30 22:21:18
Any more suggestions?

Managed to solve my issue with the following settings:
launch.json
{
"version": "0.2.0",
"configurations": [{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/srv/app/cms": "${workspaceRoot}/cms",
},
"ignore": [
"**/vendor/**/*.php"
]
},
]
}
xdebug.ini
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_host=host.docker.internal
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/usr/local/etc/php/xdebug.log

Since xdebug version 3 there are breaking changes in config names.
My current working dockerfile:
RUN apt-get update; \
apt-get -y --no-install-recommends install \
php7.4-dev \
php-pear \
libcurl3-openssl-dev \
libmcrypt-dev \
libxml2-dev \
libpng-dev \
; \
pecl install xdebug; \
{ \
echo "[xdebug]"; \
echo "zend_extension=$(find /usr/lib/php/ -name xdebug.so)"; \
echo "xdebug.mode=debug"; \
echo "xdebug.start_with_request=yes"; \
echo "xdebug.client_host=host.docker.internal"; \
echo "xdebug.client_port=9001"; \
} >> /etc/php/7.4/mods-available/xdebug.ini; \
phpenmod -v 7.4 xdebug; \
VSC debug config:
"configurations": [
{
"name": "XDebug (Docker)",
"type": "php",
"request": "launch",
"port": 9001,
"pathMappings": {
"/var/www/app": "${workspaceRoot}",
}
}
]
More info: https://xdebug.org/docs/upgrade_guide

Well, I have tried all the above answers and have spend hours but got not success because in all the above answer some concepts were not clear to new x-debug user just like me. I wish that I would have know the following concepts before starting setting up x-debugger, running on a docker container or a remote server, with my VS Code's PHP Debug. I am sure this will help searching for the solution of such issue.
X-Debug Settings
I was not certain that if my machine should be considered as client host or server host, though it was obvious but when you have spent hours on configurations then simple things started to get tricky ones.
xdebug.client_host
According to X-debug Docs:
This address should be the address of the machine where your IDE or debugging client is listening for incoming debugging connections.
I think, with the above piece of text needs no explanation. Hence, setup it up as follows in our x-debug configurations i.e. xdebug.ini:
xdebug.client_host=host.docker.internal
xdebug.remote_host
This was a bit tricky config to me. Actually there is no such configurations for x-debug but I kept thinking about it. :-)
PHP-Debug Settings
hostname
I had been using different configurations except this one (as in this thread no one had mentioned this) :-) and there was no success. I then re-explored the docs of PHP Debug came to know that it must get set to localhost in my case, and gave it a try, and boom! It worked!
"hostname": "localhost"
port
The should be set same as the xdebug.client_port and default value is 9300. Repeatedly mentioned in above answers.
pathMappings
This is an other really important config when trying to setup a docker server with your VS Code PHP Debug. It is an object and must map your local directory structure to the server/container directory i.e. /home/user/proj/html: ${workspaceRoot}. I can't further explain it, it did worked for me and I have mentioned it here. I will welcome if someone explain it.
Here are my final settings:
Hope this will help and save your hours :-)

you are missing port :9000 (or :9001) in the docker-compose.yml,
which needs to be connectable, for the IDE to connect from the outside.
for VSCode the PHP Debug extension might be required to interact with xdebug.
the default launch.json only uses port: 9000 only once - and has log: true.
{
"configurations": [{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true
}, {
"name": "Launch",
"request": "launch",
"type": "php",
"program": "${file}",
"cwd": "${workspaceRoot}",
"externalConsole": false
}
]
}
also see vscode-php-debug and starting the debugger.

For everyone running on the following stack:
Ubuntu
XDebug 3
Laravel Sail
docker/8.0/php.ini
[xdebug]
xdebug.discover_client_host=true
xdebug.start_with_request=yes
.env
SAIL_XDEBUG_MODE=develop,debug
# For linux only:
# you may read up about how to get the ip here: https://laravel.com/docs/8.x/sail#debugging-with-xdebug
SAIL_XDEBUG_CONFIG="client_host=172.22.0.1"
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug via Docker",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/html": "${workspaceRoot}/www",
},
"ignore": [
"**/vendor/**/*.php"
]
},
] }

XDEBUG v3 example for Symfony
docker-compose.yml:
###> XDEBUG 3 ###
# Use your client IP here
# Linux: run "ip a | grep docker0"
# Windows (with WSL2): Run "grep nameserver /etc/resolv.conf | cut -d ' ' -f2"
# MacOS: host.docker.internal
###< XDEBUG 3 ###
environment:
XDEBUG_CLIENT_HOST: 172.17.0.1
XDEBUG_CLIENT_PORT: 9003
PHP_IDE_CONFIG: serverName=Docker
volumes:
- ./xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
xdebug.ini
xdebug.mode=debug,coverage
xdebug.start_with_request=yes
xdebug.client_host=${XDEBUG_CLIENT_HOST}
xdebug.client_port=${XDEBUG_CLIENT_PORT}
xdebug.ini for php:7.2-fpm-alpine3.8
zend_extension=xdebug.so
[xdebug]
xdebug.cli_color = 1
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.discover_client_host = 0
xdebug.client_host=host.docker.internal
xdebug.ideKey=docker
Example config vscode modify pathMappings
launch.json:
{
"version": "0.2.0",
"configurations": [{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9003,
"log": true,
"externalConsole": false,
"pathMappings": {
"/appdata/www": "${workspaceRoot}/api",
},
"ignore": [
"**/vendor/**/*.php"
]
},
]
}

Related

Configure Xdebug in Docker container

I'm trying to create a docker container with PHP and Xdebug to use step debugging. I use VSCode and somehow this debugger it's not working.
Apparently the Dockerfile is not been executed when I use docker compose up -d command. I assume that is because in the file it has a COPY command to copy a file (called 90-xdebug.ini) from my project to a specific directory. And after the container is up, I check the directory and the file isn't there... then I have to execute the commands in Dockerfile manually.
Anyways, after installing, I know that the installation worked because the xdebug_info() function works. But I don't know why VSCode can't debug it.
My OS is Ubuntu 20.04.5 LTS; Dockerfile, docker-compose.yml and 90-xdebug.ini are in project's root.
My Dockerfile:
FROM php:7.4-apache
COPY 90-xdebug.ini "/usr/local/etc/php/conf.d"
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug
90-xdebug.ini:
xdebug.mode=debug
xdebug.discover_client_host=0
xdebug.client_host=host.docker.internal
docker-compose.yml:
services:
php-apache:
container_name: php-apache
image: php:7.4-apache
build:
context: .
dockerfile: Dockerfile
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
- ./:/var/www/html/
working_dir: /var/www/html/
ports:
- 3003:3003
entrypoint: "php -S 0.0.0.0:3003"
launch.json inside ".vscode" directory:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: 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": 0,
"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"
}
}
]
}
Extra information:
this is my program and i've put a break point on "$var = "txt1" " line, but it runs directly towards "xdebug_info()"
<?php
function fprint(string $str):void{
echo $str;
}
$var = "txt1";
fprint($var);
$var = "txt2";
fprint($var);
$var = "txt3";
fprint($var);
// echo $PHP_INI_DIR;
// echo phpinfo();
xdebug_info();
As you say (in a comment) that xdebug_info() shows that you had an active debugging connection, then that means that the debugger works. What is likely happening here is that:
you don't have any breakpoints set - in which case the debugger never breaks
more likely, that you do have a breakpoint set, but that you don't have a path mapping configured, that maps a path from inside the container (/var/www/html) to your local project route, which you can refer to with "${workspaceRoot}/html". You need to tell VS Code this path mapping by making the following configuration:
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html": "${workspaceRoot}/html",
}
},
I might not have gotten the exact right paths in this configuration. If you tell Xdebug to make a log file (-dxdebug.log=/tmp/xdebug.log and -dxdebug.log_level=10) it will create a log file in your container, where the contents tell you which breakpoint file name is tried to be matched against the files that PHP sees, something like:
[11] [Step Debug] DEBUG: I:
Matching breakpoint '/home/caio/dev/project/html/info.php:1'
against location '/var/www/html/info.php:2'.
If these paths don't match, adjust your path mappings.

VSCode and PHP with XDebug and PHP Debug extension and IISExpress

I am trying to use the PHP Debug extension in VS Code. I have followed the instructions and pasted the phpinfo contents into the wizard and downloaded the appropriate dll. I get the following error
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
php.ini
[XDEBUG]
zend_extension = C:\PHP\ext\php_xdebug-2.9.6-7.4-vc15-nts.dll
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_handler = dbgp
xdebug.remote_host = 127.0.0.1
xdebug.remote_port = 9000
xdebug.remote_mode = req
If I choose the listen option having previousy started IISExpress I get this error
Error: listen EACCES: permission denied 0.0.0.0:9000
at Server.setupListenHandle [as _listen2] (net.js:1211:19)
at listenInCluster (net.js:1276:12)
at Server.listen (net.js:1364:7)
at c:\Users\me.vscode\extensions\felixfbecker.php-debug-1.13.0\out\phpDebug.js:236:24
at new Promise ()
at createServer (c:\Users\me.vscode\extensions\felixfbecker.php-debug-1.13.0\out\phpDebug.js:184:40)
at PhpDebugSession. (c:\Users\me.vscode\extensions\felixfbecker.php-debug-1.13.0\out\phpDebug.js:240:27)
at Generator.next ()
at c:\Users\me.vscode\extensions\felixfbecker.php-debug-1.13.0\out\phpDebug.js:7:71
at new Promise () { code: 'EACCES', errno: 'EACCES', syscall: 'listen', address: '0.0.0.0', port: 9000 }
If I choose the other option and start VS Code first, I get an IISExpress error
Failed to register URL "http://localhost:9000/" for site
"XXXXXXX-0f34f450-230f-4d11-8878-1a90a024dcf6" application "/".
Error description: The process cannot access the file because it is
being used by another process. (0x80070020)
I know port 9000 is not used because if I run this command when nothing is running it shows nothing running
Get-Process -Id (Get-NetTCPConnection -LocalPort 9000).OwningProcess
Clearly both sides can start port 9000 but they are not happy sharing the same port. What am I missing?
My mistake was the site was also running on the port 9000. It needs to be a completely different port to XDebug which listens on its own port.
iisexpress.json
{
"port": 1785,
"path": "C:\\YourSite",
"clr": "v4.0",
"protocol": "http"
}

Issues when Debugging PHP in VSCode using Docker and WSL2

I've been working with VSCode + Docker in Windows for some years now, and managed to have a fully working dev environment without any issues.
Recently i setup a new development environment with WSL2. Moved all my projects, libraries, CLIs, etc, into WSL, using Docker Windows with WSL2 containers and VSCode on Windows with remote connection to WSL. Everything is working very smoothly and i like the fact i can have everything separated.
But recently i came across an issue that i'm unable to solve, i lost the ability to debug PHP files.
I'm using VSCode Remote WSL extension to work on my projects inside WSL, but when i try to debug, nothing happens.
I have tree debugging settings in my VSCode for each dev environment that i use (Windows, MacOS and WSL). All work except for the WSL. When i try to debug with WSL, literally nothing happens, no output erros, no debug console information, nothing...
Here are my VSCode debug settings:
{
"version": "0.2.0",
"configurations": [{
"name": "Listen for XDebug Win10",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/project-a/api": "\\\\wsl$\\Ubuntu\\home\\ubuntu\\PROJECTS\\project-a\\api",
},
"ignore": [
"**/vendor/**/*.php"
]
},
{
"name": "Listen for XDebug MacOS",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/project-a/api": "/Users/ricky/PROJECTS/project-a/api",
},
"ignore": [
"**/vendor/**/*.php"
]
},
{
"name": "Listen for XDebug WSL",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/project-a/api": "/home/ubuntu/PROJECTS/project-a/api",
},
"ignore": [
"**/vendor/**/*.php"
]
},
]
}
What am i doing wrong? Any ideas on how to solve this issue?
### UPDATE: I've changed the original right answer to a new one. Although #romain-prevost's solution worked, I think #dark's approach is wayyy much simpler :)
Forget about the other answers. They are working but too complicated in my opinion.
The problem is, that you can't connect to xdebug.
The solution is to tell xdebug to set remote_host to host.docker.internal. Everything from there is available to localhost. Now you only have to listen inside Visual Studio Code to localhost via hostname.
Et voilà.
Now you can debug things invoked by your browser, inside your phpunit tests or within your commandline scripts.
Complete Example
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"pathMappings": {
"/var/www/html/": "${workspaceRoot}"
},
"hostname": "localhost"
}
]
}
php.ini
[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9000
Update for XDebug 3
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html/": "${workspaceRoot}"
},
"hostname": "localhost"
}
]
}
php.ini
[XDebug]
xdebug.mode = develop
xdebug.start_with_request = yes
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003
I have been struggling with PHP xdebug in Docker using WSL2 as well. It all comes down to the remote host.
What is your xdebug config in php.ini? You should set xdebug.remote_host to your WSL2 local IP address (which you can get in a terminal using hostame -I).
I have tried several times to set the remote host IP address in my Docker—composing a file to pass to the container on startup—but it always fails. Thanks to another StackOverflow answer, however, I have a solution for that too:
In WSL2, set an environment variable for your local IP in your .bashrc file. I have set mine to
export IP=$(hostname -I)
In your Docker compose file for the PHP service, pass the IP address as a new host with the extra_hosts key. For compose v3.2 it is
extra_hosts:
- "docker.host:${IP}"
You can look here for other compose versions: https://docs.docker.com/compose/compose-file/
Finally, edit your php.ini file for xdebug to have this line:
xdebug.remote_host=docker.host
Your container will be able to reach the WSL2 distribution with your docker.host and connect to the port you set for xdebug.
I spent a lot of time figuring this out, mainly because WSL2 was released officially a few days ago and there are not many guides about it.
It was not so complicated in the end, but without the extra_hosts key I could not get the WSL2 IP address to work. It was in the container and in xdebug config, but I always got an error about the resource being unavailable, so don't forget it 😁
The answer by #Romain-prevost is definitely the easiest and best way. If for some reason you can't get that to work, there is another alternative.
host.docker.internal will likely be reachable from within your container, but is a 192.* address by default, and is your Windows-based Docker IP. So you would need to set up forwarding from your Windows host into your WSL2 instance. This is outlined in this script in this issue at Github/WSL. You would simply change the ports to 9000 in that script. You could also use WSL2-Host to have a named entry in your hosts file for that IP and modify the script further to both restrict calling/receiving IPs.

Xdebug on Vagrant Scotchbox running PHP 7.2 - Config

Not a question, but wanted to share with you the config i've used to get xDebug working on Vagrant using Scotchbox with PHP 7.2.
I had issues getting xDebug working with scotchbox and found many pother articles providing suggested configuration but none worked for me.
This was my process:
vagrant ssh
sudo pecl install xdebug. if errors read the advice given.
it says to add ../xdebug.so" to php.ini. copy this line, ie:
zend_extension=/usr/lib/php/20170718/xdebug.so
sudo nano /etc/php/7.2/apache2/php.ini
scroll to bottom > paste:
[XDebug]
sudo nano /etc/php/7.2/apache2/php.ini
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.remote_host = 192.168.33.0
xdebug.remote_port = 9000
xdebug.remote_log = /var/log/xdebug.log
restart apache: sudo service apache2 restart or if using https you will need
sudo a2enmod ssl;sudo service apache2 restart
in visual studio Code > xDebug config was DEFAULT:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/var/www/public/": "${workspaceRoot}"
},
"port": 9000,
"log": true
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
I hope this helps somebody as took me hours to work out :)

Visual Studio Code - Xdebug won't work

In Visual Studio Code (1.9.1) (mac) i have setup the php-debug plugin.
In the debug screen i start 'listen for Xdebug'.
After this i open the index.php on my XAMPP server (local).
But nothing happens.
the blue bar at the bottom of the screen turns orange.
the step over, step into and step out buttons are greyed out.
Also the following error message occurs at the watched variables:
cannot evaluate code without an connection
I try to use breakpoints on the following code:
<?php
$i = 0;
do {
$i++;
if (!($i % 1)) {
echo('<p>$i = ' . $i . '</p>');
}
}
while ($i < 100);
?>
I am using XAMPP and in my php.ini file i use port 9000 for Xdebug.
zend_extension="/usr/local/Cellar/php71-xdebug/2.5.0/xdebug.so"
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote.port=9000
I installed Xdebug using homebrew.
Here is my php info:
phpinfo.htm
Xdebug wizard tells me Xdebug is installed correctly.
my launch.json file looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
Would anyone know what i am doing wrong?
After setting the xdebug.remote_connect_back = 1 in the ini file
as n00dl3 suggested debugging most of the time works, but once in a while i get the following
error in the debug console:
<- threadEvent
ThreadEvent {
seq: 0,
type: 'event',
event: 'thread',
body: { reason: 'exited', threadId: 1 } }
I encountered this problem as well, not with the same environment (NGINX server + php-fpm) but the same symptoms. It turned out to be caused by my xdebug configuration.
How I managed to diagnose it : by writing a simple PHP script for test just like OP did :
<?php
xdebug_info();
By browsing to it, I got a bunch of info on my setup, including :
xdebug.client_host => localhost
xdebug.client_port => 9003
whereas my xdebug was listening on port 9900 (default being 9000).
Steps to fix : just add the following lines to your php.ini or xdebug.ini (wherever the rest of your xdebug configuration lies) :
# This should match your xdebug.remote_host
xdebug.client_host=localhost
# This should match your xdebug.remote_port
xdebug.client_port=9900
xdebug.mode=debug
Then rerun a debug session in VScode, add some breakpoints, and re-browse to your script : my VScode window popped up, the execution was paused on the breakpoint and the variables where accessible in the debug pannel just like expected.
EDIT : don't forget to also :
add xdebug.mode=debug to your php.ini
restart webserver and php-fpm services.
It seemed the server root needed to be set
in the launch.json with localSourceRoot like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000,
"log": true,
"localSourceRoot": "http://127.0.0.1/public_html/"
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
Now breakpoints are working like they should.
In my case, these two lines were missing from the php.ini file.
xdebug.mode = debug
xdebug.start_with_request = yes
Check xdebug.client_port
in page xdebug_info(); or phpinfo();
Config same port in launch.json of vscode
I just had this problem too.
Somehow, someday, without realizing it, I got version 3 of xdebug installed, and a lot of conf param name changed , see this SO question
So verifying the xdebug version with phpinfo for example can be worth a shot.
I am working with VSCODE devcontainer and I fix with the config below:
My launch.json for VSCODE
{
"version": "0.2.0",
"configurations": [
{
"name": "Xdebug",
"type": "php",
"request": "launch",
"port": 9003
}
]
}
I use Dockerfile with a RUN below to install xdebug:
RUN pecl install xdebug && docker-php-ext-enable xdebug
I find my xdebug config file in /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
I edit the file as below:
zend_extension=xdebug
[xdebug]
xdebug.mode=debug
xdebug.client_host=host.docker.internal
xdebug.start_with_request=yes
xdebug.idekey=VSCODE
Or you can add it in Dockfile like below:
RUN echo ' \n[xdebug] \n\
xdebug.client_host=host.docker.internal \n\
xdebug.mode=debug \n\
xdebug.start_with_request=yes \n\
xdebug.idekey="VSCODE" \n\
\n' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
mode This setting controls which Xdebug features are enabled. We’ve set develop to enable development aids, such as getting better error messages, and debug to enable step debugging.
client_host This setting tells Xdebug the IP address or hostname of the machine that is running your text editor or IDE.
start_with_request This setting determines whether a function trace, garbage collection statistics, profiling, or step debugging are activated at the start of a PHP request. Setting it to yes instructs Xdebug to always initiate a debugging session.

Categories