PHP renewed

Creating environments with Vagrant and PuPHPet

Automate the creation of shareable virtual machines for development or deployment

Content series: This content is part # of # in the series: PHP renewed Stay tuned for additional content in this series. This content is part of the series: PHP renewed Stay tuned for additional content in this series.

Every developer knows that dread feeling that you get when you submit a bug report and the response is that the code works fine. Or worse, you receive a bug report, and it's impossible for you to duplicate the issue. Usually these situations occur because of differences between the production and development environments, or even differences between two developers' environments. Frustration aside, the real issue is whether the software will run in production, because that's the final goal.

I've seen many different attempted solutions to this problem. A decade-plus ago, everyone on a development team might have acquired computers identical to those used in production. More recently, a common practice was to set up a hosted development environment that all programmers share, and require that development be done remotely. This was an effective solution, but it had the huge drawback of not allowing offline development.

“It works on my machine.” — Any developer

Thanks to increases in computing power, modern-day development can use virtual machines to help solve this issue. If each developer on your team has a virtual machine to develop against, you can create a single virtual-machine configuration for the whole team — a one-time task. Just as Composer takes care of installing your libraries for you, you can use virtual machines to standardize the system itself. This tutorial introduces you to open source tools that work in combination to automate the virtual-machine creation and configuration process.

About this series Continuously evolving under the aegis of an active open source project, PHP now powers much of the web. PHP has undergone remarkable changes since its early days as a templating language. If you haven't used or evaluated PHP technology in many years, you'd barely even recognize some current PHP projects. This series shows you the latest PHP features and how to use today's PHP to build modern, secure websites.

Virtual machines for local development

Here's Wikipedia's definition of a virtual machine:

A system virtual machine provides a complete system platform which supports the execution of a complete operating system (OS). These usually emulate an existing architecture, and are built with the purpose of [...] providing a platform to run programs where the real hardware is not available for use.

Using a virtual machine to emulate a physical computer is what gives you the benefit of consistency in your development and deployment. If you're deploying to a cloud architecture such as Amazon Web Services, you're deploying to a virtual machine already. I'll focus here on running a virtual machine on your local computer.

You can choose among several software solutions to create local virtual machines. Common options include VMware (Windows®/Linux®/Mac), Parallels (Mac), and VirtualBox (Windows/Linux/Solaris/Mac). I'll use VirtualBox for this tutorial. Not only does VirtualBox run on all these systems, but it's also probably the solution that's now most commonly used by PHP developers. Developed by Oracle as an open source project, VirtualBox is 100 percent free to use.

To follow along, go to the download page for VirtualBox and download the appropriate installer for your operating system. Run the installer, which will guide you through a typical software installation process for your OS. After installation, launch the program. The welcome screen shows that you don't have any virtual machines, as shown in Figure 1.

Figure 1. Fresh install of VirtualBox

Now I'll go through the manual process of creating a virtual machine. (You'll learn later in the tutorial how to automate these steps, but I encourage you to follow along hands-on so that you understand what the process entails.) After I click the New button, the Name and operating system dialog box asks me to name my machine and choose what type of machine I want. As shown in Figure 2, I choose to make an Ubuntu installation.

Figure 2. Choosing to make an Ubuntu machine

Now I'm taken through a series of dialog boxes. VirtualBox asks how much RAM to allot the machine, how large to make the virtual disk, and what file format the disk should use. Selecting the defaults for all of these values is recommended unless you have a specific reason to do otherwise. After I click through all the defaults, the virtual machine is created and I can see the entire configuration, as shown in Figure 3.

Figure 3. The new virtual-machine's configuration

At this point, I don't yet have a working virtual machine. VirtualBox has created a virtual machine for me that's prepared and ready for Ubuntu to be installed into it. However, this machine is no different now from a physical machine that's assembled from hardware components. You can turn it on, but unless you install the OS to the hard drive, nothing will happen.

When I click the Start button to launch this new virtual machine, the next dialog box asks me to specify the installation disk that I want it to use, as shown in Figure 4.

Figure 4. Prompting for installation medium

If I were ready to finish making a custom virtual machine for Ubuntu at this time, I would point VirtualBox to a disk image for an Ubuntu installer, and the full process of Linux installation would commence. But right now this isn't the goal. The goal is to make repeatable, consistent virtual machines — so more automation is required than what a manual installation can provide.

Introducing Vagrant

Learn more. Develop more. Connect more. The new developerWorks Premium membership program provides an all-access pass to powerful development tools and resources, including 500 top technical titles (dozens specifically for web developers) through Safari Books Online, deep discounts on premier developer events, video replays of recent O'Reilly conferences, and more. Sign up today.

Vagrant is an automation layer that was created to handle automated virtual-machine provisioning for VirtualBox and other virtual-machine software. Vagrant completely automates the steps I took in the "Virtual machines for local development" section to configure VirtualBox to create a new box.

Through the use of a custom configuration file, Vagrant establishes various settings for the virtual machine and installs the base operating system of your choice. You can specify, with detail, what virtual machine you want. Vagrant saves all of that information into a file called Vagrantfile, which you can then commit into your software projects. Analogous to the composer.json file for building your project (see "PHP renewed: Composer for PHP dependency handling"), the Vagrantfile specifies exactly the system that you want to be installed. You simply issue the vagrant up command, and Vagrant then creates and starts that server.

The Vagrant project also accesses a directory of many preconfigured machines to act as base configurations that you can build upon, making it remarkably easy to create a new installation for yourself.

Now, by using Vagrant, I can automatically create that Ubuntu virtual machine that I wanted earlier. Download Vagrant from its Downloads page and run the appropriate installer for your OS. (Once again, you'll learn later in the tutorial about further automation that will supersede the steps I'm about to perform here, but I recommend that you follow along hands-on so that you understand the process.)

Creating and configuring an Ubuntu virtual machine

Vagrant is a command-line tool, so you won't see a Vagrant GUI after installation. In the Vagrant documentation, you'll find a link to the HashiCorp Atlas box catalog. Similar to Packagist for Composer, this catalog is the default location where Vagrant looks for virtual-machine images to act as your base image. The catalog provides a search index to look for boxes, as you can see in Figure 5.

Figure 5. The HashiCorp Atlas box catalog

Right at the top of that page is a great choice for me: the official version of the latest Ubuntu release, Trusty Tahr. I note the box name as shown in the catalog ( ubuntu/trusty64 ) and now can run the following commands:

vagrant init ubuntu/trusty64 vagrant up

When you issue these commands, you'll see many lines of screen output (similar to what's shown in Figure 6).

Figure 6. Output from issuing the vagrant init and vagrant up commands

And that's it. I'm done. I not only created a VirtualBox machine, but I also configured it with Ubuntu and launched it too. My virtual machine is running in the background, ready for me to connect to it by typing vagrant ssh .

Other vagrant commands

You'll want to use several other common commands, such as:

vagrant halt Shuts down the running virtual machine forcefully. vagrant destroy Shuts down the virtual machine and deletes it completely. vagrant suspend Suspends the machine and saves the current state. vagrant resume Resumes a previously suspended machine. vagrant status Gives you the current status of the virtual machine.

Vagrant provides many other features that you can read about in the documentation, such as synced folders (so that you don't have to ssh into the box to edit code), special networking features, shared boxes, and much more. You can even configure a Vagrantfile to create and manage multiple machines simultaneously. The intricacies of the configuration file can be a little tricky — but next I'll show you why this isn't a problem.

Introducing PuPHPet

So far, I've installed VirtualBox and Vagrant and used Vagrant to automate virtual-machine creation. But I still need to get software installed on my virtual machine. Once again, doing this step by hand will likely lead to inconsistency. Luckily, several automated IT (also called infrastructure automation) solutions are available for this purpose. Among them is the open source Puppet project from PuppetLabs. Puppet uses a somewhat complicated, extensively documented configuration-file system whereby you specify which software is to be installed, commands run, settings tweaked, and much more.

In mid-2013, a PHP developer named Juan Treminio had just finished struggling through learning exactly how all these pieces fit together: how to configure Vagrant, and how to work with the complicated Puppet files to build a completely automated system. He decided that other PHP developers shouldn't have to deal with that struggle, and so PuPHPet was born. PuPHPet is an online GUI, built by Treminio, with which you can select where your machine will be deployed, what OS you want, and what software you want installed/configured on it. PuPHPet then generates both a Vagrantfile and a Puppet configuration for you. Then all you need to do is run vagrant up , and everything just works.

Assuming you've already installed VirtualBox and Vagrant, head to the PuPHPet home page. After clicking the Click Here to Get Started Right Away link, you begin to walk through a series of easy-to-use configuration pages. Figure 7 shows the getting-started page.

Figure 7. PuPHPet getting-started page

In each step of the process, as you work through the menu on the left, you configure some aspect of the server:

Deploy Target : Here, you choose the virtual-machine provider (VirtualBox is the default selection) and the OS and distribution that you want to use, such as Ubuntu Trusty.

: Here, you choose the virtual-machine provider (VirtualBox is the default selection) and the OS and distribution that you want to use, such as Ubuntu Trusty. System : You can specify packages to install, such as vim , set up users/groups/cronjobs, and more.

: You can specify packages to install, such as , set up users/groups/cronjobs, and more. Web Servers : You can choose to install Nginx or Apache, and you can configure the web server in advance.

: You can choose to install Nginx or Apache, and you can configure the web server in advance. Languages : For PHP (the default; other choices include Ruby, Python, and Node.js), you can pick not only which version but also change the PHP configuration as needed.

: For PHP (the default; other choices include Ruby, Python, and Node.js), you can pick not only which version but also change the PHP configuration as needed. Databases: MySQL is the default installation here. You supply basic configuration information such as the root password and any databases that you'd like created for you.

The other menu items — such as Mail Tools, Work Queues, and Search Servers — offer up similar options for other tools that advanced web applications might need.

After you make all of the configuration choices (the default configuration is workable for a standard PHP development system), you're at the Create Archive menu item. Click Create Archive to go to Download Your Customized Archive page, shown in Figure 8.

Figure 8. PuPHPet page for downloading your customized archive

Click Go ahead then, make it! to create the Vagrantfile and get a ZIP file with all of your configuration.

You've created a reusable system that can be shared with other developers as a repeatable resource. The ZIP download contains not only a Vagrantfile but a folder containing the rest of the configuration (for Puppet). You simply run vagrant up to start the process of building the system to your exact specifications. Be sure to have some spare time on your hands; completing all of the necessary installations can take a while. You can check this configuration into your version-control system or share it however you want with your coworkers. You can also, if you want, go into the configuration files in the puphpet directory and configure or tweak your system further. And all of the Vagrant commands that I covered in the "Introducing Vagrant" section work on this Vagrant box too.

Conclusion

In this series, you've seen the ways in which PHP has grown significantly since its early days. The language itself has not only survived the test of time — and gone on to power 82 percent of the Internet's websites — but has also evolved into a complex advanced system. The PHP ecosystem has also grown up around the language, enabling professional enterprise-level development with PHP. Whether you are making the next Facebook or an intranet for your company, PHP is an excellent and powerful choice. The recent addition of PuPHPet to the ecosystem has removed much of the pain of development, especially on large projects with dozens or hundreds of PHP developers all working together toward a common goal.

Downloadable resources

Related topics