Vagrant Basics

I’ve been hearing a lot recently about vagrant. I tried my hand at it and got no where, it seems so messy to get started.

test

I tried naming the image provider and I didnt put in Proper Case so it yelled at me. I tried getting apache installed, but when I deleted the server, it was no longer there.
So I gave up. Nothing helped.. till I went to a meetup (GrPhpDev and heard a talk by djheru. It was a super quick talk, he went over the file structure, showed a few things to change and bam he had a vm running.

The next day at work, I had to try it. He had the example up as a (github repositor)[https://github.com/djheru/VagrantExamples) so I quickly cloned it into a project I was working on, change a few files and BAM, I had one running.

So I’m calling this Vagrant Basics because well I’m still in the basics, I probably can’t add a new library or make it install something new. But I figured the little I learned from his talk, from the example and what I got working might help out someone save a few hours and get working on a powerful tool.

First step, clone the file. NOT INTO YOUR PROJECT!! You aren’t there yet.

You get it? No? Fine… Look here for help on that process.

Now you have that, its time to copy it again and edit that (so we can compare later or do it again for another project).

There are a few files you will see in the repository clone.

  • provision : manifest
  • site : vagrant examples
  • .gitignore
  • README.md
  • Vagrantfile : sample vagrant setup accessible from host at 192.168.56.2

Vagrantfile

The father file: Vagrantfile. This file gives the birth rights and info to all the other files. So this would be a good time to open that file. In that file is config.vm.box, that “key” defines which vm box you want to use. Here is a list of others. Based on the one you picked, determines what you put here: config.vm.box_url. That is for when you haven’t downloaded that image already into vagrants local system storage. After the first run, it will use the local one instead of download the 800mb file.

config.vm.network determines your ip for the machine, you can add other things here, I don’t know how yet. so sorry.

config.vm.network :private_network, ip: “192.168.56.2”

config.vm.synced_folder is kind of important, it tells vagrant on provisioning what to sync and where too.

Default: config.vm.synced_folder “./”, “/vagrant”

What I put: config.vm.synced_folder “./”, “/var/www/”, type: “nfs”

So the provision, section, it basically points to all the files you want and which is the default and then points to where the configs for each of those manifests are.

config.vm.provision :puppet do |puppet|
puppet.manifests_path = "provision/manifests"
puppet.manifest_file = "default.pp"
puppet.module_path = "provision/modules"
end

provision/manifests/default.pp

Simply put this file describes which modules to include. I don’t use mysql on my vms since I prefer having the dbs on a more consistent location and make them accessible by other apps I’m working on.

provision/modules/apache/manifests/init.pp

Just leave this along. Its good enough. Mess with it if you want.

provision/modules/apache/files/default

Well this is the default apache config file, it will upload it and put it in the sites-avaible folder in apache (for ubuntu). Just update the file paths and servname to match your websites: /var/www//<html|web|www|public>

Getting it up and running.

So you have made some edits to the files, but I said don’t put it in your project, well yeah, it wasnt ready, now it is. Take those files (minus the .gitignore and .git) and paste it into your projects top level folder. Simple as that. Vagrant is ready to do its thing. We just have to wake the beast.

Continue reading Vagrant Basics