docker – Xdebug PhpStorm: debugging scripts started with exec("php index.php")-ThrowExceptions

Exception or error:

I’m trying to add debugging to an old project that uses exec() to start a new session asynchronously from within another PHP script:

exec("php /var/www/html/validata/index.php",$result) 

The normal PHP script is fully debugable with Xdebug but the script started with the exec command isn’t because it can’t map from file:///var/www/html/index.php to a local file location since it’s started within CLI shell. The session started this way does trigger the debugger but can’t find the file locally:

Cannot find file '/var/www/html/validata/index.php' locally.
To fix it set server name by environment variable PHP_IDE_CONFIG and restart debug session.

I’ve followed the instructions to add the PHP_IDE_CONFIG to the env. I’ve also added this to the server with 127.0.0.1 replaced with the desktop PC IP address (server is running in a docker container):

export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"

Any pointers are greatly appreciated!

UPDATE

Solution:

Use the cli interpreter set to the docker container (in settings > Languages & Frameworks > PHP > CLI interpreter, add new, select docker and point it to the php binary) so that a debug session can be started with a new debugging configuration. I’ve copied the arguments from the exec command into the new configuration and it can now fully debug the script. I have to prepare a database table to make it fully testable but this is a working solution for me.

Thanks for all the replies!

How to solve:

You can solve the issue about the wrong php.ini file being loaded by specifying it in the command line:

exec("php -c " . escapeshellarg(php_ini_loaded_file()) .
    " /var/www/html/validata/index.php",$result);

Though I doubt that would make much of a difference as far as xdebug is concerned.

I think a better solution would be to just require the file, which would cause xdebug to not become lost when you fork a new process.

require_once "/var/www/html/validata/index.php";

Leave a Reply

Your email address will not be published. Required fields are marked *