golang開發:(一)開發環境搭建vagrant+VirtualBox

開發環境介紹

無論何種開發語言,目前用的比較多的開發環境基本就是Vagrant+VirtualBox搭建的虛擬開發環境,這種開發環境的好處就是一次搭建到處可用,各個平臺和系統均可以使用。開發團隊中,能夠本身製做一個box,讓團隊的成員方便安裝,保證每一個人的開發環境都是一致的。html

Vagrant能夠建立一些共享目錄,讓物理機和虛擬機使用共享的目錄,虛擬機只提供開發環境。這樣的話,開發環境隨處可用。代碼目錄只要在物理機上共享就可使用這套開發環境。shell

Vagrant 安裝

官網下載合適的安裝包傻瓜式安裝。官網下載地址:http://www.vagrantup.com/downloads.html
安裝完成後試下 命令apache

vagrant -h
Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     cloud           manages everything related to Vagrant Cloud
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile

就表示安裝成功了。ubuntu

VirtualBox 安裝

跟Vagrant相似傻瓜式安裝。官網下載地址:https://www.virtualbox.org/wiki/Downloads/
下載合適的平臺版本安裝
VirtualBox 軟件只要能夠打開就表示安裝成功了vim

各類box的下載

先進入官網box的列表 https://app.vagrantup.com/boxes/search
查找本身須要虛擬機 系統 和版本,咱們下載的基本都是VirtualBox版本的,能夠點擊菜單的VirtualBox標籤
在這裏插入圖片描述
咱們下載的是 Ubuntu 16.04 LTS
https://app.vagrantup.com/ubuntu/boxes/xenial64
找到它最近的版本,點擊連接 https://app.vagrantup.com/ubuntu/boxes/xenial64/versions/20190521.0.0
URL + /providers/ + 虛擬機就是須要下載的box網絡

咱們下載的包就是下面的連接
https://app.vagrantup.com/ubuntu/boxes/xenial64/versions/20190521.0.0/providers/virtualbox.boxapp

而後迅雷下載上面的連接ssh

添加box

執行 vagrant box add 名稱 box地址
名稱--box名稱,任意取名,默認 base
box地址--已經下載好的box地址或者遠端的box地址ide

個人機器執行的是this

vagrant box add base virtualboxubuntu.box

添加完成後執行
sudo vagrant init
個人執行

sudo vagrant init
    A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

啓動 vagrant

sudo vagrant up
/opt/vagrant/embedded/gems/2.2.2/gems/vagrant-2.2.2/lib/vagrant/util/which.rb:37: warning: Insecure world writable dir /data/code/go/bin in PATH, mode 040777
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'base'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntubox_default_1558766226465_14530
==> default: Fixed port collision for 22 => 2222. Now on port 2200.

鏈接登陸虛擬機

sudo vagrant ssh
/opt/vagrant/embedded/gems/2.2.2/gems/vagrant-2.2.2/lib/vagrant/util/which.rb:37: warning: Insecure world writable dir /data/code/go/bin in PATH, mode 040777
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-148-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 packages can be updated.
0 updates are security updates.

能夠看到是Ubuntu的系統

vagrantup 經常使用的開發配置

打開box的配置。vim Vagrantfile

config.vm.box = "base" --box 名字
config.vm.synced_folder "../data", "/vagrant_data"
--把本機的../data 掛載到虛擬機的 /vagrant_data
ned port
config.vm.network "forwarded_port", guest: 80, host: 8080
--把本機的8080端口請求轉發到虛擬機的 80端口
config.vm.network "private_network", ip: "192.168.33.10"
--網絡設置,主機虛擬機網絡互訪,主機經過192.168.33.10 能夠訪問虛擬機
config.ssh.username = "vagrant"
--登陸用戶名 
config.ssh.password = "vagrant"
--登陸密碼
config.ssh.insert_key = "true"
--帳戶密碼鍵值存儲(一直沒明白是啥意思)
config.ssh.private_key_path = "/Users/XXX/.ssh/id_rsa"
--密鑰登陸的時候,密鑰地址。
onfig.vm.provision "shell", inline: <<-SHELL
 apt-get update
 apt-get install -y apache2
 SHELL
 --虛擬機啓動的時候須要執行的腳本

瞭解一些簡單vagrantup,日常開發就夠用了。

vagrant box add/remove 添加移除 box
vagrant halt 中止虛擬機
vagrant init 初始化虛擬機
vagrant up 啓動虛擬機
vagrant reload 重載虛擬機
相關文章
相關標籤/搜索