The Development Environment

First up I will be using a Linux system in all my examples, therefore most of the content in terms of using the command line and configuration should translate directly if you’re a Mac OS X user. If you’re a Windows user things may be a bit different but shouldn’t be too hard to work out.

Requirements

The only real requirement for working through this book is that you are using PHP 5.5 or above and the SQLite extension. Throughout this book we will be using PHP both from the command line and in a webserver environment.

Install PHP and SQLite on Ubuntu easily by running:

1 $ sudo apt-get install php5-cli php5-sqlite

When working in a web environment I will be using a tool called Vagrant, which runs the development environment in a virtual machine on the computer. This removes the need to set up and configure a webserver (and database servers) directly on the development computer. This is the way I would recommend working. However, if you do want to manually set up the relevant webserver and database servers on your development machine then you can do that, but you’ll have to work that out yourself.

Vagrant

Vagrant is a neat little tool. It allows many developers working on the same project to run a local copy of the project’s environment easily without having to install all the project’s dependencies on their development machines. It does this by building and running a virtual machine from a config file included in the project.

Vagrant itself simply instructs a virtualisation provider on what type of virtual machine to create and then uses a configuration automation system to configure that virtual machine.

Different providers, such as VMWare, are available for use with Vagrant but I will be using VirtualBox.

Also, different configuration automation systems, such as Chef and Puppet, can be used with Vagrant. I am choosing to use Ansible just because I prefer the syntax.

Installing

In order to use Vagrant you will need to install:

I recommend that you install both Vagrant and VirtualBox by downloading the distribution packages directly from their websites so you get the current versions.

With Ansible, you need to make sure you have an up to date version. On Ubuntu I tend to install it via Rodney Quillo’s PPA since it’s more up to date than the version in the Software Center.

Creating a Vagrant Config for PHP Development

Once you have Vagrant, VirtualBox and Ansible installed, it’s time to build a Vagrant configuration.

It’s not hard to build a Vagrant config file by hand, or to build the config automation scripts. However, it is a bit tedious, takes some learning, and is not really something this book intends to cover. Luckily there are some fantastic online tools available which make this process a lot easier. Since we want to build a PHP development environment and we’re going to use Ansible. We will use a fantastic tool called Phansible to create our config for us.

Phansible

First, open http://www.phansible.com/ in your browser. You will see a form asking questions about the development environment that you want to create. For this example choose the following options:

Options Value
Operating System Ubuntu Trusty Tahr (14.04) 64
Name VagrantExample
IP Address 192.168.5.10
Memory 512
Shared Folder ./
Webserver Apache + PHP5
PHP Version 5.6

You can use a different local IP address if you want, but remember it, since we’ll need to use it shortly.

Ignore the Database and Package settings for now, and finally choose an appropriate Timezone.

Next, click the Generate button at the bottom of the form, and save the generated .zip file to your hard drive. I saved my file to /home/tom/Downloads/phansible_VagrantExample.zip.

Creating the Project

Now that we have generated the Vagrant configuration, let’s create a PHP project and add the Vagrant configuration to it.

At your terminal, cd to where ever you want to create your project:

1 $ cd /home/tom/Projects

Next, create a directory for your new project, and cd into it:

1 $ mkdir VagrantExample
2 $ cd VagrantExample

Now create a file inside this directory called index.php, and add the following content, using your favourite IDE or text editor:

index.php


1 <?php
2 
3 echo 'Hello wonderful World!';

Next, unpack the contents of the Vagrant configuration .zip file that we downloaded from the Phansible website earlier,

1 $ unzip /home/tom/Downloads/phansible_VagrantExample.zip

Finally, while still inside the project’s directory and with your computer connected to the Internet, run:

1 $ vagrant up

This may take some time, and you may be prompted to enter your password to allow Vagrant to sudo to update some config files. Just be patient.

When it is done, open your browser and enter the IP address we selected earlier into the location bar like so:

http://192.168.5.10/

If all has gone to plan you should see Hello wonderful World! displayed on the page! Success! - we have created a PHP development environment for our project without installing or configuring a webserver on our local machine.

Once you have marvelled in the glory of Vagrant you can shut down the virtual machine by running the following command at your terminal:

1 $ vagrant halt

Shutting Down

I have found that if I forget to shutdown my virtual machines before I try to shut down my computer, it hangs during the shut down process. If you have a solution to this problem I’d love to hear it!