XDebug with PHPStorm can do step-debugging on remote sessions started from the command line on a remote machine. You just have to set up a couple of environment variables, map the remote code to the local code that PHPStorm has at its disposal, and tunnel the xdebug connection to your workstation.
Note: If you just want to debug a PHP script (or drush command) on the local machine, that's much easier. Just enter PHPStorm's Run/Debug configuration and create a new "PHP Script" configuration.
Overview of Setup
- We'll create a PHPStorm project that contains all the code we want to debug on the remote machine. This can be done via source control with matching code, by mounting the remote directory to your local machine, or any way you want.
- Create a mapping from server side directories to PHPStorm-side code (A "Server" configuration in PHPStorm)
- Use environment variables on the remote machine to tell xdebug what to do with the debugging session
- Tunnel the Xdebug TCP connection if necessary.
- Make PHPStorm listen for a connection
- Create a breakpoint somewhere early in the execution path
- Run the command-line tool on the remote server.
- On the remote server install xdebug and set xdebug.remote_enable=1 In your xdebug.ini (or php.ini). For complete details see Remote Drupal/PHP Debugging with Xdebug and PHPStorm.
- Open your project/directory in PHPStorm; it must have exactly the same code as is deployed on the remote server. (You can optionally mount the remote source locally and open it in PHPStorm using sshfs or any other technique you want, see notes below.)
- If you're debugging drush, you probably need to copy it into your project (you don't have to add it to source control). PHPStorm is quite insistent that executing code must be found in the project.
- Create a debug configuration and a "Server" configuration in your project. The Server configuration is used to map code locations from the server to your PHPStorm code. Run->Edit Configurations, Create PHP Web App, Create a server, give the server a name. Click "Use path mappings" and configure the mappings from your project to the remote server's code. (See )
- If your remote server cannot directly create a tcp connection to your workstation, you'll have to tunnel port 9000 to your local machine.
ssh -R 9000:localhost:9000 your_account@remote_server.example.com- For more details and debugging, see
- Click the "Listen for PHP Debug Connections" button in PHPStorm. I call this the "unconditional listen" button, because it makes PHPStorm listen on port 9000 and accept any incoming connection, no matter what the IDE key. See Remote Drupal/PHP Debugging with Xdebug and PHPStorm
- In PHPStorm, set a breakpoint somewhere that your PHP script is guaranteed to hit early in its execution. For example, if you're debugging most drush actions, you could put a breakpoint on the first line of drupal_bootstrap() in includes/bootstrap.inc.
- If the computer is not reachable from the server, you will need to tunnel the connection from the server to your workstation.
ssh -R 9000:localhost:9000 firstname.lastname@example.orgFor more details and debugging, Remote Drupal/PHP Debugging with Xdebug and PHPStorm
- In your command-line shell session on the remote server set the environment variable XDEBUG_CONFIG. For example,
export XDEBUG_CONFIG="idekey=PHPSTORM remote_host=172.16.1.1 remote_port=9000"(Note that port 9000 is the default both for xdebug and for PHPStorm.) If you're tunneling the connection then remote_host must be 127.0.0.1. If you're not tunneling, it must be the reachable IP address of the machine where PHPStorm is listening.
export PHP_IDE_CONFIG="serverName=yourservername"- the serverName is the name of the "server" you configured in PHPStorm above, which does the mapping of remote to local paths.
- On the command line run the command you want to run. For example
drush cc allor
php /root/drush/drush.php status
- If all goes well you'll stop at the breakpoint. You can step-debug to your heart's content and see variables, etc.
- I've had the best luck actually copying drush into my codebase so that its mappings and Drupal's mappings can be in the same directory.
- Once you have the environment variables set you can either use the "drush" command (which must be in your path) or use "php /path/to/drush/drush.php" with your drush options. Make sure you're using the drush that's mapped as a part of your project.
Notes and resources
- We set the xdebug.remote_host in the XDEBUG_CONFIG environment variable; it could also have been configured in the xdebug.ini as
xdebug.remote_host=myworkstation.example.com. (Note that the default is "localhost", so if you're tunneling the connection you don't actually have to set it.)
- Full details about remote debugging on xdebug.org
- Debugging: Make sure that only PHPStorm is listening on port 9000. If something else (most notoriously php-fpm) is listening there, you'll have to sort it out. PHPStorm is happy to listen on another port, see the preferences, and you'll need to change your environment variable to something like
export XDEBUG_CONFIG="idekey=PHPSTORM remote_host=172.16.1.1 remote_port=9999
- You may want to use sshfs or some other technique to mount the remote code to your local machine.
sshfs email@example.com:~/drush /tmp/drushwould mount the contents of drush in the remote home directory to /tmp/drush (which must already exist) on your local machine. It must be writeable for PHPStorm to work with it as a project, so you'll have to work that out.
- The article that taught me everything I know about this is Command-line xdebug on a remote server. Thanks!