At its core, Vagrant provides developers with disposable environments. Not just any environment, a reproducable environment that ensures consistency when developing. This allows also for DevOps to test server scripts such as those used by puppet, chef and friends.Installing
First of all, do NOT use the VirtualBox or Vagrant packages from your package management. They are out of date. Instead, get the latest downloads for your OS/Distribution from the respective download pages for VirtualBox and Vagrant.
Vagrant sits on top of virtual machine tools, such as VMWare or VirtualBox. In this tutorial, VirtualBox will be used. For this purposes of this tutorial, the host system is ubuntu.
At the time of this writing, the latest versions are available as below
libxml2 is also required, so should be OK to use for your package management.
Install the tools
sudo dpkg -i https://releases.hashicorp.com/vagrant/1.8.1/vagrant_1.8.1_x86_64.deb
sudo dpkg -i vagrant_1.8.1_x86_64.deb
To test if all is well, check the version of vagrant
With the installation complete, things only get easier with Vagrant. The first step is to create a directory to hold our environment.
Then change into this directory
Now we can run the init command, along with the name of the image to use.
You should see something like this
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 `vagrantup.com` for more information on using Vagrant.
This is the same as running the following two commands.
vagrant box add hashicorp/precise64
Because we used init with the hashicorp/precise64 the config is already done in the Vagrantfile, and our first machine is ready to start up. To start the vagrant machine.
This will take a minute, and when done, will display something like this
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'hashicorp/precise64'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'hashicorp/precise64' is up to date... ==> default: Setting the name of the VM: QRdvark_test_default_1456744445668_2535 ==> default: Fixed port collision for 22 => 2222. Now on port 2200. ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2200 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2200 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Remote connection disconnect. Retrying... default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.2.0 default: VirtualBox Version: 5.0 ==> default: Mounting shared folders... default: /vagrant => /home/kevin/QRdvark_test
Thats it! Your first machine is up and running with vagrant.SSH Into Box
To access the new box with SSH, simply use this command
And the result should look a little like this.
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64) * Documentation: https://help.ubuntu.com/ New release '14.04.4 LTS' available. Run 'do-release-upgrade' to upgrade to it. Welcome to your Vagrant-built virtual machine. Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
With the new box installed, it is basically just a blank operating system. New programs can be installed on the box using apt-get as you would with any other box. But, we want our installs to be the same across every instance. To this end, Vagrant allows you to Provision the installation.
Because this is a PHP site, the QRdvark_test instance can be provisioned with PHP and Apache2. As apache2 is a dependency of PHP, only the php5 needs to be given as an arguement to apt-get in our provisioning script.
A provision script, is like a bootstrap. Acting to supply configurations to the application. So lets take a looks. With your favourite text editor, create a file called bootstrap.sh and in it, put the following.
#!/usr/bin/env bash apt-get update apt-get install -y apache2 php5 if ! [ -L /var/www ]; then rm -rf /var/www ln -fs /vagrant /var/www fi
This simply script will do just as it looks. An update, followed by the installation of php5 (apache2 will be installed also as a dependency), and then creates a symlink for our www, web root, directory.
With this file in place, the vagrant application needs to know where to find it. So, in our Vagrantfile this line can be added.
config.vm.provision :shell, path: "bootstrap.sh"
Finally, vagrant needs to be reloaded, to pick up the changes in the configuration.
Again, this will take a minute, so be patient, as the machine is updated, and the new packages installed by the systems package management tools.
vi Vagrantfile kevin@jyotish:~/QRdvark_test$ vagrant reload --provision ==> default: Attempting graceful shutdown of VM... ==> default: Checking if box 'hashicorp/precise64' is up to date... ==> default: Clearing any previously set forwarded ports... ==> default: Fixed port collision for 22 => 2222. Now on port 2200. ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2200 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2200 default: SSH username: vagrant default: SSH auth method: private key ... ... ... ==> default: Creating config file /etc/php5/cli/php.ini with new version ==> default: update-alternatives: using /usr/bin/php5 to provide /usr/bin/php (php) in auto mode. ==> default: Setting up ssl-cert (1.0.28ubuntu0.1) ... ==> default: Processing triggers for libc-bin ... ==> default: ldconfig deferred processing now taking place
With the current directory now linked to the vagrant directory, we can create a file to test the web service.
<?php phpinfo(); ?>
Although the web server is up and running, it cannot be accessed as there is no network to the machine. To create the network connection, another line needs to be added to the Vagrantfile.
Then, we reload again so the new config option is used.
The address http://127.0.0.1:5678 should now show the result of phpinfo();