Blog: PHP, Python, Linux, Web services & Continuous delivery

PHP Vagrant development instance with xhprof profiler

I've just added support for xhprof profiler into my base vagrant LAMP (PHP) stack . It automatically adds profiling to any PHP file that is served from the default virtual host (document root: /vagrant/www/) and includes a link at the footer of each page over to the xhprof output. Graphviz has also been installed so callgraphs are generated if you want something more visual.

The vagrant instance is available on github here: https://github.com/pipe-devnull/vagrant-dev-lamp

For a quick confirmation test and an example of the profiler in action browse to http://localhost:8888/profiler.php after provisioning the instance. If you click on the link 'profiler output' at the foot of the page you should see the xhprof profiler output (fig 1) which contains another link over to the callgraph (Fig 2).


xhprof output example
fig 1. xhprof example output


Fig 2. xhprof callgraph


For a recap on provisioning instances and details on how to set up remote debugging see earlier post Rapidly Building LAMP Development Environments with Vagrant.

Heres an updated spec of the vagrant stack:

  • Apache
    • mod_rewrite
    • mod_php
    • Main virtualhost set up
      • Document root /vagrant/www/ (synched VM folder)
      • Name lampdev
      • port 80
    • xhprof virtualhost
      • Document root /var/xhprof/
      • Name xhprof
      • port 8000
  • MySQL
  • PHP
    • APC op-code cache
    • PEAR
    • PHP QA tools (phpunit, phpdoc, code sniffer etc.)
    • XDebug - setup for remote debugging
    • xhprof - PHP profiler, auto enabled for all PHP files served from /vagrant/www/
  • Basic utilities
    • curl
    • vim
  • Networking
    • VM Port 80 (http) traffic forwarded to port 8888 on host - point your browser at localhost:8888
    • VM Port 22 (ssh) traffic forwarded to port 2222 on host - ssh to localhost:2222
    • VM Port 22 (ssh) traffic forwarded to port 9000 on host - ssh to localhost:9001
    • VM Port 22 (ssh) traffic forwarded to port 8000 on host - ssh to localhost:8000


A few mopre details on the Xhprof installation


  • xhprof vhost served on port 8000, forwarded to host on port 8000 too.
  • xhprof output is served via a separate virtualhost in which I symlink to the xhprofhtml and xhproflib directories that, in Debian, are placed in /usr/share/php/xhprof during xhprof installation.
  • Profiler output is stashed in /tmp/ and as such are not persisted between reboots
  • Header & footer PHP files are auto appended to all files served from the main vhost (rooted at /vagrant/www/) that enables the profiler and link over to the output
  • WARNING:
    ** If you are running more than one Vagrant instance concurrently then Vagrant may automatically assign different ports for forwarding traffic to the host. If this happens then the footer links to profiler output may need to be updated. ** Tested using Debian & Ubuntu base boxes
comments powered by Disqus