Skip to content

Instantly share code, notes, and snippets.

@aduartem
Last active May 21, 2022 19:37
Show Gist options
  • Save aduartem/55e8f9f516c317310ec9ec84689eb959 to your computer and use it in GitHub Desktop.
Save aduartem/55e8f9f516c317310ec9ec84689eb959 to your computer and use it in GitHub Desktop.
Debug de código PHP con Xdebug y Netbeans

Debug de código PHP con XDebug y Netbeans

Requisitos

  • Tener instalado php
  • Tener instalador un web server (Apache o Nginx)

Mac OS X - El Capitan

Instalar Netbeans con soporte para PHP

Link de Descarga

Configurar Xdebug en Netbeans

Ir a preferencias (cmd + ,) -> Click en la pestaña PHP -> Click en la pestaña Debugging

  • Cambiar el valor de "Debugger Port" por 9001
  • Desmarcar el checkbox "Stop at First Line"

Instalar Xdebug

Para PHP 5.3

$ brew install php53-xdebug

Para PHP 5.4

$ brew install php54-xdebug

Para PHP 5.5

$ brew install php55-xdebug

Para PHP 5.6

$ brew install php56-xdebug

Configurar Xdebug

Vamos a editar el archivo php.ini.

Usualmente si instalamos php utilizando homebrew, este archivo se debería encontrar en:

/usr/local/etc/php/5.6/

Si no puedes encontrarlo, intenta buscarlo con el siguiente comando desde la terminal:

$ php -i | grep "php.ini"

Esto devolverá por pantalla algo como esto:

Configuration File (php.ini) Path => /usr/local/etc/php/5.6
Loaded Configuration File => /usr/local/etc/php/5.6/php.ini

Editamos el archivo php.ini:

$ vim /usr/local/etc/php/5.6/php.ini

Y al final del archivo agregamos lo siguiente:

[xdebug]
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9001
xdebug.remote_handler="dbgp"

Finalmente reiniciamos el servidor web.

En el caso de Nginx, debemos reiniciar Nginx y PHP FPM.

$ nginx.restart
$ php-fpm.restart

Si queremos verificar que Xdebug quedo correctamente configurado ingresamos el siguiente comando:

$ php -v

Salida:

$ PHP 5.6.24 (cli) (built: Aug 13 2016 08:51:54)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

Indicando "with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans"

Otra forma de verificar con mayor detalle la instalación de Xdebug es creando un archivo php que invoque la función phpinfo():

<?php
phpinfo();
?>

Luego revisamos desde el navegador la página que genera el archivo. Esta página muestra la configuración del php.ini.

También podemos revisar la configuración por línea de comandos de la siguiente forma:

$ php -i | grep "xdebug"

Ubuntu 16.04

Instalar Netbeans con soporte para PHP

Link de Descarga - x64

Configurar Xdebug en Netbeans

Click en el menú Herramientas -> Click en Opciones -> Click en la pestaña PHP -> Click en la pestaña Debugging

  • Cambiar el valor de "Debugger Port" por 9001
  • Desmarcar el checkbox "Stop at First Line"

Instalar Xdebug

$ sudo apt-get update
$ sudo apt-get install php-xdebug
$ sudo service apache2 restart

Configurar Xdebug

Vamos a editar el archivo php.ini tanto de PHP como de PHP CLI. Desde la terminal revisamos donde se encuentra este archivo:

$ php -i | grep "php.ini"
Configuration File (php.ini) Path => /etc/php/5.6/cli
Loaded Configuration File => /etc/php/5.6/cli/php.ini

Con vim editamos el archivo php.ini de PHP CLI.

$ sudo vim /etc/php/5.6/cli/php.ini

Al final del archivo agregamos las siguientes líneas:

[xdebug]
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9001
xdebug.remote_handler="dbgp"

Y guardamos el archivo.

Luego editamos el php.ini de PHP. Este se encuentra en /etc/php/5.6/mods-available.

$ sudo vim /etc/php/5.6/mods-available/xdebug.ini

Al final del archivo agregamos las siguientes líneas:

xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9001
xdebug.remote_handler="dbgp"

Guardamos los cambios.

Reiniciamos el servidor web Apache:

$ sudo service apache2 restart

Si queremos verificar que Xdebug quedo correctamente configurado ingresamos el siguiente comando:

$ php -v

Salida:

$ PHP 5.6.24 (cli) (built: Aug 13 2016 08:51:54)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

Indicando "with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans"

Otra forma de verificar con mayor detalle la instalación de Xdebug es creando un archivo php que invoque la función phpinfo():

<?php
phpinfo();
?>

Luego revisamos desde el navegador la página que genera el archivo. Esta página muestra la configuración del php.ini.

También podemos revisar la configuración por línea de comandos de la siguiente forma:

$ php -i | grep "xdebug"

Shortcuts

Acción Mac OS X - El Capitan Ubuntu 16.04
Debug project Cmd + F5 Ctrl + F5
Continuar ejecución F8 F8
Paso a paso F7 F7
Ejecutar y salir Cmd + F7 Ctrl + F7
Ejecutar hasta el cursor F4 F4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment