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