作者 | Christopher Shaw
譯者 | MjSeven
Vagrant 對於虛擬機器來說是一個強大的工具,在這裡我們將研究如何在 Ubuntu 上設定和使用 Virtualbox 和 Vagrant 來提供可複製的虛擬機器。
虛擬機器,並不複雜
多年來,開發人員一直使用虛擬機器作為其工作流程的一部分,允許他們交換和更改執行軟體的環境,這通常是為了防止專案之間的衝突,例如需要 php 5.3 的專案 A 和需要 php 5.4 的專案 B。
並且使用虛擬機器意味著你只需要你正在使用的計算機就行,而不需要專用硬體來映象你的生產環境。
當多個開發人員在一個專案上工作時,它也很方便,他們都可以執行一個包含所有需求的環境,但是維護多臺機器並確保所有的需求都具有相同的版本是非常困難的,這時 Vagrant 就能派上用場了。
使用虛擬機器的好處
什麼是 Vagrant
簡而言之,這是一個與虛擬機器一起工作的工具,可以讓你自動建立和刪除虛擬機器。
它圍繞一個名為 VagrantFile
的配置檔案而工作,這個配置檔案告訴 Vagrant 你想要安裝的作業系統,以及一些其他選項,如 IP 和目錄同步。 你還可以在虛擬機器上新增一個命令的配置指令碼。
透過共享這個 VagrantFile
,專案的所有開發人員全可以使用完全相同的虛擬機器。
安裝要求
安裝 VirtualBox
VirtualBox 是執行虛擬機器的程式,它可以從 Ubuntu 倉庫中安裝。
sudo apt-get install virtualbox
安裝 Vagrant
對於 Vagrant 本身,你要前往 https://www.vagrantup.com/downloads.html 檢視適用於你的作業系統的安裝軟體包。
安裝增強功能
如果你打算與虛擬機器共享任何檔案夾,則需要安裝以下外掛。
vagrant plugin install vagrant-vbguest
配置 Vagrant
首先我們需要為 Vagrant 建立一個檔案夾。
mkdir ~/Vagrant/test-vm
cd ~/Vagrant/test-vm
建立 VagrantFile:
vagrant init
開啟虛擬機器:
vagrant up
登入機器:
vagrant-ssh
此時,你將擁有一個基本的 vagrant 機器,以及一個名為 VagrantFile
的檔案。
定製
在上面的步驟中建立的 VagrantFile
看起來類似於以下內容
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
# SHELL
end
現在這個 VagrantFile
將建立基本的虛擬機器。但 Vagrant 背後的理念是讓虛擬機器為我們的特定任務而配置,所以我們刪除註釋和調整配置。
VagrantFile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# Set the Linux Version to Debian Jessie
config.vm.box = "debian/jessie64"
# Set the IP of the Box
config.vm.network "private_network", ip: "192.168.33.10"
# Sync Our Projects Directory with the WWW directory
config.vm.synced_folder "~/Projects", "/var/www/"
# Run the following to Provision
config.vm.provision "shell", path: "install.sh"
end
現在我們有一個簡單的 VagrantFile
,它將 Linux 版本設定為 debian jessie,設定一個 IP 給我們使用,同步我們感興趣的檔案夾,並最後執行 install.sh
,這是我們可以執行 shell 命令的地方。
install.sh:
#! /usr/bin/env bash
# Variables
DBHOST=localhost
DBNAME=dbname
DBUSER=dbuser
DBPASSWD=test123
echo "[ Provisioning machine ]"
echo "1) Update APT..."
apt-get -qq update
echo "1) Install Utilities..."
apt-get install -y tidy pdftk curl xpdf imagemagick openssl vim git
echo "2) Installing Apache..."
apt-get install -y apache2
echo "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-mysqlnd
echo "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-server
mysql -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/localcerts
openssl 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.key
chmod 600 /etc/ssl/localcerts/apache*
echo "6) Setup Apache..."
a2enmod rewrite
> /etc/apache2/sites-enabled/000-default.conf
echo "
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
" >> /etc/apache2/sites-enabled/000-default.conf
service apache2 restart
echo "7) Composer Install..."
curl --silent https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
echo "8) Install NodeJS..."
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
apt-get -qq update
apt-get -y install nodejs
echo "9) Install NPM Packages..."
npm install -g gulp gulp-cli
echo "Provisioning Completed"
透過上面的步驟,在你的目錄中會有 VagrantFile
和 install.sh
,執行 vagrant 會做下麵的事情:
~/Projects
和 /var/www/
目錄透過與其他人共享 VagrantFile
和 install.sh
,你可以在兩臺不同的機器上使用完全相同的環境。
via: https://www.chris-shaw.com/blog/how-to-install-and-setup-vagrant
作者:Christopher Shaw[3] 譯者:MjSeven 校對:wxy
本文由 LCTT 原創編譯,Linux中國 榮譽推出