下载安装 VirtualBox :https://www.virtualbox.org/php
下载安装 Vagrant :http://www.vagrantup.com/node
下载须要使用的 box :mysql
以下添加一个debian的boxredis
E:\ubuntu\vagrant-box-ngixn-php-fpm-mysql-redis-nodejs>vagrant box add debian p ackage.box ==> box: Adding box 'debian' (v0) for provider: box: Downloading: file://E:/ubuntu/vagrant-box-ngixn-php-fpm-mysql-redis-nod ejs/package.box box: Progress: 100% (Rate: 11.0M/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'debian' (v0) for 'virtualbox'!
在任何vagrant工程下面都有一个vagrantfile,就像makefile一眼规,用来配置vagrant的欣慰所建立的虚拟机信息。sql
vagrant box add abc boxpath[url|path] #abc未名称
vagrant init abc #初始化shell
D:\work\test>vagrant init debian
A Vagrantfile
has been placed in this directory. You are now
ready to vagrant up
your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
vagrantup.com
for more information on using Vagrant.ubuntu
vagrant up #启动windows
D:\work\test>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'debian'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: test_default_1413449093680_48484
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => D:/work/test网络
vagrant ssh #ssh登陆ssh
D:\work\test>vagrant ssh
ssh
executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:
Host: 127.0.0.1
Port: 2222
Username: vagrant
Private key: C:/Users/zhangwei_f/.vagrant.d/insecure_private_key
开启ssh后使用xshell登陆
Vagrant的网络有三种模式
一、较为经常使用是端口映射,就是将虚拟机中的端口映射到宿主机对应的端口直接使用 ,在Vagrantfile中配置:
config.vm.network :forwarded_port, guest: 80, host: 8080 guest: 80 表示虚拟机中的80端口, host: 8080 表示映射到宿主机的8080端口。 开启这个后,若是vagrant已经启动了,在命令行输入 vagrant reload 重启机器,就能够再宿主机伤使用 localhost:8080来访问虚拟机的localhost:80 。
二、若是须要本身自由的访问虚拟机,可是别人不须要访问虚拟机,可使用private_network,并为虚拟机设置IP ,在Vagrantfile中配置:
config.vm.network :private_network, ip: "192.168.1.104" 192.168.1.104 表示虚拟机的IP,多台虚拟机的话须要互相访问的话,设置在相同网段便可
三、若是须要将虚拟机做为当前局域网中的一台计算机,由局域网进行DHCP,那么在Vagrantfile中配置:
config.vm.network :public_network
既然是开发环境,那么开发工做确定仍是须要在本地完成,而不是都要进到虚拟机中去完成,虚拟机就好好在后台运行服务就行了,否则就本末倒置了,因此这里就须要使用目录映射功能,将本地的目录映射到虚拟机的对应目录。
默认状况下,当前的工做目录,会被映射到虚拟机的 /vagrant 目录,当前目录下的文件能够直接在 /vagrant 下进行访问,固然也能够在经过 ln 建立软链接,如
ln -fs /vagrant/wwwroot /var/www
来进行目录映射,固然,从自动化配置的角度,能不进系统就不须要进系统,因此在Vagrant也能够进行目录映射的操做:
config.vm.synced_folder "wwwroot/", "/var/www"
前面的参数 “wwwroot/” 表示的是本地的路径,这里使用对于工做目录的相对路径,这里也可使用绝对路径,好比: “d:/www/”
后面的参数 “/var/www” 表示虚拟机中对应映射的目录。
vagrant up (启动虚拟机) vagrant halt (关闭虚拟机——对应就是关机) vagrant suspend (暂停虚拟机——只是暂停,虚拟机内存等信息将以状态文件的方式保存在本地,能够执行恢复操做后继续使用) vagrant resume (恢复虚拟机 —— 与前面的暂停相对应) vagrant destroy (删除虚拟机,删除后在当前虚拟机所作进行的除开Vagrantfile中的配置都不会保留) vagrant reload (重启)