Linux系統對於咱們後端工程師來講在咱們的平常工做和學習中是必不可少的,可是咱們不多將本身的操做系統直接裝成Linux,而是選擇在Windows上使用VMware或者VirtualBox工具搭建Linux虛擬機,可是搭建起來相對比較複雜,Vagrant做爲一款管理虛擬開發環境的工具,只須要簡單的配置就可以輕鬆的幫助咱們管理虛擬機,如VirtualBox、VMware等,而且可以爲咱們提供一個可配置、可移植和複用的軟件環境,所以Vagrant也須要依賴這些虛擬機工具,本文使用VirtualBox做爲虛擬機工具。html
VirtualBox相對於VMware而言,顯得小巧玲瓏,並且VMware是收費的,對於咱們來講,開源免費纔是咱們最喜好的~docker
下載地址:VirtualBox下載地址shell
安裝步驟極其簡單,直接下一步apache
下載地址:Vagrant下載地址vim
安裝步驟也比較簡單,直接下一步便可後端
安裝完成以後,在命令行輸入「vagrant -v」查看Vagrant安裝版本,測試安裝是否成功
centos
Box是Vagrant的基礎鏡像,和docker的images比較像,Vagrant官方提供的鏡像倉庫中有各類版本的Box鏡像,咱們能夠按需自行下載。ruby
地址:鏡像下載地址網絡
咱們能夠直接使用"vagrant box add xxx"下載一個鏡像到本地,而且添加到vagrant 的box列表中,可是由於鏡像地址時國外的,下載比較緩慢,建議先將box鏡像下載到本地,再使用上述命令將本地box鏡像加載到vagrant的box列表中,添加鏡像以後,使用"vagrant box list"列出本地有哪些鏡像。相似於docker 的「docker images」
app
使用上一步添加的box鏡像文件來初始化一個開發環境,在初始化以前,有一個小提示,VirtualBox默認將虛擬機文件放置在C盤,即系統盤,咱們最好事先將這個路徑改爲其餘盤防止佔用系統資源,以下圖
而後建立一個存放這個開發環境的目錄,進入目錄以後,執行vagrant命令「vagrant init centos7」,init後面跟的是本地box列表中box的名字,以下圖
出現如圖中提示說明初始化成功,它會在咱們的開發目錄中爲咱們自動生成一個Vagrantfile配置文件
Vagrantfile是相似於docker的Dockerfile同樣的東西,咱們能夠修改裏面的配置參數去初始化咱們的虛擬機環境,自動生成的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 = "centos7" # 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 # Ansible, Chef, Docker, Puppet and Salt 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
Vagrantfile文件使用ruby語法,可是由於該配置文件說明及其詳細,咱們不用去了解ruby就可以輕鬆配置,下面對裏面的幾個重要配置信息進行說明:
配置完成以後,在咱們的開發目錄下,使用」vagrant up「啓動虛擬機,以下圖
此時,虛擬機已經啓動起來了,咱們可使用"vagrant ssh"鏈接到虛擬機中去
此時咱們就能夠在Linux的海洋快樂的進行玩耍了~
須要注意的是,初始化的Linux環境默認是不支持用戶名密碼進行登錄的,須要修改/etc/ssh/sshd_config配置文件,修改步驟:
像docker同樣,vagrant支持將本身配置好的虛擬機從新打包成新的box鏡像,這樣就能將咱們本身的開發環境打包出來共享給其餘小夥伴使用,別人就能夠有和本身徹底相同的開發環境了,沒必要再糾結於在我電腦上能運行,在別人電腦上就跑不起來的各類各樣奇葩的環境問題了。
首先使用"vagrant halt"命令將虛擬機正常關閉,而後在咱們的開發目錄下使用」vagrant package「進行打包
如圖所示,在咱們的開發目錄中已經生成了一個package.box文件