Why is my Symfony 2.0 site running slowly on Vagrant with Linux host?

symfony, vagrant

Solution

Where I work, we've tried out two solutions to the problem of Vagrant + Symfony being slow. I recommend the second one (nfs and bind mounts).

The rsync approach

To begin with, we used rsync. Our approach was slightly different to that outlined in AdrienBrault's answer. Rather, we had code like the following in our `Vagrantfile`:

config.vm.define :myproj01 do |myproj|
  # Networking & Port Forwarding
  myproj.vm.network :private_network, type: "dhcp"
  # NFS Share
  myproj.vm.synced_folder ".", "/home/vagrant/current", type: 'rsync', rsync__exclude: [
    "/.git/",
    "/vendor/",
    "/app/cache/",
    "/app/logs/",
    "/app/uploads/",
    "/app/downloads/",
    "/app/bootstrap.php.cache",
    "/app/var",
    "/app/config/parameters.yml",
    "/composer.phar",
    "/web/bundles",
    "/web/uploads",
    "/bin/behat",
    "/bin/doctrine*",
    "/bin/phpunit",
    "/bin/webunit",
  ]
  # update VM sooner after files changed
  # see https://github.com/smerrill/vagrant-gatling-rsync#working-with-this-plugin
  config.gatling.latency = 0.5
end

As you might notice from the above, we kept files in sync using the Vagrant gatling rsync plugin.

The improved NFS approach, using bind mounts (recommended solution)

The rsync approach solves the speed issue, but we found some problems with it. In particular, the one-way nature of it (as opposed to sharing folders) was annoying when files (like `composer.lock` or Doctrine migrations) were generated on the VM, or when we wanted to access code in `/vendor`. We had to SFTP in to copy things back - and, in the case of new files, do it before they were cleared by the next run of the gatling plugin!

Therefore, we moved to a solution which uses binding mounts to handle folders like cache and logs differently. Not having those shared increased the speed dramatically.

The relevant bits of the Vagrantfile are as follows:

# Binding mounts for folders with dynamic data in them
# This must happen before provisioning, and on every subsequent reboot, hence run: "always"
config.vm.provision "shell",
  inline: "/home/vagrant/current/bin/bind-mounts",
  run: "always"

The `bind-mounts` script referenced above looks like this:

#!/bin/bash

mkdir -p ~vagrant/current/app/downloads/
mkdir -p ~vagrant/current/app/uploads/
mkdir -p ~vagrant/current/app/var/
mkdir -p ~vagrant/current/app/cache/
mkdir -p ~vagrant/current/app/logs/

mkdir -p ~vagrant/shared/app/downloads/
mkdir -p ~vagrant/shared/app/uploads/
mkdir -p ~vagrant/shared/app/var/
mkdir -p ~vagrant/shared/app/cache/
mkdir -p ~vagrant/shared/app/logs/

sudo mount -o bind ~vagrant/shared/app/downloads/ ~/current/app/downloads/
sudo mount -o bind ~vagrant/shared/app/uploads/ ~/current/app/uploads/
sudo mount -o bind ~vagrant/shared/app/var/ ~/current/app/var/
sudo mount -o bind ~vagrant/shared/app/cache/ ~/current/app/cache/
sudo mount -o bind ~vagrant/shared/app/logs/ ~/current/app/logs/

NFS + binding mounts is the approach I'd recommend.

Problem

I've got a Symfony 2.0 application running using Vagrant with a Linux guest and host O/S (Ubuntu). However, it runs slowly (e.g. several seconds for a page to load, often more than 10s) and I can't work out why. My colleagues who are running the site locally rather than on Vagrant VM have it running faster. I've read elsewhere that Vagrant VMs run very slowly if NFS isn't enabled, but I have enabled that. I'm also using the APC cache to try and speed things up, but still the problems remain. I ran xdebug against my site using the instructions at http://webmozarts.com/2009/05/01/speedup-performance-profiling-for-your-symfony-app, but I'm not clear where to start with analysing the data from this. I've got as far as opening it in KCacheGrind and looking for the high numbers under "Incl." and "Self", but this has just shown that `php::session_start` takes quite a long time. Any suggestions as to what I should be trying here? Sorry for the slightly broad question, but I'm stumped!

Original source

Related problems