Setting Up Personal PHP/PostgreSQL Dev Environment With Vagrant

[This blog post was originally written by Triet Le]

The Goals

From the first day at Pivot Freight we had two specific goals with regards to development environments:

  • We can’t afford to slow down adding new features.
  • We need a stable development environment that allows us to easily and quickly test our code.


Hence, we need to have a sustaining local development environment that is:

  • isolated.
  • easy to develop and deploy testing code.
  • as similar as possible to our production environment (PHP, PostgreSQL, Nginx).
Vagrant

The Hows

There are many techniques and methodologies to set up an isolated environment either hosted in the cloud like Rackspace or AWS, or a local self-sustaining environment like as Docker (we are still considering Docker for our deployment strategy – more to come).

We decided to use Vagrant because we feel like it’s lightweight and makes it easy to virtualize a Linux box on our desktop. In the future, we may use Vagrant to manage Linux VMs to run Docker containers.

The Steps

Installing Vagrant

  1. Install Vagrant from https://www.vagrantup.com/.
    Install VirtualBox from https://www.virtualbox.org/.
    More details on why we need virtualbox can be found @ http://docs.vagrantup.com/v2/virtualbox.
  2. Go to https://atlas.hashicorp.com/boxes/search and copy the link for the Ubuntu box (14.04 LTS as of this post).
  3. The flavor of linux is up to you. We are using Ubuntu to be compatible with the Linux version of our production system. Later we will talk about packaging your own Vagrant “box” to be shared.
  4. Run: Vagrant box add <link>. As of this post it will be Vagrant box add https://atlas.hashicorp.com/ubuntu/boxes/trusty64.
  5. Run: Vagrant box list and verify the Ubuntu box is available.
  6. Create a local path to create the actual Vagrant instance, e.g. ~/vagrant/ubuntu/trusty64 and cd into it.
  7. Run: vagrant init ubuntu/trusty64

Set up your VagrantFile

  1. VagrantFile is basically the way you customize your initial Vagrant box.
  2. Run: cat Vagrantfile and verify the file exists and has expected contents.
  3. More of what you can do with VagrantFile can be found at http://docs.vagrantup.com/v2/vagrantfile/index.html. I found the following config to be very useful:
    • Add a config.vm.network entry to forward 8080 (host) to 80 (guest). I’m planning to use this box as my basis for my Nginx webserver. This is a very powerful technique.
    • Edit Vagrantfile and: add a config.vm.synced_folder entry to mount “../data” (as an example, this could be where the v2 Git code is, relative to where your Vagrantfile is). This will allow a very rapid development and testing cycle.
    • Please share any other cool config you are trying in the comment section below.

Use Cases

Pivot Freight currently has 2 Vagrant boxes (more posts on this to come):

  1. Set up a PHP / PostgreSQL / Nginx testing and development environment.
  2. Set up a Java development testing and developing to work on our Java-based API.

Start Your Vagrant VM

  1. Run: Vagrant up, make sure all is well.
  2. Run: Vagrant ssh, make sure all is well.

Sharing is Caring

There is no point of building up a VM artifact if you can’t share it. You can package what you have so far by packing your Vagrant VM.

You can then “import” or box that environment by importing a prepared box.