Sunday, April 8, 2018

Vagrant in Ubuntu Linux

As a developers, we have a lot of development environment depends on our projects, sometimes each project has different environment specifications and it's difficult to re adjust.

With Vagrant we can make development environment for PHP 5.3 and PHP 5.4, MySQL, SQLite, MongoDB, Postgres, PEAR, PHPUnit, Rails 3.1, Memcached, Redis, Gearman, NodeJS in instance. It means, we doesn't need to remove and install different development environment for each of our projects. We just need  Vagrant  to manage our instance virtual machines.




First Step
     Installing VirtualBox to your machine:

  • sudo apt-get install virtualbox
  • sudo apt-get install virtualbox-dkms
   Or you can download VirtualBox from https://www.virtualbox.org/wiki/Downloads, then install that package in your local machine.

Second Step
     Installing Vagrant:
  • sudo apt-get install vagrant
Third Step
     Go to Your directory projects or create new directory projects.
  • mkdir <project_name> && cd <project_name>
  • echo “Hello! with sync to Virtual Machine” > Readme.md
Fourth Step
     Initialize projects with Vagrant project Environment
  • vagrant init
   This will create "Vagrantfile" file.

Five Step
     Open "Vagrantfile" file, it will contain something like this:

  VAGRANTFILE_API_VERSION = "<no>"
  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "<name_your_box>"
    config.vm.box_url = "<path_to_package.box>"
    config.vm.network :private_network, ip: "<the.ip.add.number>"
    config.vm.provision :puppet do |puppet|
      puppet.manifests_path = "<path/to/manifests/folder>"
      puppet.manifest_file = "<name_file_puppet.pp>"
    end
  end

     This is sample of Vagrant configuration file:

  VAGRANTFILE_API_VERSION = "2"
  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "trusty64"
    config.vm.box_url = "https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-14.04-amd64-vbox.box"
    config.vm.network :private_network, ip: "192.168.0.33"
    config.vm.provision :puppet do |puppet|
      puppet.manifests_path = "manifests"
      puppet.manifest_file = "default.pp"
    end
  end


     Descriptions:
  • config.vm.box = This configures what box the machine will be brought up against. The value here should be the name of an installed box or a shorthand name of a box in HashiCorp's Atlas.
  • config.vm.box_url = for setup source of package vagrant box, for complete list of vagrant box you can found here : http://www.vagrantbox.es/ .If not specified, no checksum comparison will be done. If specified, Vagrant will compare the checksum of the downloaded box to this value and error if they do not match. Checksum checking is only done when Vagrant must download the box.
  • config.vm.network = ip that use for communication between VM and local.
  • config.vm.provision = Configures provisioners on the machine, so that software can be automatically installed and configured when the machine is created. Please see the page on provisioners for more information on how this setting works.
  • puppet.manifests_path = path to folder manifests where to put file manifest configuration. (required by puppet)
  • puppet.manifest_file = name of file manifests configuration.

Six Step
     Setup Puppet Configurations:
  • mkdir manifests && cd manifests
  • touch default.pp
Seven Step
     Next open file <project_directory>/manifests/default.pp, add :

class system-update {
  # execute 'apt-get update'
  exec { 'apt-update': # exec resource named 'apt-update'
    command => '/usr/bin/apt-get update --fix-missing' # command this resource will run
  }
}
include system-update
class apache-setup {
  # install apache2 package
  package { 'apache2':
    require => Exec['apt-update'], # require 'apt-update' before installing
    ensure => installed,
  }
  # ensure apache2 service is running
  service { 'apache2':
    ensure => running,
  }
}
include apache-setup
# install php5 package
package { 'php5':
  require => Exec['apt-update'], # require 'apt-update' before installing
  ensure => installed,
}
# ensure info.php file exists
file { '/var/www/html/info.php':
  ensure => file,
  content => '<?php phpinfo(); ?>', # phpinfo code
  require => Package['apache2'], # require 'apache2' package before creating
}

Eight Step

  • vagrant up

Sample of working Local configurations.

Notes

  • To Login to Vagrant Box:  vagrant ssh
  • cd vagrant/
  • Congratulations, you're in VagrantBox!






Share:

0 comments:

Post a Comment