Quantcast
Channel: WordPress Tips & Tricks - SEO - Hosting Review » Hosting-Domain
Viewing all articles
Browse latest Browse all 7

How To Creat Virtual Machine With Vagrant and Virtualbox

$
0
0

Physical-Into-Virtual-Machine-Banner

Normally, to use VPS (Virtual Private Server), you have to spend some money to pay for these services. But for many IT people, the limit of spending money is necessary.

So if you want to learn VPS without paying cash for it. It is possible, you can install the software that supports virtual machine like VirtualBox, VMWare, and install CentOS or Ubuntu operating system in order to make a virtual Linux server to learn.

Using VirtualBox or VMWare is very good way to learn, but the problem here is the time you will take to install from A to Z, and not everyone can install it smoothly without errors.

In this article, I will introduce you another tool that you can use with VirtualBox or VMWare to create virtual machines in the fastest time, the easiest and most professional tools such as Vagrant.

What is Vagrant?

Vagrant is a dedicated applications for VirtualBox and VMWare to support users to create virtual machines on the computer according to your needs with command line supporting. Especially you can move the virtual machine settings in your computer to another computer (re-package), or allow members of the same team with your own access to the directory on the server to edit/view files that they do not need to install anything, as long as the shared LAN (local Network – local Area network), even you can put your server on the internet environment with just a few simple commands.

Or if you’re deploying applications on Cloud servers of Amazon EC2 or Rackspace server, Vagrant can help you easily setup like the way you do on your own computer and sync settings for the other server with just a few commands without losing the setup individually. Vagrant also supports most of the current operating system.

In this article, I summary the basic of Vagrant, after clearly about it, you will how useful it is.

Video Instructions

How to install Vagrant

In this article, I will guide how to install and use Vagrant on Windows (Windows 8.1), but if you like to use the Linux operating system like Ubuntu, Mac, etc, it will be much easier to install because you have already terminal, just follow it.

Step 1. Install VirtualBox

Before you install Vagrant, you should install VirtualBox because if you want to use Vagrant with VMWare, you have to pay money. Download VirtualBox and install it, it is free software.

virtualbox

Step 2. Install Git Bash

Why do we use Git here? Because we will use and control Vagrant via Linux commands, it will be a little inconvenient to use the CMD in Windows. Therefore we need an emulator software for UNIX environments and Git tool supports this, otherwise Vagrant is very helpful in using Git.

First you visit http://git-scm.com/download/win to download Git for Windows. Then run the downloaded file to install and select the options as shown below:

git-install-windows

git-install-windows-2

git-install-windows-3
After installation, you can see an app called Git Bash and Git GUI at Start menu on your computer, we only use Git Bash. Open up the windows you will have a command like this:

git-bash

Step 3. Setup Vagrant

Vagrant application will automatically add the command line on Windows/Linux. First, you need to access and download the installation package https://www.vagrantup.com/downloads.html with corresponding packages of your current operating system.

Download and install the software as usual. Restart a computer. Then open Git Bash, type vagrant -h.

vagrant-help

Usage Vagrant to create virtual machines

In Vagrant, it is a concept that you need to know is Box. Box means a package with multiple operating systems with built-in applications, such as box CentOS 6.5 32bit, CentOS 6.5 64bit, Ubuntu 12.04 with LAMP, etc. You can download this box easily.

When using Vagrant, you will need to create a separate folder for each virtual machine and each folder will contain the setting for one virtual machine. Because when you start, you will need access to the directory of the virtual machine to boot up and use vagrant command to start it up.

All operations at Vagrant via command lines.

Step 1. Load the box for Vagrant

Once installed, there will be no available box in Vagrant, you will have to load it on your computer. List of the box and its path you can see in https://vagrantcloud.com/discover/featured.

Now let’s open up the Git Bash and type the following commands:

vagrant box add chef/centos-6.5-i386
  • vagrant: Here is the syntax required to write when you want to use the application vagrant.
  • box: components need to interact on Vagrant, here we need to interact with the box.
  • add: an action to interact with the component box.
  • chef/CentOS-6.5-i386: name in the address box to load https://vagrantcloud.com/discover/featured.

It will ask you to load the box to the application if you are using VirtualBox, then type 1 and Enter.

vagrant-add-box

Type vagrant box to list the current box that we have.

vagrant-box-list

So this step is to finish the box loading.

Step 2. Create a new virtual machine

To create a new virtual machine, you need to create a separate folder for it, you can type the following command to create a directory named VM1:

mkdir VM1

The directory will be created by default located at C: \Users\UserName\.

Access to that folder:

cd VM1

You can type pwd command to see where you are.

Then use the following command to setup new virtual machine.

vagrant init centos65

You will see the notice:

vagrant-init-finish

It means your Vagrantfile is already loaded in your virtual machine. Here is my Vagrantfile of box centos65:

 # -*- 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| # All Vagrant configuration is done here. The most common configuration # options are documented and commented below. For a complete reference, # please see the online documentation at vagrantup.com. # Every Vagrant virtual environment requires a box to build off of. config.vm.box = "centos65" # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "<a href="http://thachpham.com/hosting-domain/huong-dan-localhost.html" title="localhost"><strong>localhost</strong></a>:8080" will access port 80 on the guest machine. # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to the bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network "public_network" # If true, then any SSH connections made will enable an agent forwarding. # Default value: false # config.ssh.forward_agent = true # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Don't boot with headless mode # vb.gui = true # # # Use VBoxManage to customize the VM. For example to change memory: # vb.customize ["modifyvm", :id, "--memory", "1024"] # end # # View the documentation for the provider you're using for more # information on available options. # Enable provisioning with CFEngine. CFEngine Community packages are # automatically installed. For example, configure the host as a # policy server and optionally a policy file to run: # # config.vm.provision "cfengine" do |cf| # cf.am_policy_hub = true # # cf.run_file = "motd.cf" # end # # You can also configure and bootstrap a client to an existing # policy server: # # config.vm.provision "cfengine" do |cf| # cf.policy_server_address = "10.0.2.15" # end # Enable provisioning with Puppet stand alone. Puppet manifests # are contained in a directory path relative to this Vagrantfile. # You will need to create the manifests directory and a manifest in # the file default.pp in the manifests_path directory. # # config.vm.provision "puppet" do |puppet| # puppet.manifests_path = "manifests" # puppet.manifest_file = "default.pp" # end # Enable provisioning with chef solo, specifying a cookbooks path, roles # path, and data_bags path (all relative to this Vagrantfile), and adding # some recipes and/or roles. # # config.vm.provision "chef_solo" do |chef| # chef.cookbooks_path = "../my-recipes/cookbooks" # chef.roles_path = "../my-recipes/roles" # chef.data_bags_path = "../my-recipes/data_bags" # chef.add_recipe "mysql" # chef.add_role "web" # # # You may also specify custom JSON attributes: # chef.json = { mysql_password: "foo" } # end # Enable provisioning with chef server, specifying the chef server URL, # and the path to the validation key (relative to this Vagrantfile). # # The Opscode Platform uses HTTPS. Substitute your organization for # ORGNAME in the URL and validation key. # # If you have your own Chef Server, use the appropriate URL, which may be # HTTP instead of HTTPS depending on your configuration. Also change the # validation key to validation.pem. # # config.vm.provision "chef_client" do |chef| # chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME" # chef.validation_key_path = "ORGNAME-validator.pem" # end # # If you're using the Opscode platform, your validator client is # ORGNAME-validator, replacing ORGNAME with your organization name. # # If you have your own Chef Server, the default validation client name is # chef-validator, unless you changed the configuration. # # chef.validation_client_name = "ORGNAME-validator" end

If you want to setup Webserver on a virtual machine, do as following:

Find:

 # config.vm.network "forwarded_port", guest: 80, host: 8080

Remove the #

Means we forward the accessing from port 8080 to port 80.

Now open up the Git Bash and go to folder vm1, restart the virtual machine:

vagrant up

vagrant-up-finish

Finally, type vagrant ssh to access to a virtual machine via SSH.

virtualbox-vagrantup

The basic command lines of Vagrant

There are some important command lines you should remember

  • vagrant box add – Load box.
  • vagrant box list – View list box.
  • vagrant suspend – For a virtual machine pauses.
  • vagrant halt – For a virtual machine to sleep, shutdown it.
  • vagrant destroy – For virtual machines in the garden.
  • vagrant login – Login to Vagrant Cloud system.
  • vagrant share –ssh: Share your virtual machine access to others, you must type the command vagrant login before using this feature.
  • vagrant reload: Reload Vagrantfile file settings in the virtual machine, the edited file you must use the vagrant halt prior to shutting down the virtual machine, then use the reload command to reload the configuration.

Some common questions

Using Root

By default, we use vagrant ssh to access SSH, and you will use the user vagrant, not root. Thus each command you use, you have to put sudo in front of. However, you can not use SFTP to access and edit the configuration file because there is no root permit.

To access SSH as root. You use another software such as PuTTY to access the server on your computer with information is:

  • IP: 127.0.0.1
  • Username: root
  • Password: vagrant
  • Port: 2222

Or you can go to the vagrant via SSH command, then type su to switch user root.

Note: Each box can have different settings, but its settings are displayed when you have finished typing the command conducted vagrant up. Except for root pass, according to my tests, the majority of the box without root pass applications are the vagrant.

Accessing web server

If you have LAMP or LEMP, every time you want to access your web server, you need to to http://127.0.0.1:8080 because Vagrant only supports port 8080.

Timeout error when connecting to SSH

If you have a timeout error when connecting to the server via SSH by vagrant ssh command, there will be two common reasons are as follows:

You are using a virtual machine to install Vagrant, resources are lacking.
Use the operating system is not compatible with the hardware. For example, you have a virtual machine that only a few hundred MB of RAM to use the 64bit version.

How can I add multiple domains on a web server?

When installing LAMP or LEMP, you just add the domain by VirtualHost as usual. Then open the hosts file on your computer and add the IP domains pointing to 127.0.0.1. For example:

Thao Nguyen 127.0.0.1

Conclusion

Congratz, you have come to the end of this article, now you can build your virtual machine to learn VPS. In the next article, you will guide you to create your own box to re-use without installing again and share that box to the VagrantCloud library for friends to use.

If you have any question, do not hesitate to ask me.

The post How To Creat Virtual Machine With Vagrant and Virtualbox appeared first on WordPress Tips & Tricks - SEO - Hosting Review.


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images