Thursday, 25 June 2015

Remote debugging PHP from phpStorm

Despite all the documentation out there, it seemed to take me longer than it should. Although in hindsight, it could have been working originally as phpStorm's indicator that it's debugging is subtle.

Debug session within phpStorm

Two articles to get you started:

To setup follow do the following:
  1. For chrome (probably the easiest), install this plugin: Chrome Plugin
  2. Configure the plugin and select phpStorm as your IDE.
  3. Next sign onto your remote server and enable xdebug module
    1. for cPanel, click select php version (only 5.4 and 5.5 supported)
    2. update version
    3. check the xdebug module
    4. click update
  4. Edit the php.ini, e.g. for cPanel, edit or create: /home/[username]/public_html/.user.ini
    ; Settings for xdebug
    module was already loaded on this machine:
    ; zend_extension=/opt/alt/php54/usr/lib64/php/modules/xdebug.so
    xdebug.remote_host=[your modems IP address]
    xdebug.remote_port=[a port forwarded through your modem to firewall, e.g. 9800]
    xdebug.remote_enable=1
    
  5. Setup phpStorm's port:
    Settings->Languages & Frameworks-->PHP-->Debug Xdebug port = [remote_port] set above
  6. Setup phpStorm remote debug settings:
    1. Run --> Edit configurations
    2. Add --> PHP Remote Debug
    3. Give it a name
    4. Ide key: "PhpStorm"
    5. Servers (select or add via the "...")
      1. Host: your remote server host
      2. Port: http port usually 80
      3. Debugger: xdebug
      4. Check "use path mappings"
      5. Setup any mappings
      6. Click validate
      7. Click close
    6. Click ok
  7. Run --> Start listening for PHP Debug Connections
  8. In the browser, click the debug icon in the address bar and select debug
  9. visit your sites URL
    You may need to go to the site first, the select debug, then refresh the page.
Note: .user.ini is cached by apache, so it may not work instantly.
When it works the page won't seem to load and in phpStorm the debugger tab should show a clickable green arrow ("Resume Program Execution") and it should show the current frame & variables.

I went through a couple of settings to get here so I may have missed a little tweak along the way.

Profiler

These settings were taken from: Diagnosing slow PHP execution with Xdebug and KCachegrind.
/home/[username]/public_html/.user.ini
; Settings for xdebug
; module was already loaded:
; zend_extension=/opt/alt/php54/usr/lib64/php/modules/xdebug.so
xdebug.profiler_output_dir=/home/[username]/php
xdebug.profiler_append=On
xdebug.profiler_enable_trigger=On
xdebug.profiler_output_name="%R-%u.trace"
xdebug.trace_options=1
xdebug.collect_params=4
xdebug.collect_return=1
xdebug.collect_vars=0
xdebug.profiler_enable=0
The relevant .trace file can be downloaded and loaded into phpStorm via: Tools->Analyse Xdebug Profiler Snapshot.
See Analyzing Xdebug Profiling Data for more information.

No comments:

Post a Comment