How to debug php artisan serve in PHPStorm?

24,642

Solution 1

Debugging using php artisan serve does not work unless you have enabled debugging in ini file.

@Bogdan pointed out the reason. artisan serve will call PHP Built-in Web Server but does not pass on the php command line options (named interpreter options in PHPStorm).

i.e. if you execute from command line:

$ php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 artisan serve

Then these options given by -d are not passed to called PHP Built-in Web server. You can see the calling of built-in server here.

Workaround in PHPStorm is to create a Run configuration that calls PHP Built-in Web server directly. Instructions:

  1. Open Run -> Edit Configurations...
  2. Create new 'PHP Built-in Web Server'
  3. Set values:
  • Host: localhost
  • Port: 8000
  • Document root: select Laravel's public catalog/directory
  • Check Use route script and select server.php in Laravel projects root directory.
  • Interpreter options: -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1
  1. click OK and run.

Now the PHPStorm will execute same command as php artisan serve does with additional interpreter options. Actually the php artisan serve only purpose is to append the server.php to PHP Built-In Web Server. server.php just emulates Apache's mod_rewrite functionality.

Update: Good reminder from @attila-szeremi: make sure "Start Listening for PHP Debug Connections" is enabled which you manually need to do if you don't run a PhpStorm configuration with "Debug"

Solution 2

I don't use phpstorm, but perhaps the solution that I use for debugging in netbeans will prove useful.

artisan serve uses a different ini file from the one loaded by your web container

Find this by typing

php --ini

On my ubuntu box it's located at

Loaded Configuration File:         /etc/php/7.0/cli/php.ini

Edit the ini for your cli environment and use the same configuration you used to enable it for your web container;

Example...

[Zend]
zend_extension=/usr/lib/php/20151012/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000

The only caveat for this, is that as long as you have this configured, it will impact other things that you use php cli for.

Additional note

If you want your debug session to always start automatically (instead of initiating a remote debug via URL request parameter XDEBUG_SESSION_START=name, for example, when debugging CLI stuff), you can set XDEBUG to always start a remote debugging session with this additional configuration;

xdebug.remote_autostart = 1

See https://xdebug.org/docs/all

Normally you need to use a specific HTTP GET/POST variable to start remote debugging (see Remote Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.

Solution 3

So, after going through Jeffry's Be Awesome in PhpStorm, I had been stuck in a similar situation to the OP (in the chapter regarding Xdebug and Laravel). I was setting up the breakpoints, but PhpStorm was not able to break the execution according to those points. And, to be honest, Josh's answer here somewhat helped me to understand the problem, but was not clear enough for me. So, I went out and found the solution myself by hit and trial method, the final solution of which I want to share. May be it will come in handy for some folks still wondering.

Firstly, as OP said/guessed, php artisan server does use another server. It is PHP's built in web server plus the server.php router file. And the configurations it uses are the CLI configurations. Try php --ini to find all the included configurations.

In my case (Ubuntu 17.10), the Xdebug configuration file being included was from the location /etc/php/7.2/cli/conf.d/20-xdebug.ini. I added the second line to enable remote xdebug and now the file looks like this with total 2 lines:

zend_extension=xdebug.so xdebug.remote_enable=1

Now for configuring PhpStorm:

  1. Run > Edit Configurations and give any name or just leave it as Unnamed
  2. Click ... after the Server: option
  3. Click + and give any name (for example Laravel App)
  4. Set Host as localhost, Port as 8000 and Debugger as Xdebug and leave any other check marks unchecked!
  5. Click Apply / OK
  6. From the drop down list of Server: select the one we just set up.
  7. Set the Start URL: the URL where you want to start debuging (for example \user)
  8. Select the browser you prefer.
  9. Click Apply / OK

Please remember that the debugger will only work after the actual web server starts, which is the server that starts with php artisan server command.

Now, start the web server ie. php artisan serve and in PhpStorm click Run > Debug 'your-debug-config', and everything should work fine!

Solution 4

Part 1 Run > Edit Configurations

choose PHP-BuiltIn Web Server and just set the Host: localhost Port: 8000 // or whichever you are using

enter image description here

Click OK

Part 2 Install Xdebug helper Chrome extension

enter image description here

In Chrome browser there should be a bug icon in top right corner Click on it and choose Debug so that the gray bug icon changes the color to green

enter image description here

Now when you run php artisan serve and set a debug checkpoint

enter image description here

and click on phone icon to "Start listening for Xdebug connection"

enter image description here

it should catch your debug checkpoint

Solution 5

Xdebug 3.0 update

Working with XDebug 3.0 is a big easiness now. How I switched on in phpstorm.

My php.ini configuration:

zend_extension = php_xdebug-3.0.0-7.4-vc15-nts-x86_64.dll
xdebug.client_port = 9000
xdebug.mode = debug

Install proper Xdebug extension for your OS: https://xdebug.org/docs/install

Settings in PHPStorm (my version 2020.2.3):

  • Start listening for PHP Debug Connections enter image description here
  • Managing first connection to server.php after running php artisan serve and first debugging

Browser

Share:
24,642
chemitaxis
Author by

chemitaxis

Updated on August 17, 2021

Comments

  • chemitaxis
    chemitaxis over 2 years

    I am using PHPStorm for develop my PHP web pages. All work fine with my Apache Server, XDebug, and a simple web PHP project. No problem.

    But, when I try to debug a Laravel 5.1 Web Project using php artisan serve, I can't debug the breakpoints. It's like the php artisan serve use another server...

    And on my PHPStorm, I always see:

    Waiting for incoming connection with ide key '(randomNumberHere)'

    I have configured all in PHPStorm (enabling remote debug, correct port, etc.), and with "normal" PHP projects all works fine.

    Can someone tell me if I need to change something?

    Thanks!

  • Haris ur Rehman
    Haris ur Rehman about 6 years
    Ok this approach worked, so now instead of running the command php artisan serve from terminal, I just click the Run button in PHPStorm and am able to break at breakpoints.
  • Attila Szeremi
    Attila Szeremi almost 6 years
    Please include a reminder to make sure "Start Listening for PHP Debug Connections" is enabled which you manually need to do if you don't run a PhpStorm configuration with "Debug".
  • Jay Chakra
    Jay Chakra almost 5 years
    use route*r* scripts
  • Cesar Martinez Dominguez
    Cesar Martinez Dominguez over 4 years
    where is "Laravel's public catalog"?
  • Cesar Martinez Dominguez
    Cesar Martinez Dominguez over 4 years
    When I do this, then Telescope assets are not loaded. Works only when I run php artisan serve in the terminal... any idea how to fix this?
  • MarijnK
    MarijnK over 4 years
    This worked for me on Windows (XAMPP) but I had to format the command with a space after each -d option for it to work: php -d xdebug.remote_enable=1 -d xdebug.remote_autostart=on -d xdebug.remote_mode=req -d xdebug.remote_port=9001 -d xdebug.remote_host=127.0.0.1 -f artisan serve
  • raigu
    raigu over 4 years
    @MarijnK, the difference is in xdebug.remote_autostart=on. It means connect always to remote listener. If it is turned off, then request must have additional XDEBUG_SESSION_START parameter. For HTTP request there are extentions for Chrome and Firefox to automate turning on or off the debug mode. I think your way is more convenient.
  • Bruno Luiz K.
    Bruno Luiz K. almost 3 years
    for xdebug 3+ use this Interpreter options: -dxdebug.mode=debug -dxdebug.start_with_request=trigger -dxdebug.client_port=9003 -dxdebug.client_host=127.0.0.1
  • Kenana Reda
    Kenana Reda over 2 years
    You save my day! Thanks!
  • Cui Qing
    Cui Qing over 2 years
    for xdebug 3.0 Use xdebug.mode=debug with xdebug.start_with_request=yes.