To set up XDebug with Laravel Sail and PhpStorm, you need to ensure that your Docker container is properly configured to communicate with PhpStorm. Here’s a step-by-step guide to help you get it working:
-
Update your
.envfile: Add the following lines to your.envfile to enable XDebug in Laravel Sail:SAIL_XDEBUG_MODE=debug,develop SAIL_XDEBUG_CONFIG="client_host=host.docker.internal" -
Rebuild your Sail containers: After updating the
.envfile, you need to rebuild your Sail containers to apply the changes:./vendor/bin/sail down ./vendor/bin/sail build --no-cache ./vendor/bin/sail up -d -
Configure PhpStorm:
- Open PhpStorm and go to
Preferences>Languages & Frameworks>PHP>Debug. - Set the
Debug portto9003(default for XDebug 3). - Ensure that
Can accept external connectionsis checked.
- Open PhpStorm and go to
-
Set up a PHP Remote Debug Configuration:
- Go to
Run>Edit Configurations. - Click the
+icon and selectPHP Remote Debug. - Name your configuration (e.g.,
Sail Debug). - Set the
Serverto the one you configured earlier or create a new one:- Click the
...next to theServerdropdown. - Click the
+icon to add a new server. - Name the server (e.g.,
Sail). - Set the
Hosttolocalhost. - Set the
Portto80(or the port your Sail application is running on). - Set the
DebuggertoXdebug. - Click
OKto save the server configuration.
- Click the
- Set the
IDE keytoPHPSTORM.
- Go to
-
Set up path mappings:
- In the same
PHP Remote Debugconfiguration, click onPath mappings. - Map the project root to
/var/www/html(the default web root in the Sail container).
- In the same
-
Start listening for debug connections:
- In PhpStorm, click the
Start Listening for PHP Debug Connectionsbutton (the phone icon in the top right).
- In PhpStorm, click the
-
Set a breakpoint:
- Open a PHP file in your project and set a breakpoint by clicking in the left margin next to the line number.
-
Trigger the debugger:
- Make a request to your application that will hit the breakpoint you set.
If everything is configured correctly, PhpStorm should stop at the breakpoint, allowing you to debug your application.
Here’s a summary of the key configurations:
.env file:
SAIL_XDEBUG_MODE=debug,develop
SAIL_XDEBUG_CONFIG="client_host=host.docker.internal"
PhpStorm Debug Configuration:
- Debug port:
9003 - Server host:
localhost - Server port:
80 - Path mappings: Project root ->
/var/www/html
By following these steps, you should be able to get XDebug working with Laravel Sail and PhpStorm. If you encounter any issues, double-check each configuration step and ensure that your Docker containers are properly rebuilt.