#Debugging with vim in Homestead/Vagrant
This is a step-by-step on how to set up vim debugging in a vagrant (homestead) environment. This assumes that you have homestead set up.
The following tasks are performed on your Homestead box.
Pathogen allows simple installation of vim plugins. We'll need it to install vdebug. Get it here.
-
Installation of Vdebug with pathogen is a synch. Follow these instructions.
-
Once installed, open ~/.vimrc and add the following lines to the top of the file:
execute pathogen#infect()
call pathogen#helptags()
syntax on
filetype plugin indent on
-
Test that it works:
-
Run
$ vim
-
Within vim, run
:help Vdebug
. You should see the graphic display. Read through chapter 3 as we'll be getting there. -
Set the vdebug listening port. To avoid collisions with other services on port 9000, we'll use port 9001.
-
$ vim ~/.vim/bundle/vdebug/plugin/vdebug.vim
-
Scroll down to below the hash defined by
let g:vdebug_options_defaults = { ... }
-
Below that hash, add the following:
let g:vdebug_options_defaults["port"] = 9001
$ pecl install xdebug
- Open
/etc/php5/fpm/conf.d/20-xdebug.ini
and enter the following:
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9001
Notice how the remote_port above corresponds to the vdebug.vim port entered earlier.
-
Restart nginx and php5-fpm:
-
$ sudo service nginx restart
-
$ sudo service php5-fpm restart
-
Create a script to run files through the vdebugger
-
Check that /usr/local/bin/ is on your path with
$ echo $PATH
-
Create a new file:
$ vim /usr/local/bin/php-xdebug
-
Add this:
#!/bin/bash export XDEBUG_CONFIG="idekey=xdebug" /usr/bin/php "$@"
- Create a new test file at your homestead web server root:
$ vim ~/Code/sandbox/phpinfo.php
- Enter the following in it:
<?php
$test = "here";
phpinfo();
?>
- Place your cursor on the line
$test = "here";
and run:Breakpoint
to set a vdebug breakpoint - In a new terminal tab (tab B), type (but don't run)
$ php-xdebug ~/Code/sandbox/phpinfo.php
- Back in tab A (with vim open), press F5 to start the debugger listening.
- In tab B, press enter. The line should hang as execution rests in tab A.
- Switch to tab A. You should see a red line through the line we stuck the breakpoint on. Refer to the vdebug shortcuts to navigate around.
In a true web app, you'll need to debug via browser requests.
- Create a Homestead mapping to this same sandbox. In my Homestead.yaml file, I put this:
sites:
- map: sandbox.app
to: /home/vagrant/Code/sandbox
- Run
homestead provision
to process the mapping. - In the browser, type the url (but don't press enter):
sandbox.app/phpinfo.php
- Back in vim, press F5 to start the debugger listening.
- Press enter in the browser
- Observe how execution halts on the first line of code in your file
- Press F5 again to play through the rest of the file, rendering the phpinfo to screen.