Faster Debugging with Lando and xdebug
I’ve been struggling with the speed (or lack thereof) of Lando for Drupal sites. I have suspected xdebug was part of the issue. Sure enough, disabling xdebug speeds up the lando instance considerably. Re-enabling it slows it right back down again. I use xdebug often, so having to lando rebuild to turn xdebug on and off is not an option. I wondered if there’s a way to leave the extension enabled but only use it when needed.
While researching config directives like xdebug.remote_enable and xdebug.remote_autostart, I came across this issue thread on the github Lando project: Allow faster xdebug on/off toggling
The title sounds promising, right?
Within this thread I found a tip that ought to be in the official Lando documentation. As it turns out, it’s quite simple to turn xdebug on and off at will. And, using Lando tooling commands it’s as easy as
lando xdebug-on or
lando xdebug-off. Here’s the post: https://github.com/lando/lando/issues/1668#issuecomment-507191275
Sample Lando File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# This will be used in the web server's URL. # Use only numbers, letters and hyphens. name: mysite # drupal9 || drupal8 || drupal7 || lamp recipe: drupal8 # Recipe configuration config: # PHP Version (as string). php: '7.2' # Use only with drupal7 and drupal8 recipes. # nginx || apache via: nginx # Path from app root (comment-out if files are at root level) webroot: web # Setup PHPStorm "server" as shown here: # https://docs.lando.dev/guides/lando-phpstorm.html # true || false xdebug: false config: # See chosen recipe's official documentation for # php, mysql, apache, nginx, etc. config file options. # Add a php.ini file for php overrides. # You may have to `lando restart` to activate changes. php: lando/php.ini # Service overrides. # Use `lando info` to see which services are defined by the chosen recipe. services: appserver: build: - composer install overrides: environment: DB_USER: main_user DB_PASSWORD: main_pass DB_NAME: main_db DB_PORT: 3306 PHP_IDE_CONFIG: "serverName=appserver" DRUSH_OPTIONS_URI: "https://mysite.lndo.site" database: # You can connect externally via "external_connection" info from `lando info`. portforward: true type: mysql:5.7 creds: # These credentials are used only for this specific instance. # You can use the same credentials for each Lando site. user: main_user password: main_pass database: main_db # Commands. Execute with: lando <command> tooling: phpunit-local: service: appserver description: Runs phpunit with config at web/sites/default/local.phpunit.xml cmd: /app/vendor/bin/phpunit -v -c /app/web/sites/default/local.phpunit.xml xdebug-on: service: appserver description: Enable xdebug for nginx. cmd: docker-php-ext-enable xdebug && pkill -o -USR2 php-fpm user: root xdebug-off: service: appserver description: Disable xdebug for nginx. cmd: rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && pkill -o -USR2 php-fpm user: root |
If you’re using Apache instead of Nginx:
1 2 3 4 5 6 7 8 9 10 11 |
xdebug-on: service: appserver description: Enable xdebug for apache. cmd: docker-php-ext-enable xdebug && /etc/init.d/apache2 reload user: root xdebug-off: service: appserver description: Disable xdebug for apache. cmd: rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && /etc/init.d/apache2 reload user: root |