Saturday, May 13, 2017

Virtual box and Vagrant

Contents

1.  Virtual box and Vagrant

2.  Install Apache

3.  Install MySQL

4.  Install Python

5.  Hello World with Python


1. Download Virtual Box and Vagrant

We can use Virtual Box and Vagrant to make a development environment for Python. They are mainly used to make virtual servers in a computer, in which we can run Python programs. We will use them and practice making Python programs in the virtual server. (These virtual machines can be deleted and created easily, so we don't need to worry if our program might harm the virtual server.)

Virtual Box - https://www.virtualbox.org/wiki/Downloads
Vagrant - https://www.vagrantup.com/

2. Check

When the download and install are finished, double-click "Oracle VM VirtualBox" and if it works, Virtual Box is successfully installed.

To check if Vagrant is successfully installed, do the following command on Command prompt (cmd):

vagrant -v

If you don't know where command prompt is, search with the word "cmd". Then command prompt should be opened.

If Vagrant version is displayed, Vagrant is successfully installed.

By the way, this command can be written like this, too:

$ vagrant -v

"$" means "you are a normal user" or "you are not a root user". So you don't need to write "$" when you use the command. It is written just to indicate what user you should be.

If version isn't displayed, something is wrong. Re-install Virtual Box and Vagrant.


3. A folder for the Virtual machine

We will create a folder to put the virtual machine inside. Create a folder under C:\. The name is "MyVM".


Inside the MyVM folder, we will create a folder again. Create a folder that is named as "MyCentOs".

When we finish creating them, move to the MyCentOs folder by the following command:

cd C:\MyVM\MyCentOs


4. Initialize Box

We will use CentOS 7 (Linux), so we will use a Box that is called "bento". Box is a package that is used to create a virtual machine.
Bento: https://atlas.hashicorp.com/bento

To use the bento box, do the following command:

vagrant init bento/centos-7.3


Then "Vagrantfile" will be created in your directory.
Right-click on the Vagrantfile and open it with Notepad.



"Vagrantfile" will be opened like this:


"#" means "this is just a comment, so this can be ignored when the program is executed", so if "#" is deleted, that will NOT be ignored when executed.

Delete one # to make it taken into consideration when Vagrant is executed.
(ici means "here" in French by the way)

(By the way, "Sublime Text" is much better than Notepad)

5. Vagrant up

When everything is done, start Vagrant with a command "vagrant up" on command prompt:

$ vagrant up

And your virtual Cent OS 7 will be started. This might take some time.

If you failed mounting the shared folder, look at this post:
vagrant mounting failed with the error: No such device

To stop Vagrant, use this command:

vagrant halt

To restart Vagrant, use this command:

vagrant reload


6.  SSH connection

To connect the virtual Cent OS with SSH, if you are using Windows OS, we need to use Teraterm or Putty.
Download Teraterm from here: https://ttssh2.osdn.jp/index.html.en
Download Putty from here: https://www.chiark.greenend.org.uk/~sgtatham/putty/

I will use Teraterm.

If you are using Mac or Linux, I think you can connect to the virtual machine with this command:

$ vagrant ssh

And to exit from the vagrant ssh mode:

$ exit

Here, we suppose we are using Windows.

Run Teraterm at first. Your Host is defined in Vagrantfile as written above, so it is "192.168.33.10"



The user name is "vagrant", and the password is "vagrant" too.


Then we can connect to the virtual machine with SSH. Although this looks like a real server, it is working on locally.


7. Update

When we finish all of them written above, we will update "yum". yum is used for a variety of purposes like to install or update software for Linux.

Do these commands on Teraterm:

$ sudo yum -y update
$ sudo yum -y upgrade



We will install "vagrant-vbguest" that automatically checks difference of version of "GuestAdditions" and the host machine. Do these commands on commpand prompt. (not on Teraterm this time!)

vagrant plugin install vagrant-vbguest

To check if version is correct, do the following command:

vagrant vbguest --status

To adjust version difference of the GuestAdditions, do the following command:

vagrant vbguest

On the other hand, Oracle virtual box is not used usually. If you can use Vagrant without any problem, that's enough. When you are using Vagrant, if you face a weird error one day and if you want to check the virtual machine's status or forcefully shut down the virtual machine, start the Oracle virtual box and use it for such purposes.

If the mounting of shared folder fails:
vagrant mounting failed with the error: No such device