Coming from a background as a Windows developer, one of the biggest issues I faced when starting to build *nix apps was how to develop locally. Trying to develop a sails.js app or build a Varnish Cache caching proxy on a Windows machine gets pretty fustrating pretty fast. Additionally even if you are running Ubuntu locally, unless you only work on a single project you’ll run into the issue of multiple apps needing port 80 for testing.
This is where virtual machines come in. By running virtual machines locally you can replicate the production deploy environment on your local machine while keeping it isolated from your local machine’s OS and config. Vagrant is quite possibly the simplest way of doing this. Lets say you want to build a Varnish Cache cache solution for your site. This will most likely get hosted on something like an Ubuntu machine. To get this running on your local dev environment, first install Vagrant & VirtualBox. Then open a command prompt at the root of your repository for this project (you are using source control aren’t you?) and run these two commands:
vagrant init ubuntu/trusty64
This will go and download the Ubuntu 14.04 64 bit virtual machine image from the Vagrant box catlog. You probably don’t want to run this while you’re on mobile data. It will also create a
Vagrantfile in this path. Open this file up with your favourite text editor and have a look. There are a lot of options you can put in here, and we’re not going to go into them now. The only thing you’ll want to do right now is uncomment this line:
#config.vm.network "private_network", ip: "192.168.33.10"
ie, remove the
# at the start of the line. This tells vagrant to make the virtual machine accessable to you at that IP address. So once you’ve got Varnish Cache installed on this VM you can just browse to http://192.168.33.10:6081 to view it (Varnish listens on port 6081 by default).
At this point you have a complete Ubuntu machine running as a VM on your host. To get on to the box you run
Your comand prompt is now in the Ubuntu virtual machine:
Now you can now install Varnish Cache as you would in an actual Ubuntu server, by running
sudo apt-get install Varnish Cache
So, now you can open up http://192.168.33.10:6081 and see Varnish Cache running.
By default, varish sets its backend to localhost:8080. Seeing as the only thing we’ve got runnning on this VM is vagrant, there’s no backend to talk to. You’ll want to edit the
/etc/varnish/default.vcl file and set the backend to something that actually works. However editing that file in the VM isn’t a great solution since it won’t be source controlled. Better to create a file in the same folder as the Vagrant file called
default.vcl. You can grab an example of what it should look like from the varnish source.
Vagrant has automatically mapped the folder on your host that you started vagrant in into the VM as
/vagrant. If you create the
default.vcl file in that folder, it will be in the VM as
/vagrant/default.vcl. You can then write a simple bash script that will copy that into
/etc/varnish and reload Varnish Cache. This way you can edit the vcl locally using a proper text editor, but run it in a simulation of your production environment.
Wrapping it up
There is heaps more you can do with Vagrant. The Vagrantfile is just Ruby, so you can program it too. The simplicity and power of Vagrant is pretty impressive, and it’s why we use it to provide the local dev experience for Section users.
‘Hark! A Vagrant Ref’: Apologies to Kate Beaton