Vagrant Up and Running

目录

  • Install Vagrant

    • Installing Vagrant
    • Verifying the Installation
  • Project Setup
  • Box
  • Up And SSH
  • Synced Folders
  • Provisioning
    • Installing Apache
    • Provision!
  • Networking
    • Port Forwarding
  • Teardown
  • Rebuild
  • Install xfce and virtualbox additions

Install Vagrant

Installing Vagrant

https://www.vagrantup.com/downloads.html

Vagrant 2.2.5

Verifying the Installation

$ vagrant --version

Project Setup

ubuntu/bionic64 Vagrant box

$ mkdir ubuntu
$ cd ubuntu
$ vagrant init ubuntu/bionic64

This will place a Vagrantfile in your current directory.

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 = "ubuntu/bionic64"

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

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

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

Box

$ vagrant box list
$ vagrant box add ubuntu/bionic64

Up And SSH

$ vagrant up
$ vagrant ssh
[email protected]:~$ logout
Connection to 127.0.0.1 closed.

Synced Folders

$ vagrant up
...
$ vagrant ssh
...
[email protected]:~$ ls /vagrant
Vagrantfile
[email protected]:~$ touch /vagrant/foo
[email protected]:~$ exit
$ ls
foo Vagrantfile

Provisioning

Installing Apache

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

Vagrantfile

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

Provision!

After everything is configured, just run vagrant up to create your machine and Vagrant will automatically provision it. You should see the output from the shell script appear in your terminal.

If the guest machine is already running from a previous step, run vagrant reload --provision, which will quickly restart your virtual machine, skipping the initial import step. The provision flag on the reload command instructs Vagrant to run the provisioners, since usually Vagrant will only do this on the first vagrant up.

After Vagrant completes running, the web server will be up and running. You cannot see the website from your own browser (yet), but you can verify that the provisioning works by loading a file from SSH within the machine:

$ vagrant ssh
...
[email protected]:~$ wget -qO- 127.0.0.1

Networking

Port Forwarding

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, guest: 80, host: 8080
end

Teardown

挂起

$ vagrant suspend

关闭

$ vagrant halt

销毁

$ vagrant destroy

Rebuild

$ vagrant up

Install xfce and virtualbox additions

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 = "ubuntu/bionic64"

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

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

  # 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"
  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
  #
  config.vm.provision :shell, path: "bootstrap.sh"
  #
  config.vm.provision "shell", inline: "sudo apt-add-repository multiverse && sudo apt-get update"
  # Install xfce and virtualbox additions
  config.vm.provision "shell", inline: "sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11"
  # Permit anyone to start the GUI
  config.vm.provision "shell", inline: "sudo sed -i 's/allowed_users=.*$/allowed_users=anybody/' /etc/X11/Xwrapper.config"
  #
  config.vm.provision "shell", inline: "sudo apt-get install -y lightdm lightdm-gtk-greeter"
  #
  config.vm.provision "shell", inline: "sudo apt-get install -y xfce4-whiskermenu-plugin"
end

原文地址:https://www.cnblogs.com/typescript/p/11254501.html

时间: 2024-08-29 21:32:16

Vagrant Up and Running的相关文章

使用 Vagrant 构建开发环境

摘要:本文描述了如使用 Vagrant 构建统一的开发环境. 问题 作为开发人员,我们通常面临的问题有: 开发环境需要手工安装配置,这包括操作系统(CentOS.Ubuntu).PHP/HHVM.Python.Node.js.MySQL.Apache/Nginx等的版本和配置. 无法重现问题.甲:你说的问题,怎么在我的机器上没事儿啊?乙:可问题的确存在,不信你到我的机器上来看.甲:那我的机器和你的环境哪里不同呢?因为环境不同,比如开发环境和生产环境不同,和测试环境也不完全相同. 团队中没有统一的

vagrant 知识库

版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://wushaobo.info/?p=83 Vagrant让虚拟化技术走近寻常家.脚踏实地地说,网络上类似“两分钟入门”的文章有不少,但求助各种问题的帖子更多,因为vagrant官网的文档太简洁了,这里头有些道理有些坑,都没涉及到.即便是O’Reilly两天前出版的 <Vagrant: Up and Running> 也含糊着某些问题.因而在Google了各种的前人零碎经验的基础上.以自己实践证明之后,有了我这篇文章,

大数据系统工具集

Bootstrapping引导:Kickstart.Cobbler.rpmbuild/xen.kvm.lxc.Openstack. Cloudstack.Opennebula.Eucalyplus.RHEV配置类工具:Capistrano.Chef.puppet.func.salstack.Ansible.rundeck监控类工具:Cacti.Nagios(Icinga).Zabbix.基于时间监控前端Grafana.Mtop 日志系统:Logstash.Scribe绘图工具:RRDtool.G

vagrant初始登录失败的一般性解决方案

vagrant初始登录失败的一般性解决方案 http://www.cnblogs.com/csliwei/p/5860005.html 今天mac机上,遇到的 vagrant halt && vagrant up后 一直卡在: ==> default: Running 'pre-boot' VM customizations...==> default: Booting VM...==> default: Waiting for machine to boot. This

Vagrant常用命令

Vagrant常用命令 Vagrant的几个命令: vagrant box add 添加box的操作 vagrant init 初始化box的操作 vagrant up 启动虚拟机的操作 vagrant ssh 登录拟机的操作 Vagrant还包括如下一些操作: vagrant box list 显示当前已经添加的box列表 $ vagrant box list base (virtualbox) vagrant box remove 删除相应的box $ vagrant box remove

Vagrant Tip: Virtualbox Guest Additions

Vagrant Tip: Virtualbox Guest Additions 12 February 2016 Tired of seeing this message when you run vagrant up? ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not m

在Mac OS上安装Vagrant和Docker的教程

转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/128.html?1455808640 当听到很多人在说Docker是多么多么的棒,很多新潮的孩子都在使用它时,我决定在我的开发环境上也来尝试下.在下面的这篇文章中,我将讲解在Mac OS X怎样建立Postgres,Elasticsearch和Redis. 什么是Docker Docker用轻量容器把一个APP从它运行的OS中隔离开.它把APP放入到一个孤立的盒子中,对外

vagrant 入门3

创建第一个Vagrant虚拟环境以及工程(续集): (8) Provisioning: 通常情况下Box只做最基本的设置,而不是一次到位的设置好所有的环境.Vagrant通常使用chef或者Puppet来做进一步的环境搭建. 回到刚才创建的index.html,我们需要安装Apache.我们下面用Puppet来完成这一设置. 1. 在项目的根目录下创建文件夹manifests,然后在该文件家中创建Puppet的配置文件default.pp,该文件内容如下: # Basic Puppet Apac

iOS安装 vitual box 和vagrant template

首先需要安装brew 最新的地址:http://brew.sh bogon:~ wj$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" ==> This script will install: /usr/local/bin/brew /usr/local/Library/... /usr/local/share/man/man1/brew.1 ==