Sunday, July 2, 2017

Vagrant and Virtual box

This post is one of a series

Create an environment for Web development
  1. Vagrant/Virtualbox - You are here
  2. Install Apache or Nginx for PHP or Nginx for Python
  3. Install MySQL OR Install MariaDB
  4. Install PHP / Install Python

Install Vagrant/Virtualbox

What vagrant/virtualbox are:
Vagrant comes with support out of the box for VirtualBox, a free, cross-platform consumer virtualization product. -- HashiCorp
Vagrant/vitualbox are softwares to create virtual machines in which we can run programs and they are usually used for development. Web programmers usually use Vagrant/virtualbox to make a virtual machine and develop a web app inside so that we don't need to buy or subscribe for a server just for development. (Or you can use Docker if the production sever is using Docker.)
Install the softwares from here:

Check if Vagrant/Virtualbox is installed properly

When the installing is finished, double-click "Oracle VM VirtualBox" and if it works, Virtual Box is successfully installed. To check if Vagrant is properly installed, open Windows Powershell (which should by default installed in your Windows, search for "Windows Powershell" from the start menu). Then run this command:
vagrant -v
If Vagrant version is displayed, Vagrant is successfully installed.

Initialize the Box

Create the Vagrantfile
We will use Centos/7 to initialize the box. What is "Box"?
Boxes are the package format for Vagrant environments. A box can be used by anyone on any platform that Vagrant supports to bring up an identical working environment. -- HashiCorp
Well, box is like a seed from which we create a virtual machine (like Ubuntu, CentOS, even Windows). We will use centos/7 to create a virtual CentOS 7 (which we will use as a local server for development).
The location where we initialize the box can be anywhere. But maybe we will initialize the box here: C:\vagrant\CentOs (create the directory if such location doesn't exist. Folders can be created just by right click and "create the new folder")

Run these commands:
cd C:\vagrant\CentOs
vagrant init centos/7
Then the Vagrantfile will be created there.

Vagrant up
Right-click on the Vagrantfile and open it with Notepad.
"Vagrantfile" is a file where you can configure the virtual machine of Vagrant. It will be opened like this:
"#" means "this line is a comment", so these lines with "#" will be ignored when the program is executed. So we need to delete the "#" to make the line un-commented. Delete the 35th line's "#" which says "config.vm.network "private_network", ip: "192.168.33.10"". And add this line bellow it:
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"

And save the file.
(By the way, "Sublime Text" is much better than Notepad)
Now run these commands:
cd C:\vagrant\CentOs
vagrant up
The creatation of the virtual machine will start:

To stop the virtual machine, use this command:
vagrant halt
To restart the virtual machine, use this command:
vagrant reload

Install vagrant-vbguest

Run this command to install vagrant-vbguest:
vagrant plugin install vagrant-vbguest
This is necessary to check version difference of "GuestAdditions" between the guest and the host.
To check if the version is correct:
vagrant vbguest --status
To adjust the version difference:
vagrant vbguest

SSH connection to the virtual machine

Maybe usually Putty is used for the SSH connection. But personally I like using Teraterm.
If you are using Linux or Mac, you can simply use the following:
$ vagrant ssh
If you are using Windows, you must use Teraterm (or Putty). Download and install Teraterm. Then start Teraterm. Write "192.168.33.10" in the "Host" (this is the IP address we wrote in the Vagrantfile) and click the "OK" button.
Now you will see this display.
  1. Write "vagrant" in the username.
  2. Click on "Use RSA/DSA/ECDSA.ED25519 key to log in".
  3. Click on "Private key file".

Then we need to specify where the private key is. You will find the private key in the lowest directory of this ".vagrant" folder (which exists in the folder where you did the "vagrant up" command):

Open the folders and find the private_key. Click on it to choose it as the private key and click on "Open" button.

(If you can't see any file, change the file type to "all".)

Now just click on "OK" to log in the virtual machine.

You will see this if you successfully log in:

Install necessary modules and restart the virtual machine

Run these commands on the Teraterm and install necessary modules to sync files between the virtual machine (guest) and the host computer.
$ sudo yum -y install kernel-devel kernel-headers dkms gcc gcc-c++ 
$ sudo yum -y update kernel
By the way, such commands for Linux, Mac are often written with "$" (not for Windows commands though).
"$" means "you are a normal user" (not a root user). So you don't need to write "$" when you use the command. It is written just to indicate what user type you should use.
And run this command on the Windows Powershell to restart the virtual machine.
cd C:\vagrant\CentOs
vagrant reload
Log in the virtual machine again. If you run this command:
$ ls /vagrant
You should see green letters "Vagrantfile". (If not green, try create some random text files in the folder and see if the files are instantly synced in the virtual machine's "/vagrant" folder. If they get synced, the configuration is success.)

Then you can create random files or folders in the folder "C:\vagrant\CentOs" (where we created to init the box) in the host computer. The files in the folder are immediately synced in the "/vagrant" folder in the guest machine. "ls" command we used can be used to check what is inside the folder, so use "ls /vagrant" to see inside the folder in CentOS. If not synced, something is wrong.
If everything works fine, run this command on the Teraterm to update existing modules in the CentOS.
$ sudo yum -y update
$ sudo yum -y upgrade

Useful Linux commands

See here for useful Linux commands.