Skip to content

Instantly share code, notes, and snippets.

@jdeathe
Last active September 20, 2018 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdeathe/3eb11fa147f607098ec5b7ca87e72025 to your computer and use it in GitHub Desktop.
Save jdeathe/3eb11fa147f607098ec5b7ca87e72025 to your computer and use it in GitHub Desktop.
Using Xdebug to Profile PHP on a Vagrant VM.

Using Xdebug to Profile a Vagrant VM.

These instructions are for profiling a PHP application running on a CentOS-6 remote host using the php56u php 5.6 packages from an OSX local host.

On remote

Install Xdebug for php56u.

# yum -y install \
  php56u-pecl-xdebug

Configure Xdebug.

# sed -i \
  -e "s~^;\(xdebug.collect_params = \)0$~\12~" \
  -e "s~^;\(xdebug.collect_return = \)0$~\11~" \
  -e "s~^;\(xdebug.profiler_enable_trigger = \)0$~\11~" \
  -e "s~^;\(xdebug.remote_enable = \)0$~\11~" \
  -e "s~^;\(xdebug.show_exception_trace = \)0$~\11~" \
  -e "s~^;\(xdebug.show_mem_delta = \)0$~\11~" \
  -e "s~^;\(xdebug.trace_enable_trigger = \)0$~\11~" \
  -e "s~^;\(xdebug.trace_format = \)0$~\10~" \
  /etc/php.d/15-xdebug.ini

Restart Apache

# apachectl graceful

On local

Install qcachegrind via brew.

$ brew install qcachegrind

Install the Vagrant plugin vagrant-scp.

$ vagrant plugin install vagrant-scp

Install the Chrome browser plugin.

Install Xdebug helper.

Port forwarding (optional)

If remote debugging is also required, forward the Xdebug host:port from the remote VM to our local by running the following in a terminal.

$ vagrant ssh -- -R 9000:localhost:9000

Optionally, set the appropriate xdebug.idekey configuration value. The example is for the PHPStorm IDE, for Atom the value should be xdebug-atom.

$ export DBGP_IDEKEY=PHPSTORM

Copy profile output files to local.

Using the "Xdebug helper" Chrome browser plugin, switch to profile mode and visit the URL of interest hosted on the remote VM. This should generate files like: /tmp/cachegrind.out.6772.08ffda.

Using the vagrant-scp plugin copy the output file to the local ~/Downloads directory for analysis.

$ vagrant scp :/tmp/cachegrind.out.6772.08ffda ~/Downloads

Viewing profile output

Open the file for analysis with qcachegrind.

$ qcachegrind ~/Downloads/cachegrind.out.6772.08ffda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment