vagrant Getting Started

About
Vagrant is a Ruby-based tool for creating and deploying virtualized development environment.
The following installation based on VirtualBox, you need to install VirtualBox before you start.
First, the installation and initialization
1. Download vagrant installer, and run the installation.
2. After restarting the computer, you can run vagrant dos command in the command line.
3. Create the project directory.

$ mkdir vagrant_getting_started
$ cd vagrant_getting_started
$ vagrant init                     //初始化目录,生成vagrantfile配置文件

Second, add Box
1. Box download site:
https://vagrantcloud.com/boxes/search
have already done a lot of virtual machine environment on the site, you can download onto a local advance. Of course, you can not advance to download, use the following command to directly download online.
2. Download the command box
$ vagrant box add hashicorp/precise64 //从HashiCorp's Vagrant Cloud
box catalog download named box "hashicorp / precise64" and stored in the "user directory /.vagrant.d/boxes."
3. namespace box
above example the user name is "hashicorp", Box is "precise64". You can also use URL or local path.
III. Use box
1. Modify the configuration file using the download box

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
end

2. You can also add the version number of the box

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.box_version = "1.1.0"
end

3. You can also add a description URL box

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"
end

Four, and starts connection box
1. Start environment vagrant

$ vagrant up

2. Use ssh connection

$ vagrant ssh

3. Disconnect ssh connection

vagrant@precise64:~$ logout
Connection to 127.0.0.1 closed.

Or use the keyboard ctrl + D
Five, directory synchronization
1. synchronize directories
local project is the root of the virtual environment / vagrant directory.

Sixth, automatic configuration
1. automatic configuration script, install apache. In the vagrant New bootstrap.sh file directory

#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -fs /vagrant /var/www
fi

2. Add the configuration file

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.provision :shell, path: "bootstrap.sh"
end

3. Note: After adding bootstrap.sh script, the script automatically restart the virtual machine does not take effect, after using the destroy command, and then re-use vagrant up, bootstrap script into effect.
4. Verify successful
add index.html in the project root directory, and verify that ssh:

$ vagrant ssh
...
vagrant@precise64:~$ wget -qO- 127.0.0.1

Seven Network Configuration
Network Configuration There are three ways: port forwarding, it seems there is a network, a bridged network.
1. Using port forwarding

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, guest: 80, host: 4567
end

2. Use the internal network (private network)

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network "private_network", ip: "192.168.33.10" #注意不要产生IP冲突
end

Eight share (External Access)
1. shared virtual environment need to download ngrok: https://ngrok.com/download and placed under the PATH environment variable, I've put in the bin directory of vagrant.
2. other users access to the virtual environment services

$ vagrant share
...
==> default: Creating Vagrant Share session...
==> default: HTTP URL: http://b1fb1f3f.ngrok.io
...

Nine, multi-application support virtualization
vagrant support multiple virtualized environment, VirtualBox, VMware, AWS. You can use preferences virtualization software:

$ vagrant up --provider=vmware_fusion

Ten, packaged distribution
Once you have configured development environment, exit and close the virtual machine. To package development environment in a terminal:

$ vagrant package

Package.box will generate a directory in the current package after the completion of the file, the file will be passed to other users, add other users as long as this box and use it to initialize its development directory will be able to get an identical development environment.
Add Method:
Suppose we got box storage path is D: /box/package.box, enter in a terminal:

$ vagrant box add test D:/box/package.box  # 添加 package.box 镜像并命名为 test
$ cd D:/vagrant/dev  # 切换到项目目录
$ vagrant init test  # 用 test 镜像初始化。
发布了15 篇原创文章 · 获赞 2 · 访问量 7930

Guess you like

Origin blog.csdn.net/c1h2cy/article/details/78963841