Easy virtual development envinronments with Vagrant

Vagrant is your next work companion: it will create a virtual development environment ready to run on every machine, you no longer need to install a web server, a database or any other package on every single machine, just set it up once and share the same environment with your team.

Requirements

Download and install Vagrant and VirtualBox. VirtualBox is the default virtualization software supported by Vagrant, it’s free and available for Linux, Windows and Mac. You can use VmWare too, but it’s not covered by this tutorial.

Installing your first box

After the setup,open a console and type:

mkdir myProject
cd myProject
vagrant init "ubuntu/trusty64"

The last command wil ask Vagrant to create a new environment based on the “ubuntu/trusty64” box inside your project folder. A file called vagrantfile will be created in your project root, it will contain the configuration for your virtual machine. In this tutorial we’ll use ubuntu/trusty64 box as a base for our environment, it’s nothing more than a basic Ubuntu LTS virtual machine ready for Vagrant. There are many boxes available on the official repository.

Power up your your virtual machine

Power up your virtual machine with:

vagrant up

Vagrant up will boot your virtual machine, downloading the base image if not already available on your pc.

SSH access

When done, access your machine using:

vagrant ssh

If asked, the default ssh password is “vagrant”. Your project folder is mounted on /vagrant.

Note: You need the ssh command available in your path. Windows doesn’t have a native ssh client, you must install OpenSSH or use Putty.

Port forwarding and provisioning

You can’t simply install everything with apt-get, it will work but you’ll have to do the same on every machine, there is a better way, called provisioning. There are some softwares, such as Chef to provion a machine, but a bash script will work as well.

Open your Vagrantfile and set it up like this:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
 config.vm.box = "ubuntu/trusty64"

 # FORWARD PORT 80 TO 8080
 config.vm.network "forwarded_port", guest: 80, host: 8080

 # RAM AND CPU SETTINGS
 config.vm.provider "virtualbox" do |vb|
   vb.memory = 2048
   vb.cpus = 2
 end

 # PROVISIONING SCRIPT
 config.vm.provision :shell, path: "init.sh"

end

With this setup we defined ubuntu/trusty64 as our base box, and we are forwarding guest port 80 to our host’s 8080. We are also setting up the vm to use up to 2gb of ram and 2 cpu cores.

The last line is about provisioning: it tells Vagrant to run a file named init.sh to configure the machine. By default, all the paths are relative to our project’s folder.

Now create the init.sh file and place it on the project’s root, in this file we’ll write all the commands to make the server ready, in our example we’ll install Apache, Php, Mysql, PhpMyAdmin, and we’ll replace /var/www/html with a link to a folder named source inside our project folder.

#!/usr/bin/env bash

#INSTALL APACHE, PHP AND MYSQL
apt-get update
apt-get -y install apache2 php5 libapache2-mod-php5
debconf-set-selections <<< 'mysql-server mysql-server/root_password password vagrant'
debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password vagrant'
apt-get -y install mysql-server

#INSTALL PHPMYADMIN
debconf-set-selections <<< 'phpmyadmin phpmyadmin/dbconfig-install boolean true'
debconf-set-selections <<< 'phpmyadmin phpmyadmin/app-password-confirm password vagrant'
debconf-set-selections <<< 'phpmyadmin phpmyadmin/mysql/admin-pass password vagrant'
debconf-set-selections <<< 'phpmyadmin phpmyadmin/mysql/app-pass password vagrant'
debconf-set-selections <<< 'phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2'
apt-get -y install phpmyadmin

#LINK /var/www/html TO /vagrant/source
rm -rf /var/www/html
ln -s /vagrant/source/ /var/www/html

I used debconf-set-selections and apt-get -y so that the script won’t stop to ask about settings and passwords. MySql’s root password is set to “vagrant”.

Test it

Ok, let’s create an index.php file inside the source folder, a simple phpinfo is enough:

<?php

phpinfo();

Back to the console, run vagrant up to start the vm, or vagrant provision if the vm is already running and we only need to re-run provisioning.

Relax and drink a coffee while provisioning is doing the dirty job… After a few minutes open your browser, go to http://localhost:8080 and you should see the phpinfo page.