As developers, we all know that creating a sustainable and uniform local WordPress development workflow can be challenging and complex. Programs like MAMP, WAMP, and XAMPP theoretically make the setup of a local web server easy, but, for anyone who has tried it you know that it can be riddled with bugs and far from ideal.
With tools like Git and Capistrano, it is relatively straightforward to deploy changes from a repository to a server, but making changes before that can be chaotic, especially for teams.
That’s where Vagrant comes in. Created by Mitchell Hashimoto, Vagrant is essentially a tool for managing virtual machines - creating a programmatic way create and configure virtual machines that mimic an application’s production environment. From the Vagrant website:
Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.
In this article, we will be using VirtualBox and Ubuntu to create a portable WordPress environment that is cross-platform compatible and is easily able to be deployed to almost any live environment.
To get started we will need to install two programs, VirtualBox and Vagrant along with a Vagrant helper plugin.
Go to https://www.virtualbox.org/wiki/Downloads and download the latest version for your operating system. Extract and install VirtualBox, and you’re ready to go.
Go to http://www.vagrantup.com/downloads.html and download and install the latest version of Vagrant for your operating system. Once installed we will need to install the Vagrant Hosts Updater plugin This plugin will modify your hosts file to allow for easy access of your VM Type this into the your terminal:
$vagrant plugin install vagrant-hostsupdater
Launching Your Own VM
Now we have to build our Virtual machine. Under normal circumstances, you would need to create your own
Vagrantfile and do a lot of custom Ruby scripting to get a machine to get WordPress to work properly. My teammate, Trevor Maglione has created a WordPress Vagrant setup that does all of the hard work for you. We recommend you fork this repository and then clone it to your computer so you can maintain your own version. It is available publicly on Bitbucket:
$git clone [my-cloned repository] /desired-path-to-project/ $cd /desired-path-to-project/
Once cloned on your computer, you need to move the file
Vagrantfile.sample to a file named Vagrantfile and edit it:
Change the value on line 12 to whatever your project is, for example:
config.vm.hostname = 'myproject'
Note: If you plan on running multiple vagrant VMs at the same time you will also want to change the IP address value on line 34 for each new VM you run. Change 192.168.3.10 to 192.168.3.11 on your second VM, 192.168.3.12 on your third VM, and so on.
Starting your VM
Now go into your terminal and go onto the directory where your cloned repository is located and type in:
Note: For Windows users you will need to be in the command line with “administrative permissions.” To issue this command, either use the elevated command prompt or run CYGWIN as an administrator. This is essential for the hosts updater plugin to work.
You will now see that Vagrant downloads a copy of Ubuntu. Currently it is using 12.04, we are planning on updating it to 14.04 in the near future. Vagrant then uses Puppet to install the required packages to run a fully functional WordPress site on a LEMP (Linux Nginx MySQL PHP5-FPM) stack. This process should take less than five minutes.
If you’re not on a Windows machine, it will prompt you for your password (sudo) to make entries into your hosts file.
We are now ready! Go to your web browser and type in the project name into your web browser. If you named it myproject in your Vagrantfile go to http://myproject.dev/. Make sure you type in
http:// or you may confuse your browser. You should now see the WordPress installation screen.
You now have a working local version of WordPress with vagrant. You can now edit files locally and they will automatically be pushed to your VM through shared folders with no delay.
When you're done working for the day, simply type
$vagrant halt to suspend your VM and type in
$vagrant up to resume it. If you ever have any issues with the VM, you should try to provision it with puppet again:
$vagrant reload --provision