Vagrant 是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。它使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境。认识 Vagrant 是从 Laravel 开始的,Laravel 官方提供了一整套本地开发环境 Homestead 。刚开始的时候对它不屑一顾,总觉得很麻烦,还不如
phpStudy
,Xampp
,AppServ
等集成开发环境来得快,后来由于本地开发环境和线上正式环境不一样,项目上线时出现了一些小问题,所以下定决心使用 vagrant 打造一个和生产环境一模一样的开发环境(注意:仅软件版本一致,硬件配置会有差异)。
下载并安装Vagrant
下载地址:https://www.vagrantup.com/downloads.html
支持跨平台(Mac OX,Linux,Windows),详细安装步骤请参见官方文档。
安装完成后可以在命令行中查看版本信息,windows默认的cmd
不好用建议使用xshell
,git-bash
等工具来代替
admin@DESKTOP-68FVB12 MINGW64 /
$ vagrant.exe -v
Vagrant 2.0.0
注意:vagrant 命令的路径一定要在
$PATH
环境变量中
下载VirtualBox
下载地址:https://www.virtualbox.org/wiki/Downloads
支持跨平台(Mac OX,Linux,Windows),如果你习惯使用VMware,vagrant也是支持的,但VMware是收费软件,在国内使用我相信你懂的。
初始化开发环境
官方镜像地址:https://atlas.hashicorp.com/boxes/search
- 如果你没有本地的box
admin@DESKTOP-68FVB12 MINGW64 /
$ mkdir dev && cd dev
admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant init centos/7 ##下载box需要等待一段时间,根据网速来定
注意:我的开发环境是centos的系统,所以我选择了
centos/7
的box,如果你想使用其它开发环境,你也可以使用其它的box来初始化。
- 如果你有本地的box文件,首先将box添加到box list中
admin@DESKTOP-68FVB12 MINGW64 /
$ vagrant.exe box add lnmp /e/boxs/lnmp.box
注意:
lnmp
是自己命名的box名称,/e/boxs/lnmp.box
等于windows下e:\boxs\lnmp.box
,即box文件的路径
admin@DESKTOP-68FVB12 MINGW64 /
$ vagrant.exe box list
lnmp (virtualbox, 0)
使用刚刚添加的lnmp
box初始化开发环境
admin@DESKTOP-68FVB12 MINGW64 /
$ mkdir dev && cd dev
admin@DESKTOP-68FVB12 MINGW64 /dev/
$ vagrant init lnmp ##无需下载,直接从本地box list读取
启动Vagrant
vagrant init
完成后会在当前目录生成一个Vagrantfile
文件,该文件为vagrant启动虚拟机的配置文件。vagrant up
会按Vagrantfile
的配置项来启动虚拟机。
admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant.exe up
登录Vagrant
默认使用的是vagrant用户登录,通过修改Vagrantfile
也可指定其它用户登录
admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant.exe ssh
Last login: Wed Nov 15 01:21:07 2017 from 10.0.2.2
[vagrant@localhost ~]$
配置LNMP开发环境
关于lnmp的开发环境安装,我也写了相关的教程,详情请查看LNMP专题
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 = "lnmp"
# 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"
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"
# mac
# config.vm.synced_folder "Code", "/usr/local/nginx/html/", type:"nfs"
# windows
config.vm.synced_folder "Code", "/usr/local/nginx/html/", :nfs => (RUBY_PLATFORM =~ /linux/ or RUBY_PLATFORM =~ /darwin/)
# 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"
vb.name = "lnmp_centos7"
vb.memory = "2048"
vb.cpus = "2"
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
注意:
config.vm.network
,config.vm.synced_folder
,vb.name
,vb.memory
,vb.cpus
,以上配置可参考示例文件根据自身情况进行设置。修改配置文件后请使用vagrant reload
重新载入配置文件。
安装插件
要想正常让宿主机和虚拟机之间共享文件,光是配置Vagrantfile
是不行,还需要安装相应的插件。
admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant plugin install vagrant-vbguest
$ vagrant plugin install childprocess
$ vagrant plugin install vagrant-winnfsd
查看已安装的插件
admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant.exe plugin list
childprocess (0.6.3)
- Version Constraint: 0.6.3
vagrant-share (1.1.9, system)
vagrant-vbguest (0.15.0)
vagrant-winnfsd (1.1.0)
优化 Nginx 配置,将sendfile
的值设为off,解决静态资源刷新延迟的问题
admin@DESKTOP-68FVB12 MINGW64 /dev
$ vim /usr/local/nginx/conf/nginx.conf
...
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile off;
#tcp_nopush on;
...
将配置好的 lnmp 开发环境打包
admin@DESKTOP-68FVB12 MINGW64 /dev
$ vagrant.exe package --output lnmp.box --vagrantfile Vagrantfile
注意:打包前请将
config.vm.network
,config.vm.synced_folder
配置项先注释,vagrant package
可以不带任何参数,--output
是指定box的名称,--vagrantfile
是指定Vagrantfile的路径,这样便可生成一个你个人独有的box,无论在任何平台,只要具备了vagrant的运行环境,你便可快速启动你的开发环境。
常用vagrant命令
更多命令请查看,https://www.vagrantup.com/docs/
vagrant init # 初始化
vagrant up # 启动虚拟机
vagrant halt # 关闭虚拟机
vagrant suspend # 暂停休眠虚拟机
vagrant resume # 唤醒虚拟机
vagrant reload # 重启虚拟机
vagrant ssh # SSH 登录虚拟机
vagrant status # 查看虚拟机运行状态
vagrant destroy # 销毁当前虚拟机