Phpstorm Xdebug Vagrant



Xdebug is a PHP extension developed by Derick Rethans for debugging using the DBGp protocol. Xdebug is the only debugging tool to implement the DBGp protocol. If you're on linux and use PECL, you can use the following command: $ pecl install xdebug. Xdebug:2.2.3 PHPStorm:2016.2.2 Vagrant:1.8.6 OS:Ubuntu 14.04 LTS. Start configuring 1. Xdebug installation Check whether Xdebug is installed. First, you need to check that Xdebug is installed, and if it is installed, skip this step. The method is very simple, you can open the phpinfo page directly, searchxdebug。 If the search results. PHPStorm + XDebug + Vagrant. Ask Question Asked 7 years, 2 months ago. Active 7 years, 2 months ago. Viewed 6k times 8. This question is about getting XDebug.

The basic idea in PhpStorm is that you tell it where to look for the xdebug session (the “server”) and then you tell it how to link the files it’s running in the VM to the files found in the repo (called “mapping”).

What follows are opinionated defaults, so adjust as you deem necessary. Specifically, the PHP versions you’ll be supporting

Confirm PhpStorm’s project setup

Make sure you have your project folders already set up in phpStorm.

  1. Open preferences in the main menu, and navigate to the Directories section
  2. Make sure your project’s content root is set
  3. Click Languages & Frameworks, PHP and fill it in like this
    • Set the PHP language level to match the site you’re debugging.
    • CLI Interpreter: select one if you have it, leave it as <no interpreter> if you don’t
    • Include Path section: add the path to the locally-mapped public_html folder of the VM for your project

Set up the Server

This is not a server in the sense of an actual server, more like the settings on how to connect to the VVV server already set up.

  1. In the default toolbar, you’ll see a select box that has either “Add Configuration…” (if you haven’t set up debugging for another project) or “Edit configurations…” (if you have). Whatever it says, click it.
    • If you don’t see this item in your toolbar, add it in by customizing the toolbar. You’ll thank me later.
  2. Click the + sign top left and select PHP Remote Debug and fill it in using the following settings:
    • Name: <your project name> Debug
    • Filter debug connection by IDE key: enabled
    • Server: skip for now
    • IDE Key: VVVDEBUG
  3. Click Apply and keep clicking Apply until you’re back in the editor.

Set up the Folder Mapping

Phpstorm Xdebug Vagrant Download

For PhpStorm and xdebug to correctly hit breakpoints and talk to each other, we need to tell PhpStorm how the files it knows about are mapped to the files that VVV knows about.

There is a way to get the server going manually but there is an easier way: get PhpStorm to do the important bits for us. Here’s how.

  1. Start listening for connections in PhpStorm by clicking the small telephone icon
  2. Load a VM page in your browser
    • PhpStorm will detect a request to connect, but will complain that path mappings are not set up. This is fine, we’re about to do that. But what PhpStorm also does is set up the “Server” configuration for us. Sweet!
  3. When you see the “Incoming Connection from Xdebug” window, click “Manually choose local file or project” and then “Accept”
    • At this point, debugging will run and then stop because the mappings are not set up yet, and there are no break points. Time to fix that.
  4. In the toolbar, select “Edit Configurations…”
  5. On the left side make sure the “ Debug' config is selected, then click the ellipsis in the 'Server:' section
  6. Look at the server already set up for us. Thanks PhpStorm. Now to do the mapping.
    • The key to remember here is that we are mapping the files from where they are relative to the mapped server folder, to where they are in the code repo as it is linked within the VM.
  7. Enable “Use path mappings….” to enable the section below it.
  8. Expand “Project files” and then expand the project path.
  9. Fill in the highest-level section you can to capture everything. For simple projects this will likely be a 1:1 mapping, but for complicated nested projects you may not have to map absolutely eveything, only the children
  10. Expand “Include Path”
  11. Fill in the items below, and only the items below, into this section (if it’s not already filled in; it should be):
File/ DirectoryAbsolute path on server
/path/to/vm/www/<project>/public_html/srv/www/path/to/mapped/folder
  1. Click Apply until you’re back at your code view.
Phpstorm

Done. Now once you enable Xdebug, turn on debugging in PhpStorm and set a breakpoint, you’re ready to debug.

Vagrant is a command-line utility used to manage the lifecycle of virtual machines.

PhpStorm provides a full integration with Vagrant allowing you to configure the Vagrant virtual environment, control the behavior of virtual machines, and execute Vagrant commands from within your project.

In the context of working with Vagrant, you will meet the following definitions:

  • Vagrantfile: the main configuration file that defines the Vagrant environment, stores all the configuration for the virtual boxes and tells Vagrant how to work with virtual machines.

  • Virtual box: a virtual sandbox that contains a preconfigured virtual machine. Vagrant works with different providers of virtual boxes, such as Oracle's VirtualBox, VMWare or AWS.

  • Instance: a virtual machine.

In this article, we will explain how to initialize the Vagrantfile, specify the virtual box, run and interact with the virtual machine from PhpStorm.

Prerequisites

  1. Install and enable Vagrant plugin as described in Installing plugins from JetBrains repository.

  2. Install Vagrant and Oracle's VirtualBox applications.

  3. Make sure virtualization is enabled on your computer.

Initialize the Vagrantfile

Phpstorm Xdebug Not Working

To start working with Vagrant, you need to initialize the Vagrantfile.

Server

Do one of the following:

  • Open the embedded Terminal (Alt+F12) and run the following command: vagrant init

    This will initialize the Vagrantfile and put it into your project root folder by default.

  • From the main menu, choose Tools | Vagrant | Init in Project Root and select the target root folder from the opened window.

In the Project tool window Alt+1, switch to the Project files view and double-click the Vagrantfile to open it in the embedded editor.

You will see that Vagrantfile already has a predefined configuration. The config.vm.box = '...' line specifies the virtual box that will be used in a project.

Specify the virtual box

As an example, we will specify the ubuntu/trusty64 box. It contains a basic Ubuntu virtual machine. You can specify any other virtual box based on your needs. To find a list of available virtual boxes, refer to Discovering Vagrant Boxes.

Do one of the following:

  • Open the Vagrantfile and change config.vm.box = 'base' line to the following: config.vm.box = 'ubuntu/trusty64'.

  • In the Settings/Preferences dialog (Ctrl+Alt+S ), select Tools | Vagrant. In the Boxes window click the button and specify the following:

    • Box name: ubuntu/trusty64

    • Box URL: https://app.vagrantup.com/ubuntu/boxes/trusty64

All the downloaded boxes are stored in the location of the Vagrantfile. To change the default root folder for the virtual boxes, open the Settings/Preferences dialog (Ctrl+Alt+S ), select Tools | Vagrant and change the Instance folder location.

Once the Vagrantfile initialization is completed and virtual box is specified, you are ready to deploy and run the virtual machine.

Launch an instance

Do one of the following:

  • Open the embedded Terminal (Alt+F12) and run the following command: vagrant up.

    You will see the following output:

  • From the main menu, choose Tools | Vagrant | Up.

SSH into a running machine

When the virtual machine is launched, it runs on a backend. To SSH into a running machine:

  • Open the embedded Terminal (Alt+F12) and run the following command: vagrant ssh

Vagrant commands to control an instance

To control an instance, use Vagrant commands. They can be run either from Terminal (Alt+F12) or from the main menu.

In this article, we show only the most important commands to work with the virtual machine. To find a full list of available Vagrant commands, refer to Command-Line-Interface.

Vagrant
  • Suspend: suspending an instance pauses all the processes and saves the current state of a virtual machine.

    Run vagrant suspend in Terminal or select Tools | Vagrant | Suspend from the main menu.

  • Resume: resuming an instance brings up a previously suspended virtual machine.

    Run vagrant resume in Terminal or select Tools | Vagrant | Resume from the main menu.

  • Reload: reloading an instance is required when you've made changes to the Vagrantfile and need Vagrant to reload the current virtual environment and its configuration.

    Run vagrant reload in Terminal or select Tools | Vagrant | Reload from the main menu.

  • Shut down: shutting down an instance stops the running virtual machine.

    Run vagrant halt in Terminal or select Tools | Vagrant | Halt from the main menu.

  • Destroy: destroying a virtual machine is important when you need to remove everything related to the previously created environment. All the resources provisioned during the creation of an instance are removed.

    Run vagrant destroy in Terminal or select Tools | Vagrant | Destroy from the main menu.