Debug PHP cli application inside docker with PhpStorm - php

I have troubles with setting up debugging php cli application. I work on Mac OS and I have here Vagrant with Ubuntu inside, and inside this Ubuntu I have docker. So one of the docker containers runs my php application, where the PHP interpreter lives.
Before (when the application was exactly in Vagrant machine) I was using this command to debug my cli applications, but now it does not work.:
export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=192.168.10.10 remote_connect_back=0"
How can I set up PhpStorm to debug my php cli app ?

Inside of your Docker container don't use remote_host. Also, you don't have to expose any additional ports in Docker or in Vagrant.
Here is my xdebug.ini file that works with PHP 5.6
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=0
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.remote_connect_back=1
Make sure that PhpStorm (2016.1 in my case) is configured correctly
Languages & Frameworks -> PHP -> Servers -> localhost -> localhost : 80 Xdebug
Languages & Frameworks -> PHP -> Debug -> Xdebug -> Debug port: 9000
Languages & Frameworks -> PHP -> Debug -> Xdebug -> Can accept external connections
Languages & Frameworks -> PHP -> Debug -> DBGp Proxy -> Port 9000
Once this is done find Listen for debugger connections icon in PhpStorm in the toolbar and click it.
If you want to call it from a command line remember to include XDEBUG_SESSION cookie, i.e.
curl 'http://localhost' -sSLI -H 'Cookie: XDEBUG_SESSION=xdebug'
If you use Firefox install The easiest Xdebug and enable it in the toolbar.

In my case, debugging through web browsers worked well, the problmes came with CLI debugging (phpunit). This is because xdebug get lost with path mappings and you need to explicit tell docker.
You need to tell Docker which server configuration in PHPStorm should use, just export that env variable inside your docker container.
export PHP_IDE_CONFIG="serverName=<server-name>"

Related

PHP - Xdebug 3.0.4 - Step debuger not working

I cant' activate the step debugger of Xdebug 3.0.4 with PHP 7.4.20.
Xdebug features status:
So, my Xdebug configuration is :
[xdebug]
;xdebug.discover_client_host=1
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.remote_handler=dbgp
xdebug.client_host=host.docker.internal
xdebug.client_port=9003
xdebug.max_nesting_level=1000
xdebug.idekey=PHPSTORM
Also, my Xdebug run into a PHP FPM docker container.
If anyone can help me :), thanks.
I couldn't solve the issue directly, but it worked with an SSH tunnel. Although my docker host IP 172.17.0.1 should have been just as good, as nc -vz 172.17.0.1 9003 could easily connect, for some reason Apache+PHP+Xdebug just refused to do it.
So, I connected to my container via ssh -R 9003:127.0.0.1:9003 dev#172.17.0.2 and then changed my xdebug.client_host to 127.0.0.1 instead of where the IDE is running (172.17.0.1). This SSH tunnel will make it such that anything that connects to port 9003 locally to the container will actually go and connect to 9003 from where I initiated the ssh command (which is my host running my IDE).
Then, Xdebug connected and worked fine when I accessed my PHP pages (or even running a CLI php script) without any issues and step debugging worked properly.
I have no idea why this works via SSH tunnel, but I suspect docker networking has some things that I'm not familiar with.

PhpStorm and XDebug on Vagrant Homestead

I'm on Windows and I'm trying to debug a Web application (hosted with Vagrant/Homestead) using PhpStorm through XDebug. I tried many configurations but I can't get things working. The PhpStorm XDebug setup is pretty common.
This is the current xdebug configuration:
vagrant#homestead: /etc/php/7.0/cli/conf.d/20-xdebug.ini
zend_extension=xdebug.so
xdebug.remote_enable = on
xdebug.remote_connect_back = on
xdebug.idekey='PHPSTORM'
In the IDE "Start listening for PHP debug connections" it's turned on. When I attempt to start debugging a new chrome tab opens and the URL ends, for example, with ?XDEBUG_SESSION_START=16588 but the debugger doesn't run and this is what the IDE says:
Also, netstat says the port 9000 is still listening.
Any idea?
You are changing the 20-xdebug.ini file for the Command Line Interface (CLI). Instead you need to change the /etc/php/7.0/apache2/conf.d/20-xdebug.ini for the Apache settings, since you are using Chrome.
UPDATE
I'm not sure of the differences between Apache and Nginx (which may be /etc/php/7.0/fpm/conf.d/20-xdebug.ini), but here is how I got it working with a VirtualBox VM running Apache, on a Windows computer.
Settings for 20-xdebug.ini:
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=192.168.1... (your windows IP address within your network)
xdebug.remote_port=9000
xdebug.autostart=1

Debug PHP in Atom While Using Vagrant

My PHP dev environment is running on a VirtualBox VM and is provisioned via vagrant. How can I use php-debug in Atom on my host machine to debug PHP scripts running on one of my VMs?
Configure xdebug:
Open your php.ini file on your VM and look for the xdebug settings and make sure that you have the following values:
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.remote_mode=req
xdebug.remote_log=/tmp/xdebug.log
xdebug.remote_host=10.0.2.2
xdebug.remote_port=9999
Note: you may have multiple php.ini files, e.g. one for cli, fpm, etc… If you do, you’ll need to make sure that you have the xdebug settings above for all environments where you want to use the debugger, e.g. I had to modify /etc/php5/cli/php.ini to use the debugger on the command line and /etc/php5/fpm/php.ini to use the debugger while running PHP scripts with apache.
Restart your web server or any other PHP related services, e.g.
$ sudo service apache2 restart
$ sudo service php5-fpm restart
Install the php-debug package in Atom:
Go to Atom -> Preferences -> Install, search for php-debug and install the package
Configure php-debug in Atom:
Atom -> Preferences -> Packages, search for php-debug and click
Settings
Set the PathMaps in the form remote;local. The PathMaps
translate the guest/remote path to the local/host path. Let’s assume
that you are debugging foo.php and that it can be found on your VM
at /var/www/mysite/foo.php and on your host box at
/Users/someuser/Documents/vagrant-mysite/foo.php. Your PathMaps
config would then be
/var/www/mysite;/Users/someuser/Documents/vagrant-mysite.
Server Port: 9999
Start debugging:
Open your target source file in Atom, e.g.
/Users/someuser/Documents/vagrant-mysite/foo.php
In the bottom left corner of your Atom screen, click the “PHP Debug” button
Set a breakpoint by clicking immediately left of your target line of code
Visit foo in the browser, e.g. http://example.com/foo.php and this should cause the code to pause in Atom and you should be able to continue debugging
If you configured the php.ini file for your PHP command line settings, you should also be able to debug just by running the script on the guest machine, e.g. php /var/www/mysite/foo.php

PHPStorm CLI debugging on Vagrant instance

I'm trying to get PHPStorm to work with Xdebug to use with PHP scripts (CLI, not web pages!) on a vagrant instance on the same machine as PHPStorm is running on.
In their docs they say this:
To tell the PhpStorm which path mapping configuration should be used
for a connection from certain machine, the value of the PHP_IDE_CONFIG
environment variable should be set to serverName=SomeName, where
SomeName is the name of the server configured in Settings /
Preferences | Languages & Frameworks | PHP | Servers:
Windows: set PHP_IDE_CONFIG="serverName=SomeName" Linux / Mac OS X:
export PHP_IDE_CONFIG="serverName=SomeName"
However, it's unclear where I'm supposed to set this-- is it set somewhere in the PHPStorm app? Is it set as part of the bash profile in the vagrant box? In the vagrant xdebug conf?
A per LazyOne comment, PhpStorm since v8 supports debugging CLI scripts over SSH directly (via Remote PHP Interpreters)-- there you do not need to do anything like that. But you may need to setup SFTP deployment and use that when creating Remote PHP Interpreter (it depends: in some cases IDE cannot use/interpret path mappings from vagrant config file.
See:
https://confluence.jetbrains.com/display/PhpStorm/Remote+debugging+in+PhpStorm+via+SSH+tunnel
https://confluence.jetbrains.com/display/PhpStorm/Working+with+Remote+PHP+Interpreters+in+PhpStorm

Remote PHP Debugging with Netbeans and XDebug

I'm trying to use XDebug in the following scenario
Physical Host on Windows 7, with Netbeans 7.1.1
Virtual guest on Ubuntu, with Apache server and PHP 5.3.10
the PHP code of my website is on a shared folder on Ubuntu, in /var/www/mysite
the PHP code is accessible from my Windows host, on \\guestIP\mysite, with R/W permissions
Website accessible from http://mysite.local.fr, from both host and guest
I created a Netbeans project from my Windows Host, pointing to \\guestIP\mysite.
In the project Run configuration, I have the following:
Run as: Local web server
Project URL: http://mysite.local.fr
Index file: index.php (does exist in the project)
In the Advanced Run Configuration:
I checked "Ask every time" (I also tried using "Do not ask" and starting the browser with the session key myself)
I tried setting '/var/www/mysite' - \\GuestIP\mysite for the mapping (and tried using nothing as well)
I haven't touched the proxy settings
I have the following in the php.ini on my Ubuntu VM
xdebug.remote_enable = on
xdebug.profiler_enable = off
xdebug.profiler_enable_trigger = off
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir = /tmp
;xdebug.remote_host=localhost,<HostIP>, mysite.local.fr
xdebug.remote_connect_back=1
xdebug.remote_handler=dbgp
xdebug.idekey="netbeans-xdebug"
xdebug.remote_mode=req
None of this works, Netbeans does not stop at any breakpoint from Windows.
Debugging directly from my VM with Netbeans works fine.
Can someone tell me how to get my debugger to work remotely from Windows?
Thanks
Sorry, i can't comment anymore. #David #JamesB41: I've been looking for this too. My setup is a Windows 7 host with NetBeans 7.1, and an Ubuntu VM in VirtualBox. I have the NetBeans project set up as a remote project, uploading and downloading using SFTP.
The following setup works for me, just use your host's IP as remote_host, and make sure the VM can see it.
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=10.217.165.193
xdebug.remote_port=9000
xdebug.remote_log="/tmp/log/xdebug.log"
NetBeans will stop at the entry point breakpoint (if you have the option set in PHP->Debugging). But, it won't stop on NetBeans-created breakpoints, because its running off the VM's files. You can use xdebug_break() though, and it will show stack and variables. It will stop at NetBeans breakpoints and highlight if you map the folders correctly in project config > Run Config > Advanced. Awesome. I am complete.
(The connect_back config didn't seem to help, possibly because $_SERVER['REMOTE_ADDR'] isn't populated.)
Go through the below document for remote debugging using NetBeans. Very helpful.
http://stuporglue.org/setting-up-xdebug-with-netbeans-on-windows-with-a-remote-apache-server/
In know this is old but a good reminder. Make sure if you are using nat in virtualbox that you set port forwarding on the xdebug port back to the local machine typically 9000 by default.
Another option is to have the VM configured to speak to its own localhost:9000 (default for xdebug) and then ssh in from host to VM and establish a port tunnel for that port back to the host machine. Then your host machine debugger simply connects to localhost:9000 and everything should work exactly as if they were running on the same machine.
See: Multiple users XDebug and PHP Debugging
Debugging remotely with Linux + Apache + PHP + xdebug + NetBeans (SFTP)
I've got the following setup and it works.
Host PC (client)
Linux Debian Jessie **
NetBeans version 8.0.2
NetBeans has a project created as "PHP Application from Remote
Server", that has to be debugged
NetBeans connects to Remote Server using SFTP
IP (for example): 192.168.0.2
** I am aware that the OP question mentions Windows, but regarding that the main setup to solve this should be made on the VM, I hope this helps somebody anyway.
Guest PC (server) / Virtual Machine
Linux Debian Jessie
Apache
PHP 5.6
xdebug 2.2.5
IP (for example): 192.168.0.1
The following steps should be made on the "Guest PC (server) / Virtual Machine"
install xdebug sudo apt-get install php5-xdebug
edit /etc/php5/apache2/php.ini,
add these lines right before the [Date] AND change xdebug.remote_host to reflect your "Host PC (client)" IP:
[debug]
; Remote settings
xdebug.remote_autostart=off
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=192.168.0.2
xdebug.remote_port=9000
restart apache sudo service apache2 restart
Reference
How to configure XDebug - Remote Debuging http://wiki.netbeans.org/HowToConfigureXDebug#Remote_Debuging
you need to have correctly configured property xdebug_remote.host on
remote machine. The IP address of local machine has to be defined in
this property. For example, you want to debug your source code on
remote machine 192.168.0.1 using Netbeans installed on 192.168.0.2.
You need to change xdebug.remote_host on 192.168.0.1 to
xdebug.remote_host=192.168.0.2. If doesn't work verify you have port
configured in xdebug.remote_port open on both machines.
If all seems to be correct but you still get "Waiting For Connection" from inside netbeans you should try in your php.ini settings
xdebug.remote_connect_back=on
as it allows connection from ANY ip or look at a lot more info on the issue http://www.devside.net/wamp-server/netbeans-waiting-for-connection-netbeans-xdebug-issue

Categories