Windows 下配置 Vagrant 環境

來源:互聯網
上載者:User

Windows 下配置 Vagrant 環境

Vagrant是一個基於 Ruby 的工具,用於建立和部署虛擬化開發環境。它使用 Oracle 的開源VirtualBox虛擬化系統。

Vagrant 在快速搭建開發環境方面是很贊的,試想一個團隊中,大家開發同一個東西,以前每個人都要自己搭建一套開發環境 ,有了 Vagrant,你只需要搭建一份,然後分發給所有團隊成員,這樣大家都立刻就有完全相同的開發環境了,即便有成員在Windows 下,也可以方便的使用 Linux 環境開發。如果團隊中來了新人,也不需要手把手教他怎麼搭建開發環境,給他丟一個Box 就好了,只要他掌握了 Vagrant 的使用方法,立刻就可以融入到開發中來,而不需要費心去安裝複雜的環境。

Vagrant 的跨平台的特性簡直是太棒了,這都要利益於 VirtualBox 這樣一款優秀的軟體和 Vagrant 這些天才工程師們。

Vagrant 還支援使用Chef和Puppet來維護你的虛擬開發環境,不過因為我對這兩個工具並不熟悉,本文中不作介紹,只簡單 介紹如果在 Windows 下配置一個 Vagrant 環境。

安裝 Vagrant

從 Vagrant 官網下載最新的 Vagrant 和對應的 VirtualBox 安裝後,建立一個檔案夾用來配置 Vagrant

因為使用vagrant init precise32 http://files.vagrantup.com/precise32.box命令下載 box 會比較慢, 所以最好是提前使用迅雷等工具下載好 box 放在一個檔案中,然後初始化時使用本地路徑,會快很多。

vagrant init precise32 ..\boxes\precise32.box

需要注意的是,這裡使用本地路徑時,需要使用 Windows 風格的路徑,即用\來作為路徑分隔字元。

PS:可用的 Vagrant Boxes 見這裡:http://www.vagrantbox.es/

連接埠轉寄

Vagrant 中配置連接埠轉寄非常方便

Vagrant.configure("2") do |config|  # other config here  config.vm.network :forwarded_port, guest: 80, host: 8080end

上面的配置會將 Vagrant 中的 80 連接埠和你原生 8080 連接埠建立轉寄關係,這樣你在本機訪問 http://localhost:8080 就相當於訪問 Vagrant 中的 http://localhost:80 了。

連接埠轉寄可以配置多組。

共用資料夾

使用 Vagrant 有一個非常重要的一步就是共用資料夾(得益於強大的 VirtualBox)

在Vagrantfile中設定

config.vm.synced_folder "E:/Blog", "/home/vagrant/Blog"

其中第一個參數E:/Blog為本機上需要共用的檔案夾路徑,第二個參數為 Vagrant 虛擬機器中的映射路徑,注意第二個參數需要 使用絕對路徑,如/home/vagrant/Blog

串連至 Vagrant

配置好後,就可以啟動虛擬機器並串連到 Vagrant 了。

首先,執行vagrant up,等待片刻,vagrant 就啟動好了。 ::

e:\Vagrant\precise32>vagrant reload[default] Attempting graceful shutdown of VM...[default] Setting the name of the VM...[default] Clearing any previously set forwarded ports...[default] Creating shared folders metadata...[default] Clearing any previously set network interfaces...[default] Preparing network interfaces based on configuration...[default] Forwarding ports...[default] -- 22 => 2222 (adapter 1)[default] -- 5000 => 5000 (adapter 1)[default] -- 3000 => 3000 (adapter 1)[default] Booting VM...[default] Waiting for VM to boot. This can take a few minutes.[default] VM booted and ready for use![default] Configuring and enabling network interfaces...[default] Mounting shared folders...[default] -- /vagrant[default] -- /home/vagrant/Blog[default] -- /home/vagrant/Notes[default] -- /home/vagrant/Projects

如果你vagrant up後又修改了 Vagrantfile,要使之生效,需要執行vagrant reload

在 Windows 下,不能使用vagrant ssh來直接存取 vagrnat,不過該命令會告訴你如何通過 ssh 串連 vagrant ::

e:\Vagrant\precise32>vagrant ssh`ssh` executable not found in any directories in the %PATH% variable. Is anSSH client installed? Try installing Cygwin, MinGW or Git, all of whichcontain an SSH client. Or use the PuTTY SSH client with the followingauthentication information shown below:Host: 127.0.0.1Port: 2222Username: vagrantPrivate key: C:/Documents and Settings/greatghoul/.vagrant.d/insecure_private_key

這樣你就可以使用類似 putty 的 ssh 用戶端來訪問 vagrant 來進行開發了,這裡極力推薦 Chrome 擴充Secure Shell。

Vagrant 的詳細介紹:請點這裡
Vagrant 的:請點這裡

如何使用vagrant在虛擬機器安裝Hadoop叢集

Vagrant 中高效的 Puppet 模組管理

使用 Vagrant 和 Fabric 用於整合測試

使用 Vagrant 搭建開發環境

本文永久更新連結地址:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.