新聞中心
如何安裝并設(shè)置vagrant?
作者:Christopher Shaw 2018-02-01 18:05:48
系統(tǒng)
Linux
虛擬化 Vagrant 對于虛擬機(jī)來說是一個(gè)強(qiáng)大的工具,在這里我們將研究如何在 ubuntu 上設(shè)置和使用 Virtualbox 和 Vagrant 來提供可復(fù)制的虛擬機(jī)。

目前創(chuàng)新互聯(lián)已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、太湖網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
Vagrant 對于虛擬機(jī)來說是一個(gè)強(qiáng)大的工具,在這里我們將研究如何在 Ubuntu 上設(shè)置和使用 Virtualbox 和 Vagrant 來提供可復(fù)制的虛擬機(jī)。
虛擬機(jī),并不復(fù)雜
多年來,開發(fā)人員一直使用虛擬機(jī)作為其工作流程的一部分,允許他們交換和更改運(yùn)行軟件的環(huán)境,這通常是為了防止項(xiàng)目之間的沖突,例如需要 php 5.3 的項(xiàng)目 A 和需要 php 5.4 的項(xiàng)目 B。
并且使用虛擬機(jī)意味著你只需要你正在使用的計(jì)算機(jī)就行,而不需要專用硬件來鏡像你的生產(chǎn)環(huán)境。
當(dāng)多個(gè)開發(fā)人員在一個(gè)項(xiàng)目上工作時(shí),它也很方便,他們都可以運(yùn)行一個(gè)包含所有需求的環(huán)境,但是維護(hù)多臺機(jī)器并確保所有的需求都具有相同的版本是非常困難的,這時(shí) Vagrant 就能派上用場了。
使用虛擬機(jī)的好處
- 你的虛擬機(jī)與主機(jī)環(huán)境是分開的
- 你可以根據(jù)你代碼的要求裁剪一個(gè)定制虛擬機(jī)
- 不會影響其他虛擬機(jī)
- 可以運(yùn)行在你的主機(jī)上無法運(yùn)行的程序,例如在 Ubuntu 中運(yùn)行一些只能在 Windows 運(yùn)行的軟件
什么是 Vagrant
簡而言之,這是一個(gè)與虛擬機(jī)一起工作的工具,可以讓你自動創(chuàng)建和刪除虛擬機(jī)。
它圍繞一個(gè)名為 VagrantFile 的配置文件而工作,這個(gè)配置文件告訴 Vagrant 你想要安裝的操作系統(tǒng),以及一些其他選項(xiàng),如 IP 和目錄同步。 你還可以在虛擬機(jī)上添加一個(gè)命令的配置腳本。
通過共享這個(gè) VagrantFile,項(xiàng)目的所有開發(fā)人員全可以使用完全相同的虛擬機(jī)。
安裝要求
安裝 VirtualBox
VirtualBox 是運(yùn)行虛擬機(jī)的程序,它可以從 Ubuntu 倉庫中安裝。
sudo apt-get install virtualbox
安裝 Vagrant
對于 Vagrant 本身,你要前往 https://www.vagrantup.com/downloads.html 查看適用于你的操作系統(tǒng)的安裝軟件包。
安裝增強(qiáng)功能
如果你打算與虛擬機(jī)共享任何文件夾,則需要安裝以下插件。
vagrant plugin install vagrant-vbguest
配置 Vagrant
首先我們需要為 Vagrant 創(chuàng)建一個(gè)文件夾。
mkdir ~/Vagrant/test-vmcd ~/Vagrant/test-vm
創(chuàng)建 VagrantFile:
vagrant init
開啟虛擬機(jī):
vagrant up
登錄機(jī)器:
vagrant-ssh
此時(shí),你將擁有一個(gè)基本的 vagrant 機(jī)器,以及一個(gè)名為 VagrantFile 的文件。
定制
在上面的步驟中創(chuàng)建的 VagrantFile 看起來類似于以下內(nèi)容
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 = "base"# 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# SHELLend
現(xiàn)在這個(gè) VagrantFile 將創(chuàng)建基本的虛擬機(jī)。但 Vagrant 背后的理念是讓虛擬機(jī)為我們的特定任務(wù)而配置,所以我們刪除注釋和調(diào)整配置。
VagrantFile:
# -*- mode: ruby -*-# vi: set ft=ruby :Vagrant.configure("2") do |config|# Set the Linux Version to Debian Jessieconfig.vm.box = "debian/jessie64"# Set the IP of the Boxconfig.vm.network "private_network", ip: "192.168.33.10"# Sync Our Projects Directory with the WWW directoryconfig.vm.synced_folder "~/Projects", "/var/www/"# Run the following to Provisionconfig.vm.provision "shell", path: "install.sh"end
現(xiàn)在我們有一個(gè)簡單的 VagrantFile,它將 Linux 版本設(shè)置為 debian jessie,設(shè)置一個(gè) IP 給我們使用,同步我們感興趣的文件夾,并最后運(yùn)行 install.sh,這是我們可以運(yùn)行 shell 命令的地方。
install.sh:
#! /usr/bin/env bash# VariablesDBHOST=localhostDBNAME=dbnameDBUSER=dbuserDBPASSWD=test123echo "[ Provisioning machine ]"echo "1) Update APT..."apt-get -qq updateecho "1) Install Utilities..."apt-get install -y tidy pdftk curl xpdf imagemagick openssl vim gitecho "2) Installing Apache..."apt-get install -y apache2echo "3) Installing PHP and packages..."apt-get install -y php5 libapache2-mod-php5 libssh2-php php-pear php5-cli php5-common php5-curl php5-dev php5-gd php5-imagick php5-imap php5-intl php5-mcrypt php5-memcached php5-mysql php5-pspell php5-xdebug php5-xmlrpc#php5-suhosin-extension, php5-mysqlndecho "4) Installing MySQL..."debconf-set-selections <<< "mysql-server mysql-server/root_password password secret"debconf-set-selections <<< "mysql-server mysql-server/root_password_again password secret"apt-get install -y mysql-servermysql -uroot -p$DBPASSWD -e "CREATE DATABASE $DBNAME"mysql -uroot -p$DBPASSWD -e "grant all privileges on $DBNAME.* to '$DBUSER'@'localhost' identified by '$DBPASSWD'"echo "5) Generating self signed certificate..."mkdir -p /etc/ssl/localcertsopenssl req -new -x509 -days 365 -nodes -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -out /etc/ssl/localcerts/apache.pem -keyout /etc/ssl/localcerts/apache.keychmod 600 /etc/ssl/localcerts/apache*echo "6) Setup Apache..."a2enmod rewrite> /etc/apache2/sites-enabled/000-default.confecho "ServerAdmin webmaster@localhostDocumentRoot /var/www/ErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combined" >> /etc/apache2/sites-enabled/000-default.confservice apache2 restartecho "7) Composer Install..."curl --silent https://getcomposer.org/installer | phpmv composer.phar /usr/local/bin/composerecho "8) Install NodeJS..."curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -apt-get -qq updateapt-get -y install nodejsecho "9) Install NPM Packages..."npm install -g gulp gulp-cliecho "Provisioning Completed"
通過上面的步驟,在你的目錄中會有 VagrantFile 和 install.sh,運(yùn)行 vagrant 會做下面的事情:
- 采用 Debian Jessie 來創(chuàng)建虛擬機(jī)
- 將機(jī)器的 IP 設(shè)置為 192.168.33.10
- 同步
~/Projects和/var/www/目錄 - 安裝并設(shè)置 Apache、Mysql、PHP、Git、Vim
- 安裝并運(yùn)行 Composer
- 安裝 Nodejs 和 gulp
- 創(chuàng)建一個(gè) MySQL 數(shù)據(jù)庫
- 創(chuàng)建自簽名證書
通過與其他人共享 VagrantFile 和 install.sh,你可以在兩臺不同的機(jī)器上使用完全相同的環(huán)境。
當(dāng)前文章:如何安裝并設(shè)置Vagrant?
分享地址:http://fisionsoft.com.cn/article/dhdcjeh.html


咨詢
建站咨詢
