Donnerstag, 2. Juni 2016

XDebug: Debug PHP scripts which are executed by Drush within your virtual machine

If you have PhpStorm (2016.1.1) installed on your host system and you want to debug PHP code in your virtual machine, which is executed by Drush (PHP command line application), so you need some configuration which is described below.

Settings in your PhpStorm IDE:

Map your project root folder to the absolute path on the server. Make sure that your webapplication's PHP code (e.g. Drupal) is located in the same folder as Drush. Like in the screenshot below.


Uncheck "Force break at the first line when a script is outside the project":


PhpStorm needs to listen to XDebug connections:


Afterwards you are able to Debug the code within Drush itself. But it is not necessarily needed to have the Drush folder mapped, when you want to only debug your Drupal code.

Install Drush via composer (PHP package manager)

composer require "drush/drush:7.3"
I have choosen a specific Drush version to suit in my webapplication's requirements. Run this command in the folder, which contains also your webapplication, to meet the path mappings:
/project-folder (run the command here)
  /webapplication
  /vendors
Drush is then located at "vendor/drush/drush". Make sure to run the Drush file which is located there by creating an alias in your .bash_profile file:
alias drush='php /var/www/vendor/drush/drush/drush.php'

Configure your PHP CLI (command line) settings, to work with XDebug

xdebug.remote_enable=true
xdebug.profiler_enable=0
xdebug.idekey=PHPSTORM
xdebug.max_nesting_level=256
xdebug.remote_autostart=true (THIS SETTING ENABLES/DISABLES XDEBUG)
xdebug.remote_connect_back=on
xdebug.remote_host=YOUR HOST IP. - E.G: 192.168.0.14

Force XDebug to break, so that you can set the path mapping

Use the following function in your code, to make XDebug connecting back to your IDE. If that works, you need only to set the path mapping within your PhpStorm IDE to be able to debug properly.
xdebug_break();

If everything works

Your IDE looks like below, if you have setup everything properly:


Now you can use the following Drush command to debug your code.
drush php-eval 'my_nice_function()'
Drupal will be booted up and you can inspect your code during the application runtime. This approach is faster then locating a certain page on your Drupal site for finding the entry point to your code or using the Devel module with it's webpage based PHP execution functionality at http://my-website.dev/devel/php.

Tip: Check out the Phing build tool which is written in PHP

I recommend to create a command with your favorite build tool (I recommend https://www.phing.info/) to quickly disable and enable XDebug. So that you execute "xd" for disabling and "xe" for enabling to save time.

Mittwoch, 1. Juni 2016

Screen: Run processes in the background on a Linux server

The program "Screen" can run processes in background on a Linux server.

This starts an new background session with name "your-screen-name"
screen -S your-screen-name
To save the screen and return to the terminal, type Ctrl+A+D (Ctrl+A is the hint for screen that you want to do something and "D" then "d"etaches from the session without stopping it).

Restore your screen :
screen -d -r your-screen-name
List all available screen sessions:
screen -ls 
Type screen -list or -ls to identify the detached screen session.
~$ screen -list
    There are screens on:
         20751.Melvin_Peter_V42  (Detached)  
Note: 20751.Melvin_Peter_V42 is your session id.

Quit a screen session:
screen -X -S [session id you want to kill] quit
Or just type
exit
if you are attached in your screen session.