Use Xdebug with dockerized PHP project - php

Recently I've started to work on a new project that is dockerized and has Xdebug module. Also I've checked in container itself and 9000 port is listening and has Xdebug module version 2.9.8
I don't have any PHP or anything installed on my Ubuntu (host system).
In my PhpStorm I set my PHP interpreter manually:
Also set DBGp proxy like
Also created a server:
This is my etc/hosts:
and I call API in Postman with the following address: api.vendet.local/api/v1/test
UPDATED path mapping:
Still not break in any point. What is the problem and how can I do that?

I've checked in container itself and 9000 port is listening
It is IDE that should be listening on port 9000, not the container.
#1: you don't need to use DBGp proxy.
#2: at PHP | Servers you have to enable "Use path mappings".
If I were you, I would delete all the configuration you made in PhpStorm (#1 & #2) and would start from the scratch.
First: make sure to configure XDebug properly in a container, see Configure Xdebug running in a Docker container. Pay attention to the value of xdebug.client_host described in the article.
Then: just run a simple Zero-configuration debugging

Be aware that as long as you are not using network_mode: "host" for the php container localhost / 127.0.0.1 for Xdebug running within the container is not the same as localhost on your Ubuntu host!
To be able to connect to Xdebug from PHPStorm you need to ensure the following:
the debugger is listening on an IP reachable from the host: listening on 127.0.0.1 it would only be accessible from within the container itself. Connecting to 127.0.0.1:9000 on the host will be forwarded to the container, but to the virtual network interface of the container and not to the loopback device. To simply listen on all devices within the container specify 0.0.0.0 as address
path mappings are enabled - since (most probably) the paths of the PHP files within the container are not identical with the paths to the same files on the host.

Related

Debug remote server [PHP app] on my local machine [PhpStorm+Xdebug]

I want to debug remote server (AWS EC2 Ubuntu), where there is a PHP application. I use PhpStorm locally on my Windows machine. I can not debug the app on the server.
I have Googled a lot and found a lot of explanations, but stuck. I have installed Xdebug on the server and locally. I have setup the SFTP connection in PhpStorm, so it has the Server and can synchronize. The PhpStorm->Settings->Languages & Frameworks->PHP->Debug - [Validate] says everything is OK, when I choose Remote Web Server
When I click Start listening for PHP Debug connections in the PhpStorm IDE, I can debug the PHP code with breakpoints. The files are exactly the same as on my server. But nothing happens right now.
First and foremost; xdebug should be able to call back home; ie the local computer running phpStorm.
Here is a quick checklist
can the remote connect to local directly? Can the remote host connect to local hosts xdebug port (generally 9000)?
IF yes;
is xdebug configuration correct? Here is an example for a direct connection between remote and local:
xdebug.remote_enable = 1
xdebug.remote_port = 900
xdebug.remote_connect_back = On
IF No;
there should be a way for remote to connect back to local. One option would be using an ssh tunnel. For that; xdebug configuration on remote should be amended accordingly; like:
xdebug.remote_host=127.0.0.1
xdebug.remote_port = 9001
xdebug now tries to connect its own port; 9001 which is actually a tunnel to your localhost; port 9000
you can create a tunnel with ssh on nix; Putty on win
Also, xdebug log files are indispensible whide debugging problems; be sure that xdebug IS creating logs; check them; example:
xdebug.remote_log = /var/tmp/xdebug.log
After all those are set; activate PHPStorm's listening mode and start debugging.
Also; may be good to remember the documentation

Xdebug with SSH tunnel on Docker for Mac

I was reading a lot of posts from the Docker community recently on how to debug PHP application in the PHPStorm with the Docker for Mac. All of them contains pieces of useful information, but haven’t seen working solution in one place.
Here is what did work for me.
Inside Docker Container
Edit xdebug configuration
# automatically start debugger on every request
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_port=9000
# send all debug requests to 127.0.0.1, remote_connect_back should be turned off
xdebug.remote_connect_back = 0
xdebug.remote_host=127.0.0.1
#log all xdebug requests to see is it working correctly
xdebug.remote_log=/var/log/remote.log
Verify that xdebug works
At this point try to run PHP application. Log should contain such entries for every request:
I: Connecting to configured address/port: 127.0.0.1:9000
I: Connected to client. :-)
If you see something like this in the log, remote_host or remote_connect_back are configured incorrectly.
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 172.18.0.1:9000.
W: Creating socket for '172.18.0.1:9000', poll: Operation now in progress.
E: Could not connect to client. :-(
I've seen situations when Xdebug worked in CLI but not from the browser, when this issue appeared in the log, remote_connect_back=0 fixed it.
sshd configuration
In order to allow ssh tunnelling to the container: edit /etc/ssh/sshd_conf and add:
GatewayPorts yes
Restart sshd if needed (ideally this should be part of the Dockerfile).
On Host Machine
Start reverse SSH tunnel
Run this command and keep it in separate Terminal tab open:
ssh -p {container_22_port} -R 9000:localhost:1111 root#127.0.0.1
where {container_22_port} is the port on host machine mapped to the exdposed 22 port on docker container. 9000 is the port used by Xdebug inside container, 1111 port that will be used by host machine to listen to Xdebug connections.
Test with netcat
At this point you can verify that Xdebug actually passes information from inside docker container to the host machine. Start netcat to see what is sent to the 1111 port and run php application:
nc -l 1111
You should see something like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/magento2/index.php" language="PHP" xdebug:language_version="7.0.12" protocol_version="1.0" appid="1006" idekey="XDEBUG_ECLIPSE"><engine version="2.5.0rc1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2016 by Derick Rethans]]></copyright></init>
Configure PhpStorm
Opne File->DefaultSettings, and there in Languages&Frameworks->PHP->Debug change Xdebug->Debug port to 1111 (the one we used to open ssh tunnel).
PhpStorm should start accepting connections from xdebug at this point.
Are there any concerns with this approach?
I found out that you don't need a SSH tunnel to get it to work.
Xdebug need only to connect out to the running IDE debugger but there might be some default ports already used like the one for FPM (9000).
Inside the container
Set the xdebug options as this:
xdebug.remote_enable = 1
xdebug.remote_host = localhost
xdebug.remote_port = 10000
xdebug.remote_connect_back = 1
Note: if using the nginx-proxy container as reverse proxy set your remote_host as the one you defined in the VIRTUAL_HOST
Using PhpStorm
File -> Settings
Languages & Frameworks
PHP
Servers: add one and set the host as localhost: 80 with Xdebug with Path mapping selected ... map the folder INSIDE the Docker (/var/www/) to the one
Debug: set the Xdebug port to 10000 with the Can accept external connections checked
use xdebug.remote_host=host.docker.internal works everywhere.

Xdebug not working in PHPStorm IDE

I am trying to setup xdebug in PHPStorm IDE and I followed the steps mentioned in one document
I followed this document :"http://www.mysolutions.it/phpstorm-server-xdebug-configuration/".
But I am getting one error "Port 9000 is busy " and also if i run debug ,it is quitting.I will share my config settings
The steps I have done
In my Xdebug.ini
zend_extension="/usr/lib/php5/20090626/xdebug.so"
xdebug.default_enable = 1
xdebug.idekey = "vagrant"
xdebug.remote_enable = 1
xdebug.remote_autostart = 0
xdebug.remote_port = 9000
xdebug.remote_handler=dbgp
xdebug.remote_log="/var/log/xdebug/xdebug.log"
xdebug.remote_host="myip"
In PHP storm
File->settings->PHPservers
Host : Ip for the virtual machine(Ip added in the host file)
Port:80
Debugger:Xdebugger
I checked the checkbox (Use PathMappings)
Under that Project files (Absolute Path on the server : /var/www/myproj)
File->settings->Deployment
Connection:
Type : FTP FTP Host :my virtual machine ip port :80 Root path:/var/www
Mappings:
Local Path: /Users/m1019238/dev/myproject/myproj
Web Path on Server : /var/www/myproj
I will share anything if i missed any settings that i have done other than this. Also very sorry for my english.
I've just change the port on "Build,execution, deployment" -> "Debugger" to something else like 9001 and the put it back to 9000. It was weird because PhpStorm itself was using the port.
For anyone else that may stumble upon this and don't want whack the entire config, you can find your PHPStorm configuration file path here: https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs
Once you locate the config folder, a text search for the used port should locate the file that configures PHP to automatically use the port (assuming phpstorm is the application listening on it).
For me on a mac, it was in ~/Library/Preferences/PhpStorm2017.1/options/other.xml
<application>
<component name="BuiltInServerOptions" builtInServerPort="9000" builtInServerAvailableExternally="true" />
</application>
The built in server defaulted to 9000. Changing this to something else fixed things. Note that for my case, turns out I also could have changed the Build,Execution,Deployment > Debugger > Port option in the IDE itself.
I just figured what was my mistake:
php-fpm was listening on 9000 so be sure you don't have php-fpm running and if so, you'll have to choose between changing your php-fpm port or your xdebug's one.

Auto setting xdebug.remote_host ip address with vagrant/puppet

I'm in the process of setting up a Vagrant environment using puppet for provisioning.
I'm stuck with one issue, I would like xdebug to 'just work' when running vagrant up however I need to specify the host machines ip address in the php.ini file xdebug.remote_host, obviously this is going to be different on each machine the config is used so I would like a way to automatically update that value when issuing vagrant up.
VagrantFile:
config.vm.network :forwarded_port, guest: 9000, host: 9000
.ini settings:
'xdebug.default_enable=1',
'xdebug.remote_enable=1',
'xdebug.remote_handler=dbgp',
'xdebug.remote_host=localhost:9000',
'xdebug.remote_port=9000',
'xdebug.remote_autostart=0',
'xdebug.max_nesting_level=250'
I have also tried it with xdebug.remote_host=localhost
ifconfig results from the vagrant machine:
vagrant#precise64 ~ : ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:cf:f9:89
inet addr:192.168.61.142 Bcast:192.168.61.255 Mask:255.255.255.0
phpinfo()
REMOTE_ADDR 192.168.61.2
REMOTE_PORT 51886
Just to confirm, if I give remote_host my actual ip address I have on my osx host machine, it works correctly.
As mentioned in http://www.xdebug.org/docs/all_settings, you can set option
xdebug.remote_connect_back = 1
So, xdebug will connect back to the host, which requested for web-page, and will ignore option "remote_host".
This solution has one problem: If you enable xdebug for any request, and user, opening web-page doesn't have running xdebug client (waiting for connection from server), and has non-closed 9000 port, the server would wait for a long time (trying to connect to client's xdebug session), before it can finally load page.
I had this problem with windows 7 machines, because it's firewall doesn't actually closes port, and connecting software can't understand, that nobody is listening port.
If this doesn't work:
I had the same situation, then I was need for VirtualBox VM with configuration, that should work on any machine with any IP.
So, I made it this way:
I've created virtual network interface in VirtualBox (I don't know, are there any options for this in vagrant, but it should be), and set it local address to 192.168.100.1,
so, my REAL machine have two addresses: eth0:192.168.1.2, and vboxnet0:192.168.100.1.
I've configured virtual machine with following values: IP=192.168.100.100, Default gateway = 192.168.100.1
Configure my XDebug to remote_ip=192.168.100.1
And now, I have 3 copies of this machine (my copy, and 2 copies used by my co-workers), and it works fine!
So, solution is to set your IP address to some "constant one", just virtually.
In case of a custom vagrant machine running GNU/Linux or using a docker image you can find the ip via the following command:
netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10
To get the ip, tun this command after you run first:
vagrant ssh #login into the machine
BONUS INFO in case you are or wanna run docker inside vagrant
Also an intriguing case is the one that you run php inside a docker container that is spawned inside a vagrant GNU/Linux image. In that case, do not attempt to shawn a shell of a running php image and the run the command above, find the ip that the VM connects back into the host, via running the command:
netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10
In the running VM's shell and NOT on a running docker container (either using docker exec or docker-compose exec). In case of an entrypoint script set an enviromental variable for the XDEBUG ip and autoset the value in case that this one is empty then autoset the ip.

Xdebug for remote server not connecting - netbeans

I'm trying to use XDebug in the following scenario
my computer Windows 7, Netbeans 6.9
Physical Host at computer on the same network (Windows 7) with virtual Box
Virtual CentOS 6.2, with Apache server 2.2.15 and PHP 5.3.3
the PHP code of my website is on a shared folder on CentOS, in /var/www/html/mysite and have separate and can access it by server ip 192.168.1.240
Edited C:\Windows\System32\drivers\etc\hosts 192.168.1.240 mysite
the PHP code is accessible from my Windows host, on \\HostIP\html\mysite, with R/W permissions
I created a Netbeans project on my computer, pointing to \\HostIP\html\mysite. In the project Run configuration, I have the following:
Run as: Remote Web Site
Project URL: http://mysite/
Index file: index.php (does exist in the project)
In the Advanced Run Configuration:
I checked "Default"
I haven't touched the proxy settings
I have the following in the php.ini on my CentOS VM
;extension=xdebug.so
zend_extension="/usr/lib64/php/modules/xdebug.so"
xdebug.remote_enable=on
xdebug.remote_log="/var/log/xdebug.log"
;xdebug.remote_host=192.168.1.31
xdebug.remote_connect_back=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
note: I tried the configurations with extension=xdebug.so and tried commenting xdebug.remote_connect_back=1 with uncomment xdebug.remote_host=192.168.1.31 which is my computer ip address.
so basically i have all the configurations like this image
but still not working! after run the debugger will open this url
http://mysite/index.php?XDEBUG_SESSION_START=netbeans-xdebug
and nothing will happened just netbeans Listing waiting for Xdebug connection
When i want to debug on a remote host i normally have to forward my local 9000 port to the remote server's local 9000 via a ssh tunnel:
ssh -R 9000:localhost:9000 username#remote.example.com
Use bitvise ssh client or putty on windows to get the same effect.
Also in the project settings -> run configuration -> Advanced button
make sure to specify the remote and local full paths the the project files so the debugger can attach (don't worry about the port in this screen leave as standard).
I know this is an old post, but it seemed like the one I ran across the most tonight.
The NAT Network setup is the way to get the least amount of conflict. In the Windows 7 Firewall, setup a custom Inbound Rule to allow inbound traffic to port 9000 for the entire /24 network that the VM is on (192.168.202/24).
My working XDebug setup in the php.ini, using 2.2.7 with Netbeans 8.0.2:
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.idekey="netbeans-xdebug"
xdebug.show_local_vars=0
;xdebug.extended_info=1
xdebug.output_buffering=off
xdebug.remote_log=/var/log/xdebug.log
Lastly, I want to point out that Netbeans listens for the remote address setup in the remote file config. Doing a quick netstat -an showed that Netbeans was listening on port 9000, with a CLOSE_WAIT status on an old address from my initial bridged network setup. Even after changing the Project URL, and the Remote Connection IP address, and closing several xdebug connections, this never changed. Only after closing Netbeans, does the stale connection drop. Once Netbeans is started again, the new remote server URL is used for the remote xdebug connection.
Mention of Netbeans holding a stale state was never discussed, and the ability to stop the xdebug connection from the IDE gives a false sense of resetting the socket.
If it still doesn't work, use the provided debugclient on the VM, visit the project page with "?XDEBUG_SESSION_START=netbeans-xdebug" appended to the index.php URL, and watch for the xdebug info to come in. At least then you know it works locally.
Try setting up any breakpoint to your executed script. Debug session may not show up while ide does not respect "break at first line" flag.
Also check if xdebug.idekey property in php.ini equals to ideKey in Netbeans' settings.

Categories