Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境。

​Vagrant​​ is an amazing tool for managing virtual machines via a simple to use command line interface. With a simple ​​vagrant up​​ you can be working in a clean environment based on a standard template.

https://www.vagrantup.com/

These standard templates are called ​​base boxes​​.

Create and configure lightweight, reproducible可再生的, and portable development environments.

官网上写的步骤:

SET UP

Download and install Vagrant within minutes on Mac OS X, Windows, or a popular distribution of Linux. No complicated setup process, just a simple to use OS-standard installer.

CONFIGURE

Create a single file for your project to describe the type of machine you want, the software that needs to be installed, and the way you want to access the machine. Store this file with your project code.

WORK

Run a single command — "vagrant up" — and sit back as Vagrant puts together your complete development environment. Say goodbye to the "works on my machine" excuse as Vagrant creates identical development environments for everyone on your team.

 

1. 安装 VirtualBox

虚拟机还是得依靠 VirtualBox 来搭建,免费小巧。

下载地址:​​https://www.virtualbox.org/wiki/Downloads​

* 虽然 Vagrant 也支持 VMware,不过 VMware 是收费的,对应的 Vagrant 版本也是收费的

2. 安装 Vagrant

下载地址:​​http://downloads.vagrantup.com/​​ 根据提示一步步安装。

此外,还得下载官方封装好的基础镜像:

Ubuntu precise 32 VirtualBox ​​http://files.vagrantup.com/precise32.box​

Ubuntu precise 64 VirtualBox ​​http://files.vagrantup.com/precise64.box​

如果你要其他系统的镜像,可以来这里下载:​​http://www.vagrantbox.es/​

* 鉴于国内网速,我们将下载镜像的步骤单独剥离出来了

3. 添加镜像到 Vagrant

假设我们下载的镜像存放路径是 ​​~/box/precise64.box​​,在终端里输入:

$ vagrant box add hahaha ~/box/precise64.box


​hahaha​​ 是我们给这个 box 命的名字,​​~/box/precise64.box​​ 是 box 所在路径

(laravel的homestead

当 VirtualBox / VMware 和 Vagrant 安装完成后,你可以在终端机以下列命令将 'laravel/homestead' 封装包安装进你的 Vagrant 安装程序中。下载封装包会花你一点时间,时间长短将依据你的网络速度决定:

vagrant box add laravel/homestead


如果这个命令失败了, 你可能安装的是一个老版本的 Vagrant 需要指定一个完整的 URL:

vagrant box add laravel/homestead https://atlas.hashicorp.com/laravel/boxes/homestead


命令行下下载速度太慢的话可以利用工具下载以下链接加速.

​https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.2.7/providers/virtualbox.box​

留意链接中的​​0.2.7​​,这是homestead box的版本号,修改url中版本号可以下载不同版本的homestead box.

还有一种方法是,先​​vagrant init minimal/centos6​​,然后直接启动​​vagrant up --provider virtualbox​​。当然这些都与下载boxes到本地效果是一样的,下载方法就是在vagrantcloud.com上点开你所需要的box版本,然后再URL里加入​​/providers/virtualbox.box​​便得到文件地址,如 ​​https://atlas.hashicorp.com/hashicorp/boxes/precise64​​ 对应的文件为https://atlas.hashicorp.com/hashicorp/boxes/precise64/providers/virtualbox.box?spm=5176.100239.blogcont47306.14.i9YtVn&file=virtualbox.box ,

 )

 

下载后执行以下命令, 注意修改 ​​/path/to/virtualbox.box​​ 为正确的 path.

vagrant box add laravel/homestead /path/to/virtualbox.box 


首先在本地创建好工作目录,并在命令行下切换到对应目录

vagrant box add base CentOS-6.3-x86_64-minimal.box

base 表示指定默认的box,也可以为box指定名称,比如 centos63 ,使用base时,之后可以直接使用 vagrant init 进行初始化,如果自行指定名称,则初始化的时候需要指定box的名称。

CentOS-6.3-x86_64-minimal.box 是box对应的文件名,这里可以是本地保存box的路径,也可以是可以下载box的网址,如果是网址的话,Vagrant会自动启动下载。

[vagrant] Downloading with Vagrant::Downloaders::File...

[vagrant] Copying box to temporary location...

[vagrant] Extracting box...

[vagrant] Verifying box...

[vagrant] Cleaning up downloaded box...

设置好box之后,在当前工作目录运行

vagrant init

生成对应的Vagrantfile

 


4. 初始化开发环境

创建一个开发目录(比如:​​~/dev​​),你也可以使用已有的目录,切换到开发目录里,用 ​​hahaha​​ 镜像初始化当前目录的环境:

$ cd ~/dev  # 切换目录
$ vagrant init hahaha # 初始化
$ vagrant up # 启动环境


你会看到终端显示了启动过程,启动完成后,我们就可以用 SSH 登录虚拟机了,剩下的步骤就是在虚拟机里配置你要运行的各种环境和参数了。

$ vagrant ssh  # SSH 登录
$ cd /vagrant # 切换到开发目录,也就是宿主机上的 `~/dev`


​~/dev​​ 目录对应虚拟机中的目录是 ​​/vagrant​

Windows 用户注意:Windows 终端并不支持 ssh,所以需要安装第三方 SSH 客户端,比如:Putty、Cygwin 等。

5. 其他设置

Vagrant 初始化成功后,会在初始化的目录里生成一个 ​​Vagrantfile​​ 的配置文件,可以修改配置文件进行个性化的定制

 默认配置文件:



# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "base"

# 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 "localhost: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 bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"

# 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|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.

# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end

# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end


 

我们要查找错误,将与vb.gui有关的注释去掉.

 

  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


 

Vagrant 默认是使用端口映射方式将虚拟机的端口映射本地从而实现类似 ​​http://localhost:80​​ 这种访问方式,这种方式比较麻烦,新开和修改端口的时候都得编辑。相比较而言,host-only 模式显得方便多了。打开 ​​Vagrantfile​​,将下面这行的注释去掉(移除 ​​#​​)并保存:

config.vm.network :private_network, ip: "192.168.33.10"


重启虚拟机,这样我们就能用 ​​192.168.33.10​​ 访问这台机器了,你可以把 IP 改成其他地址,只要不产生冲突就行。

6. 打包分发

当你配置好开发环境后,退出并关闭虚拟机。在终端里对开发环境进行打包:

$ vagrant package


打包完成后会在当前目录生成一个 ​​package.box​​ 的文件,将这个文件传给其他用户,其他用户只要添加这个 box 并用其初始化自己的开发目录就能得到一个一模一样的开发环境了。

7. 常用命令

$ vagrant init  # 初始化
$ vagrant up # 启动虚拟机
$ vagrant halt # 关闭虚拟机
$ vagrant reload # 重启虚拟机
$ vagrant ssh # SSH 至虚拟机
$ vagrant status # 查看虚拟机运行状态
$ vagrant destroy # 销毁当前虚拟机


更多内容请查阅官方文档 ​​http://docs.vagrantup.com/v2/cli/index.html​

 

 我

vagrant up报错:

The guest machine entered an invalid state while waiting for it

to boot. Valid states are 'starting, running'. The machine is in the

'poweroff' state. Please verify everything is configured

properly and try again.

从提示看不出错在哪里,要去virtualbox目录下看错误日志。日志在

'C:\Users\Administrator\VirtualBoxVMs\下面。

我打开日志看到错误:

00:00:00.747736 ERROR [COM]: aRC=E_FAIL (0x80004005) aIID={872da645-4a9b-1727-bee2-5585105b9eed} aComponent={ConsoleWrap} aText={VT-x is disabled in the BIOS for all CPU modes (VERR_VMX_MSR_ALL_VMX_DISABLED)}, preserve=false aResultDetail=0

00:00:00.747940 Console: Machine state changed to 'PoweredOff'

00:00:00.753476 Power up failed (vrc=VERR_VMX_MSR_ALL_VMX_DISABLED, rc=E_FAIL (0X80004005))

看样子是vt-x没有开启。

  • 运行vagrant up 启动 virtualbox 后,GUI会给出提示
    VT-x/AMD-V硬件加速在您的系统中不可用。您的64-位虚拟机将无法检测到 64-位处理器,从而无法启动。
  • 这是由于在BOIS中没有开启cpu虚拟化支持,重启F2或F10等进入BIOS设置Virtualization为Enable(我的Thinkpad是Security=>Virtualizatio设置为Enable);
  • 电脑重启后,再次vagrant up启动虚拟机还是有一些问题,当时也没有记录下来错误信息,只记得解决方案是使用vagrant destroy将虚拟机从磁盘中删除,然后使用vagrant up命令重新创建。

 

常用配置:

Vagrant 三种网络配置详解


 



  • Forwarded port
  • Private network
  • Public network

Vagrant 中一共有三种网络配置,下面我们将会详解三种网络配置各自优缺点。

端口映射(Forwarded port),顾名思义是指把宿主计算机的端口映射到虚拟机的某一个端口上,访问宿主计算机端口时,请求实际是被转发到虚拟机上指定端口的。Vagrantfile中设定语法为:




config.vm.forwarded_port 80, 8080


以上将访问宿主计算机8080端口的请求都转发到虚拟机的80端口上进行处理。

默认只转发TCP包,UDP需要额外添加以下语句:




config.vm.forwarded_port 80, 8080, protocol: "udp"


优点:

  • 简单易理解
  • 容易实现外网访问虚拟机

缺点:

  • 如果一两个端口需要映射很容易,但是如果有有很多端口,比如MySQL,MongoDB,tomcat等服务,端口比较多时,就比较麻烦。
  • 不支持在宿主机器上使用小于1024的端口来转发。比如:不能使用SSL的443端口来进行https连接。


私有网络(Private network),只有主机可以访问虚拟机,如果多个虚拟机设定在同一个网段也可以互相访问,当然虚拟机是可以访问外部网络的。设定语法为:

config.vm.network "private_network", ip: "192.168.50.4"



优点:

  • 安全,只有自己能访问

缺点:

  • 因为私有的原因,所以团队成员其他人不能和你写作

公有网络(Public network),虚拟机享受实体机器一样的待遇,一样的网络配置,vagrant1.3版本之后也可以设定静态IP。设定语法如下:

config.vm.network "public_network", ip: "192.168.1.120"


公有网络中还可以设置桥接的网卡,语法如下

config.vm.network "public_network", :bridge => 'en1: Wi-Fi (AirPort)'



优点:

  • 方便团队协作,别人可以访问你的虚拟机

缺点:

  • 需要有网络,有路由器分配IP

 



 

设置共享目录位置与读写权限

Virtualbox共享目录需要VM上装有Guest Additions。自己安装到VM再打包成box文件做基础模板,或者一开始就找好合适的box文件(前文提到过),随便你怎么整好。

vagrant 默认将宿主机上Vagrantfile所载目录共享至VM上的 /vagrant 目录。

我们常需要的是指定共享某个目录,为此vagrant 支持两种方式:

(

默认的,vagrant将共享你的工作目录(即Vagrantfile所在的目录)到虚拟机中的/vagrant,所以一般不需配置即可,如你需要可配置:

版本"2"

 




1


2


3


4




​Vagrant.configure(​​​​"2"​​​​) ​​​​do​​ ​​|config|​


​# other config here​


​config.vm.synced_folder ​​​​"src/"​​​​, ​​​​"/srv/website"​


​end​



 

  "src/":物理机目录;"/srv/website"虚拟机目录

 

)

简单:vboxsf

vboxsf是 V irtual boxS hared F older(或 VboxS hared F older)的缩写。很多人知道可以指定vboxsf作类型来mount,却不知这个这个词全称,甚至误解为“虚拟机文件系统”而错写成vboxfs。

vagrant默认使用这种方式共享目录。优点方便,缺点是性能不好。配置时,只要在Vagrantfile里指明:

config.vm.synced_folder "path/on/host", "/absolute/path/on/vm"


  • 前一个参数须是宿主机上已存在的目录,若为相对目录,那是相对Vagrantfile所在目录。
  • 后一个参数须是VM上的绝对路径,若不存在,vagrant会在启动VM时建好,多层的目录也没关系。

正经:NFS

NFS是正途,不过它也挑剔。

vagrant 内置了宿主机和VM两端对NFS的支持与配置的协调。如果你用windows,那vagrant当作你的配置不存在,直接忽略(我也觉得你在瞎胡闹)。Vagrantfile里很容易,标上标记就行了:

config.vm.synced_folder "path/on/host", "/absolute/path/on/vm", :nfs => true


  • 两个目录可以一样,如都用 “/shared” ,藉此建立多个机器节点的共享存储了。

NFS的共享目录这种方式的挑剔之处在于目录内的读写权限。

 

 

Linux安装:

 apt-get install virtualbox

 apt-get install vagrant

 

 

 

 


laravel homestead使用:

https://phphub.org/topics/744