开发环境介绍
无论何种开发语言,目前用的比较多的开发环境基本就是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 重载虚拟机
原文出处:https://www.cnblogs.com/feixiangmanon/p/10992075.html