DEV Community

Vuong
Vuong

Posted on

Debugging PHP application in Vagrant by Xdebug & VSCode

Notes

  • Follow all steps to set up for VSCode.
  • Follow 1, 2, 5 to set up for a server (required for any debugger tool).

Requisites

  • PHP >=5.3 in CentOS 6.x
  • Your PHP project is running in Vagrant
  • The Vagrant instance in this tutorial running on CentOS 6
  • Host machine in this tutorial is Windows 10
  • VSCode, of course!

Install & configuration Xdebug in Vagrant

  • Access root permission by command
sudo -i
Enter fullscreen mode Exit fullscreen mode
  • Run below commands in Vagrant
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum install --enablerepo=remi --enablerepo=remi-php56 php-pear php-devel -y
pecl install xdebug
service httpd restart
Enter fullscreen mode Exit fullscreen mode
  • Add configurations for XDebug in the end of /etc/php.ini file
zend_extension=/usr/lib64/php/modules/xdebug.so
[XDebug]
xdebug.remote_enable=1
xdebug.remote_autostart=1
; Host machine of Vagrant instance
xdebug.remote_host=10.0.2.2
xdebug.remote_connect_back=1

; Another xdebug configuration (in php.ini) set to profiling source code (optional). [Read more](https://xdebug.org/docs/profiler).

; xdebug.profiler_enable_trigger=1
; xdebug.profiler_output_dir=/vagrant/profiler
Enter fullscreen mode Exit fullscreen mode
  • Open .vscode/launch.json or (button setting for debug mode in VSCode), configure it like this below:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "serverSourceRoot": "/var/www/your_website",
            "localSourceRoot": "${workspaceRoot}",
            "port": 9000,
            "log": true
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Install PHP Debug for Visual Studio Code

  • Search the extension as name PHP Debug, install it

Problem with Firewall

There may be a problem with the OS firewall.

Solve: In Windows, access Control Panel\System and Security\Windows Firewall >> Choose to Allow an app or feature through Windows Firewall > Check both options (Private/Public network) for VSCode (or PhpStorm).

Relate topics

Configuring Visual Studio Code for PHP Development

Top comments (0)