In the first installment, we took a look at Vagrant; what it is and why you should consider switching to it for your development environment.

Vagrant and PuPHPet

Having hopefully swayed you towards investigating Vagrant and its capabilities, we'll guide you through installing Vagrant and Virtualbox, and getting your own development environment setup with the help of PuPHPet.

Installing Vagrant and Virtualbox

Setting up vagrant is very easy, no matter what platform you happen to be on. In the interest of keeping things free we are going to use Virtualbox as the virtual machine provider for Vagrant, however you are free to use any of the supported virtual machine tools.

Installing Virtualbox

The first step is to install Virtualbox. If you are on Windows or Mac OSX you can find installers on the Virtualbox website under the downloads section.

If you are under Linux then it is very likely that virtualbox is in your distribution's repository. If you are on a debian based distribution then you can use the command:

sudo apt-get install virtualbox

If you are on an arch based system then you can use the command:

sudo pacman –S virtualbox

If you're on CentOS:

sudo yum install virtualbox

Installing Vagrant

Getting Vagrant setup on any machine is a very simple process. Simply head over to the Vagrant website. In their downloads section are easy to use installers for Windows, Mac OS X and Linux distributions that will take care of everything for you.

If you are on an unsupported distribution then it is likely that vagrant is in your local repository, and can be installed through your package manager.

Creating a Basic Vagrant Box

Before we can start creating more complex development environments in vagrant we first need to understand the basic workflow of creating a basic vagrant box.

Open up your command line/terminal and navigate to a directory where you want to keep your first vagrant box and type the following commands:

$ vagrant init hashicorp/precise32
$ vagrant up

After running the above commands you should have a fully working vagrant box. You can SSH into this box with the command:

$ vagrant ssh

When you are done with the box you can remove it by typing:

$ vagrant destroy

If you want to learn more about vagrant then you can visit their documentation.

Using PuPHPet to Create and Manage Vagrant Boxes

Not everybody has the time or knowledge to create provisioning scripts in a system like Puppet, and while I highly recommend learning a provisioning system like Puppet, sometimes you just want to get something up and running quickly. This is where a tool like PuPHPet comes into play.

For reference, Puppet is a configuration management system that allows you to define the configuration/state of a system and then it enforces it. PuPHPet is a GUI tool that allows you to easily create Puppet configurations for vagrant virtual machines.

Creating a Basic LAMP Vagrant Box

Lets get up and running with a basic LAMP stack. For this example we are going to be using Ubuntu as the base image, but you can obviously use whatever distribution suits your fancy.

Firstly navigation to the PuPHPet website and under "Local VM Operating System" select "Ubuntu Trusty 14.04". Then, under the "Sharing Folders with Local VM" section find the "Box Sync Folder Source" text box and enter the location where you want your code to be stored on your local machine. On windows I use "C:/www/{project-name}" and on OSX I use  "/Users/{username}/www/{project-name}". Be sure to double check the folder exists!

We don't need to adjust any of the settings under "Server Packages and Firewall Rules" so skip past it and navigate to "Web Servers". Find the "Apache Virtual Hosts" section and under server name enter the domain from which you wish to access your project, I’ll use lamp.dev. Change the document root to the folder which your project launches from, in my case I’ll change it to /var/www.

Next, under “Languages” and “PHP Libraries” select “Install xDebug” and then under database you can fill in whatever settings you'd prefer. Finally, once you've checked over everything, skip to the "Create" section and click the create button.

Once the file has downloaded, extract the folder contents and then navigate to the folder in your terminal or command prompt. Once in the folder enter the following command:

$ vagrant up

This will provision the virtual machine. This may take a while for the first run depending on your internet connection as the base image is downloaded. After than provisioning usually takes around 10 minutes. While you are waiting for the virtual machine to be provisioned you may want to set up your hosts file.

Updating the hosts file

Updating your hosts file is a very simple process. This is what will allow you to view your website from the url “lamp.dev” or whatever you chose earlier on. There's quite a lot of resources available on Google to find, edit and save your hosts file.

Regardless of operating system your hosts file should look something like this:

192.168.56.101           lamp.dev

You should now be able to access your website at http:://lamp.dev. If you are using Google Chrome then it may be necessary to add the http:// to the front of the url the first time.

Updating the PuPHPhet Provisioning Script

You should now have a working Vagrant box in which to develop with and share with your team, but what do you do if you need to make an update to your vagrant development environment?

You could simply run a command from inside the virtual machine, but this is not very flexible and these changes cannot be shared easily with others. A better method is to update the PuPHPet provisioning script.

Always Remember Your Extensions

Let us take a common example, you are dealing with image processing in PHP using PHP’s gd library. If you are on an Ubuntu or a Debian box this library will not come with the default PHP install, so we will need to install it manually.

Navigate to the folder where your vagrant box files are located and find the config.yaml file located in puphpet/config.yaml. Find the section named “server”, this should be around line 60 and under “packages” add php5-gd, it should look something like this:

server:
    install: ‘1’
        - vim
        - php5-gd

Save the file and navigate to the vagrant directory in your command line / terminal window. Type the command:

$ vagrant provision

After a few minutes your vagrant box should now have the PHP gd extension installed. You can now share this new configuration file and after running “vagrant provision” the changes should be applied.

0
Ignite your brand, utilise user-generated content no matter where you or your audience are ›