2.2 vagrant&VirtualBox for Mac

Vagrant下載

  • Vagrant本質上就是命令行版的virtualbox,它依賴VirtualBox
  • 它使用命令行來執行virtualbox的各類操做,提升開發的效率
  • vagrant執行的成果,也能夠在virtualbox上無縫呈現。
  • 能夠在各類系統上作各類測試,而無需污染物理主機上運行的系統
Vagrant與Docker有不少類似之處,又有很多不一樣。一言蔽之,Vagrant用來管理虛擬機,Docker用來隔離應用環境。 https://www.vagrantup.com/downloads.html
vagrant -v
Vagrant 2.1.1

Centos上安裝php

wget https://releases.hashicorp.com/vagrant/2.1.2/vagrant_2.1.2_x86_64.rpm
yum install -y vagrant_2.1.2_x86_64.rpm

vagrant鏡像

vagrant box add 只會下載鏡像,並不會馬上建立一個虛擬機html

vagrant box list/add/remove

vagrant虛擬機

CLI列表mysql

https://www.vagrantup.com/doc...nginx

1. 建立centos7系統

#會生成一個Vagrantfile文件
vagrant init centos/7
#基於Vagrantfile配置文件,建立啓動centos7
vagrant up
#進入啓動完成的centos7系統
vagrant ssh
若是須要使用vagrant建立其餘系統,能夠訪問 https://app.vagrantup.com/box...,查看全部的系統列表

若是追求穩定版,能夠優先使用 https://app.vagrantup.com/bentosql

2. 查看centos7狀態

vagrant status
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

3. 關機

vagrant halt

4. 重啓

vagrant reload默認狀況下會更新Vagrantfile文件中除了config.vm.provision配置之外的配置,而後重啓系統。
使用--provision選項,會強制更新config.vm.provision配置docker

vagrant reload

5. 修改root爲進入系統的默認用戶

默認狀況下,咱們是使用用戶名爲vagrant、密碼爲vagrant的帳號登錄的。可是在執行命令前不加sudo就經常會致使權限問題。所以能夠經過以下方式,將默認帳號改成root:shell

密碼方式登錄centos

先使用vagrant帳號登錄進系統,而後su -切換成root用戶,root用戶的密碼默認爲vagrantruby

修改/etc/ssh/sshd_config文件,修改成或添加以下配置,而後 systemctl reload sshd 重啓sshd服務app

若是是密碼方式登錄:

PermitRootLogin yes
PasswordAuthentication yes

若是是證書方式登錄:

PermitRootLogin yes
PubkeyAuthentication yes

最後編輯Vagrantfile文件,添加以下配置,vagrant reload後從新vagrant ssh便可

若是是密碼方式登錄:

config.ssh.username = 'root'
config.ssh.password = 'vagrant'

若是是證書方式登錄:

config.ssh.username= "root"
config.ssh.private_key_path="~/.ssh/id_rsa"

private_key_path若是不顯示聲明的話,vagrant就會報錯:

Authentication failure. Retrying...
PHPStorm目前並不支持Vagrant文件的語法高亮。使用IntelliJ打開項目,而後安裝一個ruby插件,就能夠實現語法高亮了

vagrant實戰

  • 打造一個自定義的vagrant
  • 配置好lamp和lnmp環境
  • 可以運行YII二、Laravel、ThinkPHP

1. 初始化啓動虛擬機

vagrant默認的root密碼爲vagrant,爲了便於記憶,咱們將虛擬機中的各個軟件的密碼都設置爲vagrant

2. 軟件安裝

  • nginx
  • mysql
  • php

3. 高級知識

  • 端口轉發,實現宿主機與虛擬機端口的映射,要求宿主機的端口設置必須大於1024

按照以下配置,咱們便可在宿主機上經過8080端口,去訪問虛擬機中80端口的內容

  config.vm.network "forwarded_port", guest: 80, host: 8080
  • 共享目錄,能夠實現代碼和資源文件的同步

4. 打包分發

打包前須要vagrant halt虛擬機,注意打包文件的輸出目錄必定不要放在vagrant數據同步的目錄內,緣由你懂的。。。

vagrant package --output xxx.box
vagrant package --output xxx.box --base 虛擬機名稱

若是咱們給別人了1.0版本的box,而後又分發了一個2.0的box,老用戶想升級的話,此時能夠由發行者在Vagrantfile文件的shell部分編寫升級腳本.

若是咱們經過sudo vagrant up建立了一個vm,可是使用vagrant status來讀取vmzhuang tai

The VirtualBox VM was created with a user that doesn't match the
current user running Vagrant. VirtualBox requires that the same user
be used to manage the VM that was created. Please re-run Vagrant with
that user. This is not a Vagrant issue.
The UID used to create the VM was: 502
Your UID is: 501

別人拿到打包的.box文件,使用以下命令便可使用了

vagrant init siguoya.box
vagrant up

5. 經常使用配置

Vagrant.configure("2") do |config|
  # https://docs.vagrantup.com.
  config.vm.box = "centos/7"
  config.vm.hostname = "centos"
  config.vm.box_check_update = false
  config.disksize.size = "60GB"
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  # 配置private_network的好處:
  # - 不須要配端口轉發,突破端口轉發宿主機端口設置不能小於1024的問題
  # - 可使用nfs進行文件同步,避免出現文件類型不一致的問題
  # 記得不要和宿主機所在的網段衝突,例如個人宿主機爲192.168.88.66,而後將ip設成192.168.88.168的時候就致使虛擬機沒法啓動了
  # 設置auto_config爲true,是爲了打包分發別人的時候,避免私有ip致使的問題,保障可以正常使用
  config.vm.network "private_network", ip: "192.168.33.10", auto_config: true
  #因爲VM的bug,須要將nginx的sendfile設置爲false,否則可能會致使代碼不能當即生效
  #10.15 之前 config.vm.synced_folder "/usr/project/code", "/usr/project/code", :nfs => true
  config.vm.synced_folder "/System/Volumes/Data/usr/local/coding/code", "/usr/local/coding/code", type: "nfs" , nfs_version: 3, nfs_udp: false
  #因爲虛擬機內外文件系統不一致,vagrant默認的文件夾同步會報錯,所以經過以下方式進行禁止
  config.vm.synced_folder ".", "/vagrant", :disabled => true
  config.vm.provider "virtualbox" do |vb|
    vb.name = "vagrant_siguoya"
    vb.memory = "2048"
    vb.cpus = "2"
  end
end

配置了nfs同步以後,vagrant up 會要求輸入密碼,若是不想輸入密碼,能夠 sudo vi /etc/sudoers 添加以下內容:

# VAGRANT
Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports
Cmnd_Alias VAGRANT_NFSD = /sbin/nfsd restart
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /usr/bin/sed -E -e /*/ d -ibak /etc/exports
%admin ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD, VAGRANT_EXPORTS_REMOVE

常見問題

若是/etc/exports的文件內容以下:

# VAGRANT-BEGIN: 501 79c27c9a-9f3f-4ac7-b2b1-fb453217fb92
"/usr/project/code" 192.168.33.10 -alldirs -mapall=501:20
# VAGRANT-END: 501 79c27c9a-9f3f-4ac7-b2b1-fb453217fb92
# VAGRANT-BEGIN: 501 0b26c233-7623-4acd-931a-670b8ecbfcd4
"/usr/project/code/zy108830/docker-demo/vagrant-network/labs" 192.168.205.10 -alldirs -mapall=501:20
# VAGRANT-END: 501 0b26c233-7623-4acd-931a-670b8ecbfcd4

就會報錯:

NFS is reporting that your exports file is invalid. Vagrant does this check before making any changes to the file. Please correct the issues below and execute "vagrant reload":
exports:5: /usr/project/code/zy108830/docker-demo/vagrant-network/labs conflicts with existing export /usr/project/code

If you had some previous images/boxes installed in your VirtualBox installation, so there were a few invalid entries in /etc/exports already. So you had to clean up that file and restart your Vagrant box.

sudo rm /etc/exports && sudo touch /etc/exports
vagrant halt && vagrant up --provision
相關文章
相關標籤/搜索