VAGRANT初体验.md

win7 64bit 下载并安装

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

参考官方文档进行初步训练

https://www.vagrantup.com/docs/installation/

先下载这个box,下载完成后放到C:\HashiCorp\boxes\virtualbox.box下:

https://atlas.hashicorp.com/ubuntu/boxes/trusty64/versions/20160323.0.0/providers/virtualbox.box

#初始化,只是建立了环境,没没有加载box
C:\HashiCorp\project_test>mkdir vproject_test
C:\HashiCorp\project_test>cd project_test
C:\HashiCorp\project_test>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.
#这个地方用的box文件只是个基础模板,不会被更新的,被更新的是在VirtualBox VMs目录下的虚拟机文件。
C:\HashiCorp\project_test>vagrant box add ubuntu C:\HashiCorp\boxes\virtualbox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu' (v0) for provider:
    box: Unpacking necessary files from: file://C:/HashiCorp/boxes/virtualbox.box
    box: Progress: 100% (Rate: 289M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'ubuntu' (v0) for 'virtualbox'!

C:\HashiCorp\project_test>

修改C:\HashiCorp\project_test下的Vagrantfile:

config.vm.box = “base” 改为 config.vm.box = “ubuntu”

启动虚拟机

C:\HashiCorp\project_test>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: project_test_default_1459092508107_50458
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Remote connection disconnect. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => C:/HashiCorp/project_test

C:\HashiCorp\project_test>

用xshell登陆

ip为127.0.0.1
用户名:vagrant
选择用私钥登陆。

安装Apache

再C:\HashiCorp\project_test路径下新建一个installApache.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

重新载入

C:\HashiCorp\project_test>vagrant reload --provision
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => C:/HashiCorp/project_test
==> default: Running provisioner: shell...
    default: Running: C:/Users/ADMINI~1/AppData/Local/Temp/vagrant-shell20160327-9000-1xdcjvp.sh
==> default: stdin: is not a tty
==> default: Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
==> default: Ign http://archive.ubuntu.com trusty InRelease
==> default: Get:2 http://security.ubuntu.com trusty-security/main Sources [110 kB]
==> default: Get:3 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
==> default: Get:4 http://security.ubuntu.com trusty-security/universe Sources [34.1 kB]
==> default: Get:5 http://archive.ubuntu.com trusty-backports InRelease [65.9 kB]
==> default: Get:6 http://security.ubuntu.com trusty-security/main amd64 Packages [448 kB]
==> default: Hit http://archive.ubuntu.com trusty Release.gpg
==> default: Get:7 http://archive.ubuntu.com trusty-updates/main Sources [270 kB]
==> default: Get:8 http://security.ubuntu.com trusty-security/universe amd64 Packages [125 kB]
==> default: Get:9 http://security.ubuntu.com trusty-security/main Translation-en [244 kB]
==> default: Get:10 http://security.ubuntu.com trusty-security/universe Translation-en [74.0 kB]
==> default: Get:11 http://archive.ubuntu.com trusty-updates/restricted Sources [5,352 B]
==> default: Get:12 http://archive.ubuntu.com trusty-updates/universe Sources [151 kB]
==> default: Get:13 http://archive.ubuntu.com trusty-updates/multiverse Sources [5,946 B]
==> default: Get:14 http://archive.ubuntu.com trusty-updates/main amd64 Packages [740 kB]
==> default: Get:15 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [15.9 kB]
==> default: Get:16 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [356 kB]
==> default: Get:17 http://archive.ubuntu.com trusty-updates/multiverse amd64 Packages [13.2 kB]
==> default: Get:18 http://archive.ubuntu.com trusty-updates/main Translation-en [370 kB]
==> default: Get:19 http://archive.ubuntu.com trusty-updates/multiverse Translation-en [7,227 B]
==> default: Get:20 http://archive.ubuntu.com trusty-updates/restricted Translation-en [3,699 B]
==> default: Get:21 http://archive.ubuntu.com trusty-updates/universe Translation-en [186 kB]
==> default: Get:22 http://archive.ubuntu.com trusty-backports/main Sources [8,696 B]
==> default: Get:23 http://archive.ubuntu.com trusty-backports/restricted Sources [28 B]
==> default: Get:24 http://archive.ubuntu.com trusty-backports/universe Sources [34.5 kB]
==> default: Get:25 http://archive.ubuntu.com trusty-backports/multiverse Sources [1,898 B]
==> default: Get:26 http://archive.ubuntu.com trusty-backports/main amd64 Packages [9,782 B]
==> default: Get:27 http://archive.ubuntu.com trusty-backports/restricted amd64 Packages [28 B]
==> default: Get:28 http://archive.ubuntu.com trusty-backports/universe amd64 Packages [41.3 kB]
==> default: Get:29 http://archive.ubuntu.com trusty-backports/multiverse amd64 Packages [1,571 B]
==> default: Get:30 http://archive.ubuntu.com trusty-backports/main Translation-en [5,843 B]
==> default: Get:31 http://archive.ubuntu.com trusty-backports/multiverse Translation-en [1,215 B]
==> default: Get:32 http://archive.ubuntu.com trusty-backports/restricted Translation-en [28 B]
==> default: Get:33 http://archive.ubuntu.com trusty-backports/universe Translation-en [35.9 kB]
==> default: Hit http://archive.ubuntu.com trusty Release
==> default: Get:34 http://archive.ubuntu.com trusty/main Sources [1,064 kB]
==> default: Get:35 http://archive.ubuntu.com trusty/restricted Sources [5,433 B]
==> default: Get:36 http://archive.ubuntu.com trusty/universe Sources [6,399 kB]
==> default: Get:37 http://archive.ubuntu.com trusty/multiverse Sources [174 kB]
==> default: Hit http://archive.ubuntu.com trusty/main amd64 Packages
==> default: Hit http://archive.ubuntu.com trusty/restricted amd64 Packages
==> default: Hit http://archive.ubuntu.com trusty/universe amd64 Packages
==> default: Hit http://archive.ubuntu.com trusty/multiverse amd64 Packages
==> default: Hit http://archive.ubuntu.com trusty/main Translation-en
==> default: Hit http://archive.ubuntu.com trusty/multiverse Translation-en
==> default: Hit http://archive.ubuntu.com trusty/restricted Translation-en
==> default: Hit http://archive.ubuntu.com trusty/universe Translation-en
==> default: Ign http://archive.ubuntu.com trusty/main Translation-en_US
==> default: Ign http://archive.ubuntu.com trusty/multiverse Translation-en_US
==> default: Ign http://archive.ubuntu.com trusty/restricted Translation-en_US
==> default: Ign http://archive.ubuntu.com trusty/universe Translation-en_US
==> default: Fetched 11.1 MB in 3min 37s (51.2 kB/s)
==> default: Reading package lists...
==> default: Reading package lists...
==> default: Building dependency tree...
==> default: Reading state information...
==> default: The following extra packages will be installed:
==> default:   apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3
==> default:   libaprutil1-ldap ssl-cert
==> default: Suggested packages:
==> default:   apache2-doc apache2-suexec-pristine apache2-suexec-custom apache2-utils
==> default:   openssl-blacklist
==> default: The following NEW packages will be installed:
==> default:   apache2 apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3
==> default:   libaprutil1-ldap ssl-cert
==> default: 0 upgraded, 8 newly installed, 0 to remove and 3 not upgraded.
==> default: Need to get 1,283 kB of archives.
==> default: After this operation, 5,348 kB of additional disk space will be used.
==> default: Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libapr1 amd64 1.5.0-1 [85.1 kB]
==> default: Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libaprutil1 amd64 1.5.3-1 [76.4 kB]
==> default: Get:3 http://archive.ubuntu.com/ubuntu/ trusty/main libaprutil1-dbd-sqlite3 amd64 1.5.3-1 [10.5 kB]
==> default: Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libaprutil1-ldap amd64 1.5.3-1 [8,634 B]
==> default: Get:5 http://archive.ubuntu.com/ubuntu/ trusty-updates/main apache2-bin amd64 2.4.7-1ubuntu4.9 [839 kB]
==> default: Get:6 http://archive.ubuntu.com/ubuntu/ trusty-updates/main apache2-data all 2.4.7-1ubuntu4.9 [160 kB]
==> default: Get:7 http://archive.ubuntu.com/ubuntu/ trusty-updates/main apache2 amd64 2.4.7-1ubuntu4.9 [87.5 kB]
==> default: Get:8 http://archive.ubuntu.com/ubuntu/ trusty/main ssl-cert all 1.0.33 [16.6 kB]
==> default: dpkg-preconfigure: unable to re-open stdin: No such file or directory
==> default: Fetched 1,283 kB in 50s (25.2 kB/s)
==> default: Selecting previously unselected package libapr1:amd64.
==> default: (Reading database ... 62985 files and directories currently installed.)
==> default: Preparing to unpack .../libapr1_1.5.0-1_amd64.deb ...
==> default: Unpacking libapr1:amd64 (1.5.0-1) ...
==> default: Selecting previously unselected package libaprutil1:amd64.
==> default: Preparing to unpack .../libaprutil1_1.5.3-1_amd64.deb ...
==> default: Unpacking libaprutil1:amd64 (1.5.3-1) ...
==> default: Selecting previously unselected package libaprutil1-dbd-sqlite3:amd64.
==> default: Preparing to unpack .../libaprutil1-dbd-sqlite3_1.5.3-1_amd64.deb ...
==> default: Unpacking libaprutil1-dbd-sqlite3:amd64 (1.5.3-1) ...
==> default: Selecting previously unselected package libaprutil1-ldap:amd64.
==> default: Preparing to unpack .../libaprutil1-ldap_1.5.3-1_amd64.deb ...
==> default: Unpacking libaprutil1-ldap:amd64 (1.5.3-1) ...
==> default: Selecting previously unselected package apache2-bin.
==> default: Preparing to unpack .../apache2-bin_2.4.7-1ubuntu4.9_amd64.deb ...
==> default: Unpacking apache2-bin (2.4.7-1ubuntu4.9) ...
==> default: Selecting previously unselected package apache2-data.
==> default: Preparing to unpack .../apache2-data_2.4.7-1ubuntu4.9_all.deb ...
==> default: Unpacking apache2-data (2.4.7-1ubuntu4.9) ...
==> default: Selecting previously unselected package apache2.
==> default: Preparing to unpack .../apache2_2.4.7-1ubuntu4.9_amd64.deb ...
==> default: Unpacking apache2 (2.4.7-1ubuntu4.9) ...
==> default: Selecting previously unselected package ssl-cert.
==> default: Preparing to unpack .../ssl-cert_1.0.33_all.deb ...
==> default: Unpacking ssl-cert (1.0.33) ...
==> default: Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
==> default: Processing triggers for ureadahead (0.100.0-16) ...
==> default: Processing triggers for ufw (0.34~rc-0ubuntu2) ...
==> default: Setting up libapr1:amd64 (1.5.0-1) ...
==> default: Setting up libaprutil1:amd64 (1.5.3-1) ...
==> default: Setting up libaprutil1-dbd-sqlite3:amd64 (1.5.3-1) ...
==> default: Setting up libaprutil1-ldap:amd64 (1.5.3-1) ...
==> default: Setting up apache2-bin (2.4.7-1ubuntu4.9) ...
==> default: Setting up apache2-data (2.4.7-1ubuntu4.9) ...
==> default: Setting up apache2 (2.4.7-1ubuntu4.9) ...
==> default: Enabling module mpm_event.
==> default: Enabling module authz_core.
==> default: Enabling module authz_host.
==> default: Enabling module authn_core.
==> default: Enabling module auth_basic.
==> default: Enabling module access_compat.
==> default: Enabling module authn_file.
==> default: Enabling module authz_user.
==> default: Enabling module alias.
==> default: Enabling module dir.
==> default: Enabling module autoindex.
==> default: Enabling module env.
==> default: Enabling module mime.
==> default: Enabling module negotiation.
==> default: Enabling module setenvif.
==> default: Enabling module filter.
==> default: Enabling module deflate.
==> default: Enabling module status.
==> default: Enabling conf charset.
==> default: Enabling conf localized-error-pages.
==> default: Enabling conf other-vhosts-access-log.
==> default: Enabling conf security.
==> default: Enabling conf serve-cgi-bin.
==> default: Enabling site 000-default.
==> default:  * Starting web server apache2
==> default: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.2.15. Set the 'ServerName' directive globally to suppre
ss this message
==> default:  *
==> default: Setting up ssl-cert (1.0.33) ...
==> default: Processing triggers for libc-bin (2.19-0ubuntu6.7) ...
==> default: Processing triggers for ureadahead (0.100.0-16) ...
==> default: Processing triggers for ufw (0.34~rc-0ubuntu2) ...

C:\HashiCorp\project_test>

配置端口转发

在Vagrantfile文件加入:

config.vm.network :forwarded_port, guest: 80, host: 4567

重新载入:

C:\HashiCorp\project_test>vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 80 (guest) => 4567 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => C:/HashiCorp/project_test
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

C:\HashiCorp\project_test>

验证 在本地计算机上输入:http://127.0.0.1:4567/ 就可以看到结果了。

关闭虚拟机

可以用两个命令,一个是暂挂,一个是关机

#下面这个命令优点是下次再启动比较快,缺点是耗费磁盘空间
vagrant suspend
#下面这个命令的优点是释放磁盘空间,缺点是启动慢
vagrant halt
#下面的这个命令相当于还原的命令,一旦执行就回到了刚刚建立虚拟机的状态了

重启下

C:\HashiCorp\project_test>vagrant halt
==> default: Attempting graceful shutdown of VM...

C:\HashiCorp\project_test>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 80 (guest) => 4567 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => C:/HashiCorp/project_test
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

C:\HashiCorp\project_test>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贤时间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值