Cómo habilitar el módulo Xdebug integrado de ServBay
Como una potente herramienta de desarrollo web integrada, ServBay incluye un módulo Xdebug, y su proceso de habilitación es muy sencillo. Xdebug es una herramienta de depuración y análisis para PHP que ayuda a los desarrolladores a depurar código, realizar análisis de rendimiento y verificar la cobertura de código, siendo una herramienta esencial para desarrollar y mantener aplicaciones en PHP.
Introducción al módulo Xdebug
Xdebug es una extensión PHP potente utilizada para depurar y analizar código PHP. Ofrece una amplia gama de funciones de depuración, incluyendo depuración de puntos de interrupción, rastreo de pila, vigilancia de variables, entre otros. Además, Xdebug también soporta análisis de rendimiento y verificación de cobertura de código, ayudando a los desarrolladores a optimizar el rendimiento del código y asegurar la calidad del mismo.
Características principales
- Depuración de puntos de interrupción: Xdebug permite establecer puntos de interrupción en el código y depurarlo línea por línea, facilitando la identificación y corrección de errores.
- Rastreo de pila: En caso de errores, Xdebug puede generar información de rastreo de pila detallada, ayudando a los desarrolladores a localizar rápidamente el problema.
- Análisis de rendimiento: Xdebug proporciona funciones de análisis de rendimiento, generando informes detallados que ayudan a los desarrolladores a optimizar el rendimiento del código.
- Cobertura de código: Xdebug admite la verificación de cobertura de código, ayudando a los desarrolladores a garantizar que las pruebas cubran todas las rutas del código.
- Soporte para entornos de desarrollo integrados (IDE): Xdebug se puede integrar con diversos entornos de desarrollo integrados, como PHPStorm y Visual Studio Code, ofreciendo una experiencia de depuración conveniente.
Versión del módulo Xdebug integrado con ServBay
ServBay soporta múltiples versiones de PHP y para cada versión incluye un módulo Xdebug preinstalado. Las versiones específicas son:
- PHP 5.6, 7.0, 7.1: Xdebug 2.5.5
- PHP 7.2, 7.3, 7.4: Xdebug 3.1.6
- PHP 8.0, 8.1, 8.2, 8.3: Xdebug 3.3.1
- PHP 8.4: Xdebug 3.4.0-dev
Aviso
Cada versión de PHP tiene un puerto Xdebug independiente, asociado al número de versión, por ejemplo, el puerto Xdebug de PHP 7.2
es 39072
y el de PHP 8.3
es 39083
.
Cómo habilitar el módulo Xdebug
Por defecto, el módulo Xdebug está deshabilitado. Habilitar el módulo Xdebug es muy sencillo, solo hay que modificar el archivo de configuración del PHP correspondiente. Los pasos detallados son los siguientes:
Paso 1: Ubicar el archivo de configuración
Primero, localiza el directorio conf.d
de la versión de PHP correspondiente. Por ejemplo, para habilitar el módulo Xdebug en PHP 8.3, necesitamos editar el siguiente archivo:
/Applications/ServBay/etc/php/8.3/conf.d/xdebug.ini
Paso 2: Editar el archivo de configuración
Abre el archivo xdebug.ini
y descomenta el siguiente contenido:
[Xdebug]
; Uncomment the following line to enable XDebug
zend_extension = xdebug.so
2
3
Paso 3: Configurar los parámetros de Xdebug
Para aprovechar al máximo las funciones de Xdebug, se pueden configurar algunos parámetros habituales en el archivo xdebug.ini
. Por ejemplo:
xdebug.mode=debug,develop
xdebug.start_with_request=yes
xdebug.client_host=localhost
xdebug.client_port=39083
xdebug.log=/Applications/ServBay/logs/xdebug/8.3/xdebug.log
2
3
4
5
Paso 4: Reiniciar el servicio PHP
En el panel de gestión de servicios de ServBay, reinicia el servicio PHP correspondiente. Por ejemplo, reinicia el servicio PHP 8.3. Una vez reiniciado, el módulo Xdebug se cargará correctamente.
Verificar si el módulo Xdebug se ha cargado correctamente
Puedes verificar si el módulo Xdebug se ha cargado correctamente creando un archivo PHP simple. Crea un archivo phpinfo.php
en el directorio raíz del servidor web con el siguiente contenido:
<?php
phpinfo();
?>
2
3
Accede a https://servbay.host/phpinfo.php
y busca la información relacionada con el módulo Xdebug en la página de información de PHP. Si puedes ver la información de Xdebug, significa que el módulo se ha cargado correctamente.
Configuración de Xdebug en PHPStorm
Para utilizar Xdebug para depurar en PHPStorm, necesitas seguir estos pasos de configuración:
Paso 1: Abrir PHPStorm y acceder a la configuración
- Abre PHPStorm.
- Haz clic en el menú
File
y luego seleccionaSettings
(Windows/Linux) oPreferences
(macOS).
Paso 2: Configurar el intérprete PHP
- En la ventana de configuración, navega a
Languages & Frameworks
->PHP
. - En la sección
CLI Interpreter
, haz clic en el botón...
a la derecha. - En la ventana emergente, haz clic en el signo
+
para añadir un nuevo intérprete PHP, eligeLocal
. - Establece la ruta del intérprete PHP a la ruta correspondiente en ServBay, por ejemplo
/Applications/ServBay/package/php/8.3/8.3.7/bin/php
. - Haz clic en
OK
para guardar la configuración.
Paso 3: Configurar la depuración con Xdebug
Aviso
Cada versión de PHP tiene un puerto Xdebug independiente, asociado al número de versión, por ejemplo, el puerto Xdebug de PHP 7.2
es 39072
y el de PHP 8.3
es 39083
.
- En la ventana de configuración, navega a
Languages & Frameworks
->PHP
->Debug
. - En la sección
Xdebug
, establece elDebug Port
a39083
, coincidiendo con la configuración dexdebug.client_port
. - Asegúrate de que la opción
Can accept external connections
esté marcada. - Haz clic en
Apply
y luego enOK
para guardar la configuración.
Paso 4: Configurar el servidor de aplicaciones web PHP
- En la ventana de configuración, navega a
Languages & Frameworks
->PHP
->Servers
. - Haz clic en el signo
+
a la derecha para añadir una nueva configuración de servidor. - Establece el nombre del servidor, por ejemplo,
ServBay Localhost
. - Establece
Host
al host correspondiente en ServBay, por ejemploservbay.new
, y el puerto a 443 (HTTPS). - Establece el
Debugger
aXdebug
. - Haz clic en
OK
para guardar la configuración.
Paso 5: Iniciar la sesión de depuración
- En PHPStorm, abre el proyecto PHP que deseas depurar.
- Establece un punto de interrupción en el código (haz clic en el área en blanco a la izquierda del número de línea).
- Haz clic en el botón
Start Listening for PHP Debug Connections
(icono de un insecto) en la barra de herramientas superior de PHPStorm. - En el navegador, accede a tu aplicación PHP, Xdebug se conectará automáticamente a PHPStorm y pausará la ejecución en los puntos de interrupción.
Conclusión
ServBay proporciona una manera conveniente de gestionar y habilitar el módulo Xdebug. Con una configuración simple y un reinicio, los desarrolladores pueden habilitar rápidamente el módulo Xdebug en diferentes versiones de PHP, aprovechando sus potentes funciones de depuración y análisis para mejorar la eficiencia en el desarrollo y mantenimiento de aplicaciones PHP. Las funciones de depuración de puntos de interrupción, análisis de rendimiento y verificación de cobertura de código de Xdebug lo convierten en una herramienta indispensable para los desarrolladores de PHP.