The story

Presumably everyone’s childhood has seen “Dragon Ball” bar, which is the all-purpose capsule invented by the Buerma family, it can be described as high-tech products ah, a small capsule, what motorcycle, car, bed, house as long as play a finger to change out. Of course, there are a lot of advantages, such high-tech things would be great if they existed in reality, the ideal is good, the reality is in the next few decades or impossible to develop such a capsule, but in the world of programmers, the impossible is often possible. This “all-purpose capsule” is Vagrant.

Basic introduction

Vagrant is a tool for building and managing virtual machine environments. Vagrant has an easy-to-use workflow and a focus on automation that reduces developer setup time and increases productivity. Fixed the “it works on my machine” problem. Vagrant is designed for easy implementation of virtualized environments. Developed in Ruby and based on interfaces to virtual machine management software such as VirtualBox, Vagrant provides a configurable, lightweight and portable virtual development environment. Vagrant makes it easy to set up a virtual environment and simulate multiple virtual machines, allowing us to simulate distributed systems on development machines.

Reasons for choosing

As a PHP back-end developer, the most troublesome and basic thing is to install and deploy the development environment. A server environment needs some service support such as LNMP, Redis and Mongo, and each new employee has to spend half a day or even a day to install the development environment. With Vagrant, you just need to bring your package (development tools, code libraries, configured servers, etc.) to work, which is great for productivity.

The preparatory work

VirtualBox installation

  • Download address: www.virtualbox.org/wiki/Downlo…

The installation of the Vagrant

  • Download address: www.vagrantup.com/downloads.h…

PS: Once installed, you can enter the Vagrant command on the terminal.

Version compatibility

  • VirtualBox: After 4.3.12, you are advised to install 4.3.12.
  • Vagrant: You can upgrade directly from 1.0.x to 1.x. Vagrant is backwards-compatible with Vagrant1.0.x, but 1.1+ does not support the 1.0.x plug-in, so the plug-in needs to be upgraded accordingly.

Using a Vagrant

The box is introduced

Box is a packaged operating system (universal capsule) file with the suffix.box, which is actually a compressed package containing Vagrant configuration information and VirtualBox virtual machine image files. Vagrant Up starts the virtual machine based on a Box file, so you must have the box file ready before starting the virtual machine. Or you can specify the remote box address on startup. In this case, I downloaded the box file and specified it on startup.

The tutorial

1. Create a new directory on your operating system and execute the directory in the current directory: if you have a box package locally (e.g., test.box), if you don’t have a box file address that you can execute remotely (e.g., Vagrant provides many box download addresses).

vagrant box add test.box
Copy the code

Check whether the box is created successfully

vagrant box list
Copy the code

2. Initialize the box above

vagrant init test.box
Copy the code

This initialization produces a Vagrant File, a “universal capsule” specification that contains the configuration of the Vagrant virtual machine. 3. Start Vagrant

 vagrant up
Copy the code

4. Connect to Vagrant

  vagrant ssh
Copy the code

Vagrantfile “Universal Capsule Instruction Manual” introduction

  • Config.vm. box = “text.box”, box file to connect to
  • Config.vm.net work “forwarded_port”, guest: 80, host: 80, port mapping
  • Config.vm.net work” private_network”, IP: “192.168.33.10”, change the access IP address
  • Config.vm. synced_folder “E:/ WWW”, “/home/vagrant/Code/WWW”, shared directory, front-local, the latter VM

Vagrant often commands

  • Vagrant Box add adds box
  • Vagrant Init initializes box
  • Vagrant Up starts the virtual machine
  • Vagrant Logs in to the VM over SSH
  • Vagrant Box List Lists the current box list of Vagrant
  • Vagrant Box remove deletes the corresponding box
  • Vagrant Destroy stops the currently running virtual machine and destroys all created resources
  • The vagrant halt to turn it off
  • The Vagrant package packages the current running virtual machine environment as a box file
  • Vagrant Plugin Installs and uninstalls the plugin
  • Vagrant Reload reboots the virtual machine and reloads the configuration file
  • Vagrant resume resumes the suspended state
  • Vagrant Status Obtains the status of the current VM
  • Vagrant Suspend suspends the current virtual machine
  • Vagrant global-status Views information about all VMS currently managed by Vagrant