Typed PHP, Stronger Types for Cleaner Code (2014)

Extensions

Vagrant + Phansible

Many of the libraries, we will be working with, require a bit of special installation. Instead of labouring away at a guide for each operating system, we’re going to look at using Vagrant for our development environment.

Vagrant (in case you haven’t heard of it yet) is a programatic interface for managing virtual machines. If you’ve ever set up a virtual machine, installed the operating system, installed the development tools, you will know how much time it takes to do well. This process can be automated with Vagrant.

Vagrant depends on underlying virtualisation providers and provisioners. We’ll look at using VirtualBox as the virtualisation provider and Ansible as the provisioner.

Installing

To get VirtualBox installed, go to https://www.virtualbox.org/wiki/Downloads and download the installer for your operating system.

Once you have downloaded and installed VirtualBox, you should be able to install Vagrant. Go to http://www.vagrantup.com/downloads.html and download the installer for your operating system.

We’ll also need to install Ansible so we can use play books to provision the virtual machine. Go to http://docs.ansible.com/intro_installation.html and download the installer for your operating system.

Provisioning

Provisioning is just another word for a set of instructions that tell Vagrant which dependencies to install for you. There are many kinds of Vagrant provisioners, but the only one we will use is called Ansible.

We’ll use http://phansible.com to do most of the heavy-lifting.

Set the following options:

1.    Operating System: Ubuntu Precise Pangolin 64

2.    Webserver: Nginx + PHP5-FPM

3.    PHP: 5.5

4.    Composer: Enabled

5.    PHP Modules: php-pear php5-cli php5-common

When you click “Generate”, you’ll start downloading an archive of files. Extract these into a working directory and start up Terminal.

These files are instruction files (Ansible-specific YAML syntax) which describe which dependencies Vagrant must automatically install. You shouldn’t need to change them to get the PHP stack working, but feel free to familiarise yourself with what they are doing.

To start the virtual machine, run the following command:

1 $ vagrant up

2

3 Bringing machine 'default' up with 'virtualbox' provider...

4 ==> default: Importing base box 'precise64'...

5 ==> default: Matching MAC address for NAT networking...

6 ==> default: Setting the name of the VM: Default

7 ==> default: Clearing any previously set network interfaces...

You may be asked to provide an administrator password, as part of setting up the virtual machine. This will allow the NFS shared folders to be set up.

Once the virtual machine is initialised, you can log into it with:

1 $ vagrant ssh

2

3 Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

4

5  * Documentation:  https://help.ubuntu.com/

6 Welcome to your Vagrant-built virtual machine.

7 Last login: Tue May 27 11:16:05 2014 from 10.0.2.2

8

9 vagrant@precise64:~$

You can also check the installed version of PHP (and that the CLI was installed correctly) with:

1 $ php -v

2

3 PHP 5.5.12-2+deb.sury.org~precise+1 (cli) (built: May 12 2014 13:46:35)

4 Copyright (c) 1997-2014 The PHP Group

5 Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

6     with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies

Vagrant Commands

There are a few Vagrant commands you’re likely to use often:

1 $ vagrant up

This command will start the Vagrant virtual machine, and run any outstanding provisioning. That means the first time you run this command, it will take longer boot (depending on the complexity of your provisioning scripts).

1 $ vagrant halt

This command will shut the virtual machine down. It will try to gracefully shut the machine down.

1 $ vagrant destroy

This command will remove the virtual machine and clean up settings applied when it was created. If you break something inside the visual machine, and you want to reset it to the default state, you’ll want to run this command followed by vagrant up.

1 $ vagrant ssh

This command will take you inside the virtual machine, just as if you were connecting to a remote server. Inside the virtual machine, you can run any of the commands usually supported by the guest operating system, including executing PHP scripts against the packages installed on the virtual machine.

http://phansible.com is the brain-child of Erika Heidi. She is also the author of Vagrant Cookbook. I highly recommend reading this book, if you have any questions or simply want to know more about Vagrant!