Tips for getting started with Vagrant, dev environments evolved

by Cedric Dugas on October 21, 2012

I have been using xampp for my local lamp stack for ages. It works well, but that’s definitely nothing like my live production server. Enter Vagrant, “easily” reproducible dev stacks.

Wouldn’t it be perfect if you could just give a recipe to your new developers and boom, their app environment is ready, they can get to code right away. No more fiddling with hosts, memcache, enabling by hand php modules. Just 2 or 3 commands and your done. I’m not going to go deep into a tutorial since I am still very new to Vagrant but I thought I could give you a couple of tips to get you started easily.

Video tutorial

One nice thing about Vagrant is their introduction video that helps you automate your first lamp stack. The bad news is that while it will get you 90% there, Vagrant changed a lot since that video was made and you will have to do a bit of testing to get everything working.

These slides are also a good starting point, it’s a bit more up to date and the coding style is more elegant.

Fiddling with hosts

Vagrant by default will forward the vm to your 127.0.0.1 with a special port (any port you choose). On your side you can’t use your host to forward 127.0.0.1:8080 to (for example) devdomain.com. Hosts file are only for dns not for port so it won’t help you. You could of course go to forward back the port using firewall apps and etc.

But the easiest thing to do is nothing, put your domain in your host file and then you can just type devdomain:8080 and it’s going to work since you already forwarded that domain to your localhost in your host file.

MYSQL installation

There is currently a bug when you install mysql with the default ubuntu box that Vagrant provides, for some reason chef tries to connect before mysql is ready and it’s going to throw an error.

Error executing action `run` on resource 'execute[mysql-install-privileges]'
STDERR: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Fortunately there is an easy monkey patch until they get that worked out. Go to your mysql recipe and edit the server.rb file

139 subscribes :run, resources("template[#{grants_path}]"), :immediately
140 ignore_failure true # "Can't connect to MySQL" monkeyfix

There is not much informations about this bug, but if you look a bit there is these guys that talk about it.

PHP and it’s modules

First thing first, do not forget to add

include_recipe "apache2::mod_php5"

Or your files won’t load in your browser, you will download them since the header won’t work. In fact thinking about apache, you probably should include all of those:

include_recipe "apache2"
include_recipe "apache2::mod_php5"
include_recipe "apache2::mod_rewrite"
include_recipe "apache2::mod_deflate"
include_recipe "apache2::mod_headers"

As for php, do not forget to add all the modules you need to, your list should look something like:

include_recipe "php"
include_recipe "php::module_apc"
include_recipe "php::module_memcache"
include_recipe "php::module_curl"
include_recipe "php::module_mcrypt"

Unfortunately the PHP cookbook does not include recipe for all php modules, for example, mcrypt is not in there, you will have to copy a module recipe and add your custom modules yourself. Fortunately it’s pretty straight forward and the code is simple, here how I implemented mcrypt:

pkg = value_for_platform(
    [ "centos", "redhat", "fedora" ] => {"default" => "php5-mcrypt"}, 
    "default" => "php5-mcrypt"
  )
 
package pkg do
  action :install
end

Following that convention you should be able to get all the php modules working.

File templates

One of the nice thing about Vagrant is that you can pretty much template any file from your vm. I use this functionality to rewrite /etc/hosts for what I need.

on your recipe you do:

template "/etc/hosts" do
	source "hosts.conf.erb"
	owner "root"
	group "root"
	mode 0644
end

Then in your hosts.conf.erb you can pretty much do whatever you want with that file using ruby, you also have access to a wide range of variables from vagrant.

Vagrant, a very impressive technology

Vagrant is impressive, it’s unfortunate that it’s hard to find good up-to-date tutorials to get started, we often get puzzled by a code that should work and not because it changed since the article was written. Hope this couple of tips can help you get your environment running!



Leave your comment

Required.

Required. Not published.

If you have one.