Vagrant 安装使用以及个人理解

Ubuntu 环境:

  1. 下载安装 VritualBox
  2. 安装最新的 vagrant https://www.vagrantup.com/docs/installation/
  3. up and running
   $ vagrant init hashicorp/precise64 box-address
   $ vagrant up
  1. vagrant ssh (链接到vagrant 的虚拟机中)
  2. 退出命令 exit
  3. 同步文件夹:默认下,vagrant 共享的是你的项目目录(就是有vagrantfile文件的)
  4. Provision 操作:在vagrant中,我们可以通过SSH使用apt 命令安装软件<br />也可以在执行vagrant up 命令的时候自动安装。
    步骤:
    1.新建bootstrap.sh 文件
#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
     rm -rf /var/www
     ln -fs /vagrant /var/www
fi

2.在Vagrantfile 中添加配置

Vagrant.configure("2") do |config|
      config.vm.box = "hashicorp/precise64"
      config.vm.provision :shell, path: "bootstrap.sh"
end

3.执行

若vagrant没有运行,执行命令:vagrant up
若vagrant已经运行,执行命令:vagrant reload --provision
  1. NETWORK 网络
    1.Port Forwarding :端口转发允许你通过宿主机上的端口转发虚拟主机上的特殊端口。
    配置Vagrantfile:
Vagrant.configure("2") do |config|
     config.vm.box = "hashicorp/precise64"
     config.vm.provision :shell, path: "bootstrap.sh"
     config.vm.network :forwarded_port, guest: 80, host: 4567
end

2.运行:

运行vagrant reload 或者是 vagrant up 
在浏览器上:http://127.0.0.1:4567
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容