Vagrant installation Configuration development environment in Linux

Source: Internet
Author: User
Tags pack ssh

Playing Linux under Windows is a virtual machine, but installing the system is still quite a bother. Now everything is automated today, vagrant is such an artifact, based on Ruby Development, using open source VirtualBox as virtualization support, you can easily deploy across platforms.
Today try to change the environment of several machines to vagrant, very cool and smooth, so I tried to tidy up the use of summary:
 
currently selected is the latest version of the current Vagrant 1.2.7 (vagrantfile V2 version), VirtualBox is using 4.2.16
Prepare:
Download installation virtualbox:https://www.virtualbox.org/
Download installation vagrant:http:// www.vagrantup.com/
Download the box you want to use:
An official example: Http://files.vagrantup.com/precise32.box
can also be used in http:// www.vagrantbox.es/here to download more different systems and even the environment has been configured directly to use the box, although the direct use of the Web site directly in the vagrant, by vagrant Automatic download installation, but given the network situation, or recommend their own first download good.
Because of the habit of using CentOS, so I centos 6.3 x86_64 minimal, this box according to individual circumstances to choose.
Setting up the environment:
After the installation is complete and the system is restarted, you can start setting up the environment.
First create a good working directory locally and switch to the corresponding directory at the command line
 
1 vagrant box Add base centos-6.3-x86_64-minimal.box


Base means to specify the default box, or to specify a name for the box, such as CENTOS63, when using base, you can then initialize directly using Vagrant init, and if you specify a name, you need to specify the name of the box when initializing.
Centos-6.3-x86_64-minimal.box is the file name of box, this can be the path to save the box locally, you can also download the box URL, if it is the Web site, vagrant will automatically start the download.

1 [vagrant] downloading with vagrant::D ownloaders::file ...

2 [vagrant] copying box to temporary location ...

3 [vagrant] extracting box ...

4 [vagrant] Verifying box ...

5 [vagrant] cleaning up downloaded box ...


After box is set, run in the current working directory

1 vagrant Init


Generates the corresponding vagrantfile. Opening vagrantfile through a text editor allows for some further common configurations:
Network configuration:
There are three different modes of vagrant network
1, more commonly used is the port mapping, that is, the virtual machine in the port map to the host corresponding to the port directly used in the Vagrantfile configuration:

1 Config.vm.network:forwarded_port, guest:80, host:8080


GUEST:80 represents the 80 port in the virtual machine, host:8080 represents the 8080 port mapped to the host.
2, if you need to freely access the virtual machine, but others do not need access to the virtual machine, you can use Private_network, and set up IP for the virtual machine, in the Vagrantfile configuration:

1 config.vm.network:p rivate_network, IP: "192.168.1.104"


192.168.1.104 represents the virtual machine IP, multiple virtual machines need to access each other, set in the same network segment can
3, if you need to use the virtual machine as a computer in the current LAN, the LAN for DHCP, then in the Vagrantfile configuration:

1 config.vm.network:p Ublic_network


Directory mappings:
Since it is a development environment, the development work must still be done locally, instead of all going into the virtual machine to complete, the virtual machine will be good in the background to run the service is better, or put the cart before the horse, so here you need to use the directory mapping function, the local directory map to the corresponding directory of the virtual machine.
By default, the current working directory is mapped to the/vagrant directory of the virtual machine, the files in the current directory can be accessed directly under/vagrant, and of course you can create a soft connection through LN, such as

1 ln-fs/vagrant/wwwroot/var/www


For directory mapping, of course, from an automated configuration point of view, can not enter the system does not need to enter the system, so in vagrant can also do directory mapping operations:

1 Config.vm.synced_folder "wwwroot/", "/var/www"


The previous argument "wwwroot/" represents the local path, where the relative path to the working directory is used, and the absolute path can be used here, for example: "d:/www/"
The following parameter "/var/www" indicates the directory of the corresponding mappings in the virtual machine.
To run the script:
Although not required, if there is a need to run some scripts at startup (the installation of the environment or the startup of some services needs to be done after the directory mapping is complete), you can edit the script, similar to the following (excerpt from Vagrant Document):
1 #!/usr/bin/env Bash

2

3 Apt-get Update

4 Apt-get install-y Apache2

5 rm-rf/var/www

6 ln-fs/vagrant/var/www

Save in the same directory as vagrantfile, file name yourself (such as boot.sh), and then add in Vagrantfile:
1 Config.vm.provision:shell,:p ath => "boot.sh"

After the initial use of the basic settings is complete, you can use vagrant up to start the virtual machine
Bringing machine ' default ' up with ' VirtualBox ' provider ...

[Default] Setting the name of the VM ...

[Default] clearing any previously set forwarded ports ...

[default] Creating Shared Folders metadata ...

[Default] clearing any previously set network interfaces ...

[Default] Preparing network interfaces based on configuration ...

Modified [default] are trying to forward to privileged ports (ports < = 1024). Most operating systems restrict this to only privileged process (typicallyprocesses running as a administrative user). This is a warning in case

The port forwarding doesn ' t work. If any problems occur, please try a port higher than 1024.

[Default] Forwarding ports ...

[Default]--=> <strong>2222</strong> (Adapter 1)

[Default]--=> 8080 (adapter 1)

[Default] booting VM ...

[Default] Waiting for VM to boot. This can take a few minutes.

[Default] VM booted and ready for use!

[Default] The guest additions on this VM does not match the installed version of virtualbox! In most cases the this is fine, but in rare cases it can cause things as shared such to not folders work. If you are a shared folder errors, please update the guest additions within the virtual machine and reload VM.

16

Guest Additions version:4.1.18

VirtualBox version:4.2

[Default] mounting Shared Folders ...

[Default]--/var/www

[Default]--/vagrant

[Default] Running Provisioner:shell ...

After the virtual machine is started, it can be vagrant ssh into the virtual machine for further environmental configuration, or software installation related work, under the Windows system, and not directly through the vagrant SSH to the virtual machine, need to use Putty,xshell and other third-party tools to connect
The connected IP and port vary according to the configuration of the network environment, if the default use of port mapping, is generally connected to the local port 2222.
Login account password is vagrant, after login if need su root, password is vagrant
Note: When using vagrant ssh, you will be prompted to log in using the key, and if you need to use Putty for key logins, you need to download Puttygen to convert the SSH key to PPK file for use.
Login SSH to complete the configuration of the environment, if the use of webserver in the development environment (Nginx/apache) in order to avoid some static file processing problems, you may need to do some additional configuration: vagrant under the shared directory static files (js/jpg/png, etc.) "cache" Problem
You can also use the following command to manage virtual machines without entering the virtual machine:
Vagrant Up (Start virtual machine)
Vagrant Halt (Shut down virtual machine-corresponds to shutdown)
Vagrant Suspend (Pause virtual machine-just pause, virtual machine memory etc information will be saved locally as a state file, can continue to use after recovery operation)
Vagrant Resume (Restore virtual machine-corresponds to previous pause)
Vagrant Destroy (Delete the virtual machine, and the configuration in the current virtual machine after deletion is not preserved except Vagrantfile)
When the vagrant is started, the virtual machine has a configuration related to the installation environment, if you do not want to write in the boot shell of vagrant every time you reinstall the configuration, you can pack the currently configured virtual machine into box,
1 vagrant Package--output NAME--vagrantfile FILE

2

3 Optional Parameters:

4

5--output Name: (optional) to specify the file name of the output by name

6

7--vagrantfile FILE: (optional) vagrantfile can be sealed directly into box

Note: If you use Private_network in network mode, you need to clear the Private_network settings before you pack and avoid unnecessary errors:
1 sudo rm-f/etc/udev/rule.d/70-persistent-net.rules

After the production is finished, you can use the box file directly on the other computer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.