使用Vagrant搭建屬于自己的box開發環境

Vagrant 是一個基于Ruby的工具,用于創建和部署虛擬化開發環境。它使用Oracle的開源VirtualBox虛擬化系統,使用 Chef創建自動化虛擬環境。認識 Vagrant 是從 Laravel 開始的,Laravel 官方提供了一整套本地開發環境 Homestead 。剛開始的時候對它不屑一顧,總覺得很麻煩,還不如phpStudy,Xampp,AppServ等集成開發環境來得快,后來由于本地開發環境和線上正式環境不一樣,項目上線時出現了一些小問題,所以下定決心使用 vagrant 打造一個和生產環境一模一樣的開發環境(注意:僅軟件版本一致,硬件配置會有差異)。

下載并安裝Vagrant

下載地址:https://www.vagrantup.com/downloads.html
支持跨平臺(Mac OX,Linux,Windows),詳細安裝步驟請參見官方文檔。
安裝完成后可以在命令行中查看版本信息,windows默認的cmd不好用建議使用xshellgit-bash等工具來代替

admin@DESKTOP-68FVB12 MINGW64 /
$ vagrant.exe -v
Vagrant 2.0.0

注意:vagrant 命令的路徑一定要在$PATH環境變量中

下載VirtualBox

下載地址:https://www.virtualbox.org/wiki/Downloads
支持跨平臺(Mac OX,Linux,Windows),如果你習慣使用VMware,vagrant也是支持的,但VMware是收費軟件,在國內使用我相信你懂的。

初始化開發環境

官方鏡像地址:https://atlas.hashicorp.com/boxes/search

  • 如果你沒有本地的box
admin@DESKTOP-68FVB12 MINGW64 /
$ mkdir dev && cd dev
admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant init centos/7        ##下載box需要等待一段時間,根據網速來定

注意:我的開發環境是centos的系統,所以我選擇了centos/7的box,如果你想使用其它開發環境,你也可以使用其它的box來初始化。

  • 如果你有本地的box文件,首先將box添加到box list中
admin@DESKTOP-68FVB12 MINGW64 /
$ vagrant.exe box add lnmp /e/boxs/lnmp.box

注意:lnmp是自己命名的box名稱,/e/boxs/lnmp.box等于windows下e:\boxs\lnmp.box,即box文件的路徑

admin@DESKTOP-68FVB12 MINGW64 /
$ vagrant.exe box list
lnmp (virtualbox, 0)

使用剛剛添加的lnmpbox初始化開發環境

admin@DESKTOP-68FVB12 MINGW64 /
$ mkdir dev && cd dev
admin@DESKTOP-68FVB12 MINGW64 /dev/
$ vagrant init lnmp        ##無需下載,直接從本地box list讀取

啟動Vagrant

vagrant init完成后會在當前目錄生成一個Vagrantfile文件,該文件為vagrant啟動虛擬機的配置文件。vagrant up會按Vagrantfile的配置項來啟動虛擬機。

admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant.exe up

登錄Vagrant

默認使用的是vagrant用戶登錄,通過修改Vagrantfile也可指定其它用戶登錄

admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant.exe ssh
Last login: Wed Nov 15 01:21:07 2017 from 10.0.2.2
[vagrant@localhost ~]$

配置LNMP開發環境

關于lnmp的開發環境安裝,我也寫了相關的教程,詳情請查看LNMP專題

Vagrantfile配置

配置Vagrantfile主要是為了宿主機和虛擬機之前通信,文件共享等功能,這樣在本地寫好代碼后才能在虛擬機中運行。


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://vagrantcloud.com/search.
  config.vm.box = "lnmp"

  # 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.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # 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"
  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"

  # mac
  # config.vm.synced_folder "Code", "/usr/local/nginx/html/", type:"nfs"

  # windows
  config.vm.synced_folder "Code", "/usr/local/nginx/html/", :nfs => (RUBY_PLATFORM =~ /linux/ or RUBY_PLATFORM =~ /darwin/)

  # 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"
        vb.name = "lnmp_centos7"
        vb.memory = "2048"
        vb.cpus = "2"
  end

  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # 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
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

注意:config.vm.network,config.vm.synced_folder,vb.name,vb.memory,vb.cpus,以上配置可參考示例文件根據自身情況進行設置。修改配置文件后請使用vagrant reload重新載入配置文件。

安裝插件

要想正常讓宿主機和虛擬機之間共享文件,光是配置Vagrantfile是不行,還需要安裝相應的插件。

admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant plugin install vagrant-vbguest
$ vagrant plugin install childprocess
$ vagrant plugin install vagrant-winnfsd

查看已安裝的插件

admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant.exe plugin list
childprocess (0.6.3)
  - Version Constraint: 0.6.3
vagrant-share (1.1.9, system)
vagrant-vbguest (0.15.0)
vagrant-winnfsd (1.1.0)

優化 Nginx 配置,將sendfile的值設為off,解決靜態資源刷新延遲的問題

admin@DESKTOP-68FVB12 MINGW64 /dev
$ vim /usr/local/nginx/conf/nginx.conf
...
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        off;
    #tcp_nopush     on;
...

將配置好的 lnmp 開發環境打包

admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant.exe package --output lnmp.box --vagrantfile Vagrantfile

注意:打包前請將config.vm.network,config.vm.synced_folder配置項先注釋,vagrant package可以不帶任何參數,--output是指定box的名稱,--vagrantfile是指定Vagrantfile的路徑,這樣便可生成一個你個人獨有的box,無論在任何平臺,只要具備了vagrant的運行環境,你便可快速啟動你的開發環境。

常用vagrant命令

更多命令請查看,https://www.vagrantup.com/docs/

vagrant init  # 初始化
vagrant up  # 啟動虛擬機
vagrant halt  # 關閉虛擬機
vagrant suspend  # 暫停休眠虛擬機
vagrant resume  # 喚醒虛擬機
vagrant reload  # 重啟虛擬機
vagrant ssh  # SSH 登錄虛擬機
vagrant status  # 查看虛擬機運行狀態
vagrant destroy  # 銷毀當前虛擬機
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容