In this post, we described the very basics of Vagrant. In this post, we’ll explore some more advanced concepts. We will boot a virtual machine on top of VirtualBox (we described Digital Ocean in this post in case you would want to try it out using DigitalOcean) and install Apache immediately onto it. Automatically that is, not manually. Let’s go!
Provisioning is generally referring to the installation and configuration of software on your virtual machines in an automated way, e.g.using provisioning tools (shell, Puppet, Chef, Ansible…). For this tutorial, we will use shell script, just get familiar with the concept.
Change your vagrantfile to look like the below snippet. If you are unfamiliar with this, please refer to thispost.
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| config.vm.box = "precise64" config.vm.hostname = "wim" config.vm.box_url = "http://files.vagrantup.com/precise64.box" config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.provider "virtualbox" do |vb| vb.name ="VM-Test" end config.vm.provision "shell", path: "provision.sh" end
As you can see, we are referring to a ‘provisioning.sh’ file. This looks as follows (note: this is tune for Ubuntu as the base image for vagrant is based on Ubuntu)
#!/usr/bin/env bash echo "Installing Apache ..." apt-get update >/dev/null 2>&1 apt-get install -y apache2 >/dev/null 2>&1 rm -rf /var/www ln -fs /vagrant /var/www
We will also put an index.html file in our directory that just echoes the word ‘Index’, or whatever you prefer.
On your CLI, then run the ‘vagrant up’ command
macbook-pro:VagrantBookExample wim$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'precise64'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: VM-Test ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 80 => 8080 (adapter 1) default: 22 => 2222 (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key .... ==> default: stdin: is not a tty ==> default: Installing Apache and setting it up...
When you then go to http://localhost:8080, you will see a webpage displaying the word ‘Index’. Don’t believe me…check out for yourself!