Build a cross-platform development environment with Vagrant

Source: Internet
Author: User

We need a better way to manage virtual machines. For example, you might want to create a virtual machine for several Linux systems, because the project you are developing will ultimately run on a server on a Linux system. So, you're planning to build a development environment for your project locally, using a Linux system, and it's too cumbersome to manually install different types of Linux operating systems. You need to Vagrant.

Vagrant is a tool for building a virtual development environment that is ideal for developing Web applications in languages such as Php/python/ruby/java, which is a history of "code running on my machine without problems".

We can package a Linux development environment through Vagrant and distribute it to team members. Members can develop programs on their favorite desktop system (Mac/windows/linux), but the code can be unified in a packaged environment to run, very domineering

Vagrant can let us use the command to download the virtual machine image directly from the cloud, you want any system is OK, centos,ubunto ... Only a single line of command, and then use the command to operate the virtual machine, start, pause, shut down, restart, you can more easily manage some of the local computer on the virtual machine image, manually to configure them, you can re-packaged into a new image, so that you and your team on other projects to reuse.

Installation steps

1. Installing VirtualBox

Virtual machines still have to rely on VirtualBox to build, free small. (Recommended installation 4.2.16 other seems to have some bugs)
:https://www.virtualbox.org/wiki/Downloads

* Although Vagrant also supports VMware, VMware is charged, and the corresponding Vagrant version is also charged

2. Installing Vagrant

: http://downloads.vagrantup.com/Follow the prompts step by step installation.

In addition, you have to download the official encapsulated base image:
Ubuntu Precise VirtualBox Http://files.vagrantup.com/precise32.box
Ubuntu Precise VirtualBox Http://files.vagrantup.com/precise64.box

If you want to mirror other systems, you can download them here: http://www.vagrantbox.es/

* In view of the domestic speed, we separate the steps to download the image separately.

Box

box, you can think of it as a box filled with something. When using Vagrant to create a virtual machine, you need to use Box, it will wrap the operating system image, the different Box with the operating system may not be the same, such as Centos,ubuntu, etc., you can based on them to create their own version of the Box, such as the virtual machine to install some software , then repackage it into Box.

The cloud service provided by Vagrant (https://vagrantcloud.com/) can be downloaded to someone else's good Box, the cloud service is now called Atlas, is a product of Hashicorp company, Vagrant is also the project of this company.    Open this address, you can search for the Box you want to use, you can search the name of the operating system, you can also search for specific software names, such as Laravel,drupal ... The number of times the box is downloaded will be shown on the search results, which you can use to determine if the box is popular.

We need a clean Box, which contains only the operating system, and no other superfluous stuff is installed on the system. Note that there are some differences between Linux operations in different distributions, and I recommend using the CentOS operating system first.

Vagrant Add Box

You have to download box to the local computer, to Vagrant to manage, so when the virtual machine is created, Vagrant will copy the box you specified into your project, so that your operation on this virtual machine will not affect other projects. Determine the number of operating systems on the computer you are using, which is now generally a 64-bit operating system. After opening the command line tool, execute the Vagrant Box add command to add the box.

Vagrant Box Add Name

If the Box you specified does not exist on your local computer, Vagrant will go to its cloud to download it for you. Perform:

Vagrant Box Add chef/centos-6.5

If it is a 32-bit operating system, perform the following:

Vagrant Box Add chef/centos-6.5-i386

The top chef/centos-6.5 and centos-6.5-i386 is the name of the box you want to add, the first chef, a namespace, usually the name of the box's author, and the centos-6.5 that follows the box's operating system Is CentOS, the version is 6.5, i386 represents a 32-bit system.

Vagrant did not find the box you want to add, it will go to the cloud to download for you, it will prompt you to choose the box version, useful on the Virtualbox box, also useful on the VMware box, choose which depends on your system installed on the virtual machine software is what, if You follow my steps, the installation should be Virtualbox, so, select to select 1, then enter, continue to perform the naming

View the Box list on-premises

When you're done, you can check the Box list on the local.

Vagrant Box List
How to manually download and add Box

In the execution of Vagrant box add box, Vagrant will show box on the command line, download speed and progress, if you are in the country, it is likely that the download speed is very slow. You can copy Box, and then use the download tool, such as with thunder to download. In the Powershell above the copy, you can first select what you want to copy, and then press ENTER, then open the Thunder, paste the downloaded address to download Box.

After completion, find the downloaded files, the file name will contain the. box extension, if not, you need to manually add this extension. and rename the file, for example, named Centos_6.5.box. Then put it on the table and go anywhere else. Open the command line and go to the location of the Box file under manual. Like what

CD ~/desktop

Then take a look at the file list:

Ls

Make sure the box file is below the current directory, and then execute the Vagrant Box add command to add the box manually.

Vagrant Box Add name path

Name is the name you want to give this box, we will use this name to manage the box, the path is the location of the box file, if you want to add the box directly below your current directory, you can directly use the box file name, like this:

Vagrant Box Add centos-6.5 centos-6.5.box

Vagrant will copy the box file to a specific directory on the system, and after completion, you can delete the box file on your desktop.

RM Centos-6.5.box

Take a look at the available Box list:

Vagrant Box List

In the returned list, you should be able to find your own manually added centos-6.5 this Box.

Manage Box

The Box that is added to the local computer is placed under a specific directory. Commands related to box can be viewed using the Vagrant Box command. The method used is to vagrant box followed by the corresponding sub-command, followed by the corresponding parameters. The usual is Add, list and remove. Add is adding a new box, list can see the existing box, remove is to remove the box from the system. Note that when you create a virtual machine, Vagrant will copy the Box you specified to the bottom of the specific directory. Therefore, when you use the Remove command to remove a box that is added to the system, it does not affect the existing virtual machines.

Create a virtual machine

After adding box, we can use Vagrant based on this box to create a virtual machine. Find a place to create a directory, which is the directory of your project, it will automatically synchronize with a directory on the virtual machine, that is, the directory on your computer files, you can also be found in a directory in the virtual machine. For example, I want to create a directory on the desktop called Ninghao-project, and then go to this directory, under the command-line tool to execute:

CD ~/desktopmkdir Ninghao-projectcd Ninghao-project
Initialization

The first thing to do is initialize, use Vagrant init, and specify the name of the Box you want to use later. When you are finished, create a file called Vagrantfile under your directory.

Check the available Box first

Vagrant Box List

If you follow my steps, you should be able to see a Box called centos-6.5. Let's use this Box to initialize the project.

Vagrant Init centos-6.5

When you're done, look at what's under the current directory.

Ls

You will see a vagrantfile file that is used to configure the current project's virtual machine, and we will come back to see how to use this file later.

Start a virtual machine

Vagrant provides commands to manipulate virtual machines, such as starting, pausing, shutting down, or destroying virtual machines. To start the virtual machine, execute:

Vagrant up

The first time you start, take a little longer, because Vagrant will copy the Box you specified for the project to a place and then start it. When you're done, you can check the status of the virtual machine with the command:

Vagrant Status

If the return is running, the virtual machine starts successfully, and now you can use SSH to connect to the virtual machine to control it.

Controlling virtual machines

To operate on a virtual machine on a Linux system, you connect to the virtual machine with SSH, which is the same as the server you are remotely controlling Linux on. If you are a MAC user, you can go to the directory where the project resides, and then connect to the virtual machine directly with vagrant ssh. Windows users want to use putty to connect.

Mac
Vagrant SSH
Windows

After starting the virtual machine with vagrant up, you can then enter Vagrant SSH, you will be prompted to say that you did not find SSH, you can choose your favorite SSH client, and then use the following information to connect.

Host:127.0.0.1port:2200username:vagrant

After opening the Putty tool, select SSH for the type of connection, then enter the IP address of the host, 127.0.0.1, port port number 2200, click Connect. This will open a command-line tool, prompting you to enter the user name, which is the user name of the Linux system you want to connect to, Vagrant will create a virtual machine called Vagrant user, the user's password is Vagrant, enter the user name, and then enter the password, You can successfully connect to a virtual machine on a Linux system.

After the virtual machine is connected with Putty, the operation of the virtual machine is done on this gadget.

Using this method, you can also control the server of the Linux system on the Internet, when the putty is opened, the connected host enters the IP address of your server, then the user name of your server and the password.

To this vagrant add box to complete the work, ready to build lamp or LNMP environment

Deploy the lamp environment to package it.

6. Packaging and distribution

When you have configured your development environment, exit and shut down the virtual machine. To package the development environment in the terminal:

$ vagrant Package

After the package is finished, a file is generated in the current directory package.box , which is passed to other users, and other users can get an identical development environment simply by adding the box and initializing their own development directory.

7. Common commands
$ vagrant Init # Initialize $ vagrant Up # start virtual machine $ vagrant Halt # Shut down virtual machine $ vagrant Reload # Restart virtual machine $ vagrant SSH # SSH to virtual machine $ vagra NT Status # View virtual machine run status $ vagrant Destroy # destroys the current virtual machine


Build a cross-platform development environment with Vagrant

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.