Debugging with Xdebug and PHPStorm
This site uses Lando’s pantheon
recipe. Getting Xdebug and PHPStorm to talk to each-other is a bit different for the pantheon
recipe than for the drupal8
recipe. The instructions here should work for both Drupal’s front-end (i.e.: from a web browser) and CLI scripts (i.e.: drush, behat).
Last tested on 2021-08-04 with Lando v3.1.4, PhpStorm 2021.2, PHP 7.4.16, Xdebug v3.0.4 running on Linux 5.11.0-25 / Ubuntu 21.04 hirsute.
Set up Lando
1. Add or inspect a .lando.local.yml
at the root of the repo, and add this if it’s missing.
2. Run lando rebuild -y
to re-create the appserver with xdebug enabled, and the new environment variables set.
Set up PHPStorm
- Run
lando php --version
and note the major PHP version (e.g.: 7.3)
- Go to Settings -> PHP (i.e.: the root of the PHP settings). Set the following:
- PHP language level = (set to the major PHP version)
- CLI Interpreter =
<no interpreter>
- Include path:
- Remove all other paths if they exist.
- Add
~/.lando/config/pantheon
- Click “Apply” to save your changes.
- Go to Settings -> PHP -> Servers
-
Remove other servers if they exist
-
Add a new server:
- Name =
appserver
- Host =
settlenet.lndo.site
(or whatever lando rebuild
gives you), port = 80
, debugger = Xdebug
- Check
Use path mappings
- Add the following mappings. If the Pantheon path isn’t there, navigate to File -> Open…, open that path, and attach it to the current session. (You should be able to add the entry then.)
File / Directory |
Absolute path on the server |
$path_to_repo |
/app |
~/.lando/config/pantheon |
/srv/includes |
-
Click “Apply” and/or “OK” to save your changes.
- Go to the Run menu -> Edit configuration:
- Click “+” to add a Run/Debug configuration, and choose “PHP Web Page”:
- Name =
settlenet
- Server =
appserver
- HTTPS = (check)
- Start URL =
/
- Click “Apply” and/or “OK” to save your changes.
Debugging
- When you’re ready to start debugging, then:
- Make sure you have a breakpoint set,
- Make sure breakpoints aren’t muted,
- Make sure PHPStorm is not listening for [arbitrary] debug connections (Run menu -> “Stop Listening for PHP Debug Connections” - if you see “Start Listening…” then it’s turned off) - the next step will tell PHPStorm to only listen for Xdebug connections from the
settlenet
appserver.
- Go to Run -> Debug ‘settlenet’. A Debug pane will appear. The icon beside “settlenet” should have a little green dot in the bottom-right to indicate it is actively listening.
- This should automatically load the front page with debugging enabled. Note the
?XDEBUG_SESSION_START=NNNNN
(where NNNNN
is an arbitrary number, usually 5 digits), or copy it to a temporary file on your workstation.
- Navigate in the tab that PHPStorm started to the page you want to trigger the breakpoint on. Or, if you want to use a different browser or container, navigate to the page, then paste the
?XDEBUG_SESSION_START=NNNNN
you noted earlier and press Enter to reload the page. If applicable, do whatever you need to do in order to trigger the breakpoint (e.g.: submit a form, start a batch process, etc.).