Thursday, July 10, 2014

Vagrant basic plus multiple servers.

This article shows how to install ubuntu trusty server automatically on Virtual box by using "Vagrant" tool.(

First, we have to install virtual box( and vagrant.
I used mint Linux as a workstation. This distro is very gorgeous. Just personal taste.

 Vagrant version: (1.6.3).   Linux Mint 17 Qiana \n \l

oyj@oyj-ThinkPad-Edge-E535 ~ $ mkdir vt
oyj@oyj-ThinkPad-Edge-E535 ~ $ cd vt/
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ ls
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`` for more information on using Vagrant.

Find suitable image from below link. I shall use ubuntu server trusty.
Click popular link. There is ubuntu/trusty64 that is offcial image from Canonical.

Edit below line from "base" to "ubuntu/trusty64" = "ubuntu/trusty64"

oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vt_default_1405043268291_38468
==> default: Fixed port collision for 22 => 2222. Now on port 2202.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2202 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address:
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /home/oyj/vt

Ubuntu server image shows up virtual box screen.

*Sshing to this server.

oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant ssh
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:

 System information disabled due to load higher than 1.0

Last login: Wed Jul  9 04:13:03 2014 from

* Packaging.
Not to download image from "" everytime that I want to install servers, I decided to package image.

oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant package
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: /home/oyj/vt/
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ ls

* I want to make three servers for development. 1. web-apache, 2. tomcat7, 3. mysql server.
Here is Vagrantfile that I made. It is easy to understand. So I think there is no need to explain every lines. :)

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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.define "apache" do |web| = "apache"
    web.vm.box_url = "file:///home/oyj/vt/"
    web.vm.provision "shell", inline: "sudo apt-get update; sudo apt-get -y install apache2" "private_network", ip:""

  config.vm.define "tomcat7" do |was| = "tomcat7"
    was.vm.box_url = "file:///home/oyj/vt/"
    was.vm.provision "shell", inline: "sudo apt-get update; sudo apt-get -y install tomcat7" "private_network", ip:""
# To install mysql servers I used shell script
  config.vm.define "mysql" do |db| = "mysql"
    db.vm.box_url = "file:///home/oyj/vt/"
    db.vm.provision "shell", :path =>  "" "private_network", ip:""


* script
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ cat
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password rootpass'
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password rootpass'
sudo apt-get update
sudo apt-get -y install mysql-server-5.5

* Now running vagrant up and connecting using ssh.

oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant up
Bringing machine 'apache' up with 'virtualbox' provider...
Bringing machine 'tomcat7' up with 'virtualbox' provider...
Bringing machine 'mysql' up with 'virtualbox' provider...
==> apache: Importing base box 'apache'...


* There is no problem :).

* sshing.
oyj@oyj-ThinkPad-Edge-E535 ~/vt $ vagrant ssh apache
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:

  System information as of Thu Jul 10 22:27:13 EDT 2014

  System load:  0.82              Processes:           86
  Usage of /:   19.4% of 6.99GB   Users logged in:     0
  Memory usage: 29%               IP address for eth0:
  Swap usage:   0%

  Graph this data and manage this system at:

Last login: Wed Jul  9 04:13:03 2014 from

Same as mysql and tomcat7.

No comments:

Post a Comment