While using virtualized environments for development is certainly not a new concept, several tools that have been released in the last few years have made it almost mandatory. Gone are the days of having either separate physical hardware for each customer or project, or worse, trying to work in a single environment for multiple projects.
Vagrant is an open source wrapper for several popular virtualization packages such as Virtual Box and VMWare and configuration management software such as Puppet, Chef and Salt.
One of the more powerful features of Vagrant is the use of Synced Folders. It automatically and seamlessly handles sharing of folders and files between the host and guest operating systems.
Puppet is an open source data center automation system that allows centralized management of IT infrastructure.
PuPHPet is an open source web based application that will very quickly (and repeatably) generate Puppet configuration files that will be used by Vagrant.
The focus of this article will be on using the tools above to set up a LAMP type development environment using the tools above.
Getting Started with Vagrant
Once you have Vagrant installed, create a folder that will contain your VM config as well as Synced Folders. For example, on my Mac I have a folder called ~/Documents/Dev_VM. Under this I create new folders for each environment that I set up. In the Dev_VM directory I created a folder called dev_test_01.
As detailed in the Vagrant Getting Started guide on their website without doing anything else, I could get a running VM working with the following command.
$ vagrant init hashicorp/precise32
$ vagrant up
That’s it. I now have an Ubuntu 12.04 LTS 32-bit headless VM running.
To get into it, from the dev_test_01 folder I would type
$ vagrant ssh
After logging out, I can completely destroy the VM by typing:
$ vagrant destroy
The Vagrant Getting Started guide goes into all of the details needed to get up and running.
Building Vagrant Configs with PuPHPet
Just having a default OS install running in two shell commands is good, but since we want to set up a development environment, there are other tools and packages that need to be installed. This is where PuPHPet comes in.
The PuPHPet website has a wizard that will walk you though things like the guest OS, IP configuration, Apache, Apache Modules, PHP, PHP libraries and Databases. Once you step through the wizard it creates a Manifest that you simply download and unarchive into your VM folder such as the dev_test_01 folder that I created above.
Now when you execute the vagrant up command the VM will be set up with the OS, packages and settings that you selected. It takes a few minutes to complete depending on the options selected, but once it’s done we end up with a fully configured VM
Working with the VM
Just having a headless development server setup wouldn’t be very useful if you couldn’t use your favorite development tools to access it. Not many people want to write full applications using Nano! This is where the power of Vagrant’s Synced Folders come in. By default the folder where your Vagrant configuration file exists on the host (i.e. /dev_test_01 in the example above) will be mapped to a folder on the guest called /vagrant.
To see this in action, create a file in your VM folder on the host, and then log into the VM using vagrant ssh and you will see the same file in the /vagrant folder. This is all covered in detail in the Vagrant Getting Started Guide. During the PuPHPet configuration wizard, you have the option to change this. The default is to map ./ to /var/www. This would allow you to edit a web application from your host using your normal development tools. This same web application would run under Apache on the guest machine.
Accessing a Website on the VM
Two of the options during the PuPHPet configuration wizard were the Server Name and Server Alias. To access the website from the host, modify your /etc/hosts file to point the Server Alias to the IP that you gave the VM. Now from a host browser you can get to the website using the server alias.
From the Vagrant ssh login you could manage MySQL using the mysql command line tools, but you can also access it from your favorite database management tools such as Sequel Pro or MySQLWorkbench.
You can connect to MySQL using SSH. The settings will be based on the options that were set in the PuPHPet configuration.
These are the settings I used with Sequel Pro.
MySQL Host: 127.0.0.1
Password: Database: leave set to optional or choose a default database
SSH Host: 192.168.56.103
SSH User: vagrant
SSH Key: …/puphpet/files/dot/ssh/id_rsa
Besides the obvious benefit of getting development VM’s up quickly, using this method will allow you to standardize development environments across entire teams by sharing your configuration files. Everyone on your team can have the exact same VM that you have, configured the way you want it. No need to worry what version of PHP someone’s running or what modules they have installed.