Initial setup

Before we go into explanations, let's create the required configuration for Vagrant. Create a file named Vagrantfile at the root of your project code base, and write the following code into it:

Vagrant.configure("2") do |config|

# Which box we'll be using as base
  config.vm.box = "hashicorp/precise64"

# Following is only for reference, as Vagrant knows
# where to get the "precise64" box right from the start.
#  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

# What to do with the base box as initial setup
  config.vm.provision :shell, :path => "bootstrap/01-prepare-precise64.sh"
  config.vm.provision :shell, :path => "bootstrap/02-configure-app-for-precise64.sh"
  config.vm.provision :shell, :path => "bootstrap/03-prepare-application.sh"

# How to expose the web application inside the box:
# publish port 80 at the virtual machine as port 8888 at the host machine.
  config.vm.network "forwarded_port", guest: 80, host: 8888

end

This file is Ruby code. If you want, you can write arbitrary expressions here as long as you do Vagrant.configure in it as well.

With this file in place, you can do the magic of vagrant up. When you issue this command for the first time in an unprepared machine and code base, Vagrant will do the following:

  1. Download the box from box_url. In our case, Vagrant knows already where the box named hashicorp/precise64 is.
  2. Unpack the box and register it as the virtual machine image in the VirtualBox suite. The unpacked box will be stored in the home directory of the current user and will be reused for all later invocations of vagrant up.
  3. Launch the virtual machine using the usual VirtualBox means. If you wish, you can even open the VirtualBox management UI and see the Vagrant-managed virtual machine listed among other machines, if any.
  4. Run the provisioning scripts, that is, launch everything listed in the config.vm.provision setting. In our case, provisioning is split into three separate shell scripts, which must be run in exactly that order.
  5. Perform the port forwarding as we specified.

It can do some other things as well, but we are interested only in these steps.

At the second and later invocations of vagrant up, Vagrant will just launch the virtual machine and forward the ports. As was already said, the code base will be constantly shared between virtual and host machines.

It is obvious that Vagrant provides an enormously useful local deployment environment for developers. The most complex part is to correctly craft the provisioning scripts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.28.108