如何安装并设置 Vagrant
Vagrant 对于虚拟机来说是一个强大的工具,在这里我们将研究如何在 Ubuntu 上设置和使用 Virtualbox 和 Vagrant 来提供可复制的虚拟机。
虚拟机,并不复杂
多年来,开发人员一直使用虚拟机作为其工作流程的一部分,允许他们交换和更改运行软件的环境,这通常是为了防止项目之间的冲突,例如需要 php 5.3 的项目 A 和需要 php 5.4 的项目 B。
并且使用虚拟机意味着你只需要你正在使用的计算机就行,而不需要专用硬件来镜像你的生产环境。
当多个开发人员在一个项目上工作时,它也很方便,他们都可以运行一个包含所有需求的环境,但是维护多台机器并确保所有的需求都具有相同的版本是非常困难的,这时 Vagrant 就能派上用场了。
使用虚拟机的好处
- 你的虚拟机与主机环境是分开的
- 你可以根据你代码的要求裁剪一个定制虚拟机
- 不会影响其他虚拟机
- 可以运行在你的主机上无法运行的程序,例如在 Ubuntu 中运行一些只能在 Windows 运行的软件
什么是 Vagrant
简而言之,这是一个与虚拟机一起工作的工具,可以让你自动创建和删除虚拟机。
它围绕一个名为 VagrantFile
的配置文件而工作,这个配置文件告诉 Vagrant 你想要安装的操作系统,以及一些其他选项,如 IP 和目录同步。 你还可以在虚拟机上添加一个命令的配置脚本。
通过共享这个 VagrantFile
,项目的所有开发人员全可以使用完全相同的虚拟机。
安装要求
安装 VirtualBox
VirtualBox 是运行虚拟机的程序,它可以从 Ubuntu 仓库中安装。
sudo apt-get install virtualbox
安装 Vagrant
对于 Vagrant 本身,你要前往 https://www.vagrantup.com/downloads.html 查看适用于你的操作系统的安装软件包。
安装增强功能
如果你打算与虚拟机共享任何文件夹,则需要安装以下插件。
vagrant plugin install vagrant-vbguest
配置 Vagrant
首先我们需要为 Vagrant 创建一个文件夹。
mkdir ~/Vagrant/test-vm
cd ~/Vagrant/test-vm
创建 VagrantFile:
vagrant init
开启虚拟机:
vagrant up
登录机器:
vagrant-ssh
此时,你将拥有一个基本的 vagrant 机器,以及一个名为 VagrantFile
的文件。
定制
在上面的步骤中创建的 VagrantFile
看起来类似于以下内容
VagrantFile:
# -*- mode: ruby -*-
1. vi: set ft=ruby :
1. All Vagrant configuration is done below. The "2" in Vagrant.configure
1. configures the configuration version (we support older styles for
1. backwards compatibility). Please don't change it unless you know what
1. you're doing.
Vagrant.configure("2") do |config|
1. The most common configuration options are documented and commented below.
1. For a complete reference, please see the online documentation at
1. https://docs.vagrantup.com.
1. Every Vagrant development environment requires a box. You can search for
1. boxes at https://vagrantcloud.com/search.
config.vm.box = "base"
1. Disable automatic box update checking. If you disable this, then
1. boxes will only be checked for updates when the user runs
1. `vagrant box outdated`. This is not recommended.
1. config.vm.box_check_update = false
1. Create a forwarded port mapping which allows access to a specific port
1. within the machine from a port on the host machine. In the example below,
1. accessing "localhost:8080" will access port 80 on the guest machine.
1. NOTE: This will enable public access to the opened port
1. config.vm.network "forwarded_port", guest: 80, host: 8080
1. Create a forwarded port mapping which allows access to a specific port
1. within the machine from a port on the host machine and only allow access
1. via 127.0.0.1 to disable public access
1. config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
1. Create a private network, which allows host-only access to the machine
1. using a specific IP.
1. config.vm.network "private_network", ip: "192.168.33.10"
1. Create a public network, which generally matched to bridged network.
1. Bridged networks make the machine appear as another physical device on
1. your network.
1. config.vm.network "public_network"
1. Share an additional folder to the guest VM. The first argument is
1. the path on the host to the actual folder. The second argument is
1. the path on the guest to mount the folder. And the optional third
1. argument is a set of non-required options.
1. config.vm.synced_folder "../data", "/vagrant_data"
1. Provider-specific configuration so you can fine-tune various
1. backing providers for Vagrant. These expose provider-specific options.
1. Example for VirtualBox:
1. 1. config.vm.provider "virtualbox" do |vb|
1. # Display the VirtualBox GUI when booting the machine
1. vb.gui = true
1. 1. # Customize the amount of memory on the VM:
1. vb.memory = "1024"
1. end
1. 1. View the documentation for the provider you are using for more
1. information on available options.
1. Enable provisioning with a shell script. Additional provisioners such as
1. Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
1. documentation for more information about their specific syntax and use.
1. config.vm.provision "shell", inline: