Trying to setup PHP development environment with Vagrant

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

We want to set up a PHP development environment.  In the past, we would install the LAMP stack (Linux Apache MySQL and PHP) directly on our machine or use a package like XAMPP, MAMP, or WAMP.  But there is a better way.  Instead of polluting our machine with all these various installation and possibly different versions of PHP etc, we will create a virtual machine within our host machine using Vagrant.  The virtual machine will be where we run our PHP development environment.

For the purpose of this tutorial, we will use a Windows host and VirtualBox to create a virtual Linux box in which to run PHP engine.  So our virtual Linux box acts like a server.  And our host Windows machine is where we will run a browser to hit webpages on our “server”.

1.  To create a virtual Linux box on our Windows host, we use VirtualBox.  Download and install VirtualBox from virtualbox.org — selecting the correct download for you host.  See tutorial on installing VirtualBox.

2.  Another reason we use Virtualbox is because Vagrant supports VirtualBox out of the box (pun intended).   Vagrant is like an higher-order wrapper software that runs on top of a virtualization software like Virtualbox so that we can get our PHP development environment up faster, without need to do a whole bunch of tedious PHP, apache, and MySQL configuration on our Linux virtual box on our own.

So let’s install Vagrant next from www.vagrantup.com.  See tutorial on installing Vagrant.

3. Once Vagrant is installed and you rebooted your system. We can confirm that it runs by opening Terminal on the Mac.  Or the Command prompt in Windows (or type cmd in the “Search programs and files”…

Windows command prompt

Windows command prompt

4. At the command prompt, type “vagrant” and it should give you a help screen confirming that it was installed correctly…

vagrant help screen

vagrant help screen

5. To use vagrant, you need to have a “vagrant box”.  You can search for one on HashiCorp’s Discover Vagrant Boxes (HashiCorp is the makers of Vagrant).  For example, if you search “Official Ubuntu Server”, you get a couple of choices …

search vagrant boxes

search vagrant boxes

6. Since I’m on a 64-bit host, I click on “unbuntu/trusty64” to get the box details…

vagrant box detail

vagrant box detail

7. More importantly, it gives me the commands to download and install this box and create my virtual machine with Ubuntu Server.  But before we type those commands.  Create a new empty directory and navigate to it in the Terminal or Command Prompt.  I created the folder “C:\vagrantbox”.  Then type the first command …

vagrant init ubuntu/trusty64

Vagrant init

Vagrant init

This creates a Vagrantfile in that directory (which you can open in a text editor to peek if you like).  It is basically a configuration file for Vagrant.

8. Then to install and download and startup the box to run in VirtualBox as the provider, type  …

vagrant up –provider virtualbox

vagrant up

vagrant up

This is known as the “vagrant up” command.  This might take a while (like 45 minutes on my machine).  In the end, I’m getting the Warning “Connection timeout. Retrying…

connection timeout

connection timeout

Ctrl-C will stop this.

9.  In any case, I see that a new virtual machine was created in Oracle VM VirtualBox Manager…

vagrantbox created

vagrantbox created

To learn more about using VirualBox Manager to start and stop virtual machines, see this tutorial.

10.  Let’s try another approach.   This time I generate a Vagrant configuration using https://puphpet.com/

11.  After downloading and unziping puphpet.zip, I copy the content of the folder with the Vagrantfile to an empty folder like c:\vm\

12.  In Mac Terminal or Windows Git Bash, navigate to where the Vagrantfile is and type “vagrant up”.  The virtual machine that is created will be configured according to the specs in custom.yaml file.

13.  Then I got error during download …

error during download

error during download

14.  Let’s try downloading again with “vagrant up”.  Nope, same problem.  But this time Estimated time remaining is 19 minutes when it failed (instead of 51 minutes).