Vagrant是一个基于Ruby的工具,用于建立和部署虚拟化开发环境。它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef建立自动化虚拟环境。php
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.html
https://www.vagrantup.com/java
These standard templates are called base boxes.laravel
Create and configure lightweight, reproducible可再生的, and portable development environments.web
官网上写的步骤:shell
SET UPapache
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.segmentfault
CONFIGUREwindows
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.centos
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
你会看到终端显示了启动过程,启动完成后,咱们就能够用 SSH 登陆虚拟机了,剩下的步骤就是在虚拟机里配置你要运行的各类环境和参数了。
$ vagrant ssh
~/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
更多内容请查阅官方文档 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 三种网络配置详解
- 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)'
优势:
缺点:
设置共享目录位置与读写权限
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|
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
http://www.cnblogs.com/hubing/p/5933779.html
https://segmentfault.com/a/1190000000264347#articleHeader3
http://lovelace.blog.51cto.com/1028430/1423343
http://www.iversong.com/2015/04/20/vagrant-dev-env/
Vagrant 知识澄清与杂症诊治
https://segmentfault.com/a/1190000002436885
https://yq.aliyun.com/articles/47306