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

Rapidly Building LAMP Development Environments with Vagrant

Provisioning new Virtualbox LAMP development VMs from scratch can be dome with minimal effort using vagrant. By making this process easy and fast I can dedicate an entire VM environment for each development project I have on hand and be sure that my environment remains consistent. That last part is not new, we have been using VMs like this for years but in my experience the VMs often become too valuable because of the manual effort required for initial set up, provisioning software, and ongoing maintenance. Vagrant scripts enable me to build and destroy VMs several times a day at speed, as and when required - removing the manual overhead.

No more "it works on my machine" scenarios, keep development environments isolated and in line with production specifications.

Base Dev LAMP Stack spec

Available here my basic LAMP Development environment includes:

UPDATE 30/07/2013: New version includes xhprof profiler built in, see PHP Vagrant development instance with xhprof profiler

  • Apache
    • mod_rewrite
    • mod_php
    • Named virtualhost set up
      • Document root /vagrant/www/ (synched VM folder)
      • Name lampdev
  • MySQL
  • PHP
    • APC op-code cache
    • PEAR
    • PHP QA tools (phpunit, phpdoc, xhprof etc)
    • XDebug - setup for remote debugging
  • 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

Quick Guide

1. Install Virtualbox
2. Install vagrant
3. Install vagrant base box

# You can use an alternative base box if you wish
vagrant box add precise64

Then ...

# Clone the Vagrant LAMP stack configuration
git clone --recursive
# enter the cloned directory
cd vagrant-dev-lamp
# Build the VM using vagrant
vagrant up

On my now slightly old dual core machine it takes about 7 minutes to build.

Base Box

If you want to change the base box you just need to add the box you do want into your environment (like in the command above) and then edit the Vagrantfile, replacing the base box name in the line = "precise64" with the one alternate one just added. You can also build your own base box if you wish, there is also a tool available to help with that


Puppet, chef or shell? You can provision the software and services with any, I'm using Puppet, mainly because I have some good modules that make installing Apache, MySQL, PHP and common PHP tools quick and easy, my requirements here are basic and you do not need to know huge amounts about Puppet to get things working.

In order to use the puppet modules for provisioning within vagrant I've included them as git submodules (inside the directory puppet/modules) and added a line into the vagrant file specifying where it should look for to find my puppet modules:

config.vm.provision :puppet do |puppet|
    puppet.manifests_path = "puppet/manifests"
    puppet.module_path = "puppet/modules"
    puppet.manifest_file  = "lamp.pp"

Remote Debugging

Remote debugging via xdebug is enabled during the VM creation, all you need to do is configure your IDE to use it. Here are some instructions on how to do that using eclipse PDT.

1 Go to Window > Preferences
2 Drill down through PHP > Debug > Installed Debuggers
3 Select XDebug from the right hand side and click 'Configure'
4 Change the setting 'Accept remote session (JIT)' to 'localhost' and click OK

eclipse remote debug config

5 In the same settings window drill down through PHP > PHP Servers

6 Click the New button on the right hand side
...6.1 Name the server i.e. 'Remote'
...6.2 Enter the base URL 'http://localhost:8888'
...6.3 Enter the path to the web root, this is the path to the vagrant synced folder '/path/to/vagrant/www'
...6.4 Click 'Finish' and then set the new server as the default (or optionally configure this manually when setting up each debug session)

eclipse remote debug config

The above will work with the base LAMP stack, if you have multiple environments in play then you may have to change the port numbers in use appropriately.


  • - PHP PEAR puppet module
  • - PHP QA tools puppet module
comments powered by Disqus