一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

使用Vagrant 构建开发环境

时间:2016-06-24 编辑:简简单单 来源:一聚教程网

最近vagrant比较流行,它的主要作用简言之就是打包一份已经装好的系统,打包好的系统在各平台通用。特别是对于一个比较复杂的开发环境,新换一台电脑,新换一个工作,必须在重新搭建环境,这种事情搞起来还是比较费时间的,比如PHP开发所用的LAMP环境(我平时都装LANMP – Apache+Nginx+MySQL+PHP),还有Redis,MongoDB,RabbitMQ这些扩展的东东,虽然自己有完全的文档,但是多年搞下来也成一个体力活儿了。这样vagrant对我的优势简直不言而愉了。可以将一个已经打包好的系统给你,你可以和我有完全相当的开发环境。节省时间,提升效率

废话不多说了,接下来开始使用这个vagrant

安装软件的事情就不??铝耍?agrant+virtualbox),闭眼下一步就行
下载一个box镜像(直接找个镜像下载就行)

上面那两步完成以后,让我们来添加一个叫lamp的box

添加一个叫lamp的虚拟机(MAC OS X环境)

PHP

vagrant box remove lamp
mkdir  lamp
cd lamp
vagrant box add lamp ~/dev/ubuntu-14.04-amd64.box
vagrant init

1
2
3
4
5
6
vagrant box remove lamp
mkdir  lamp
cd lamp
vagrant box add lamp ~/dev/ubuntu-14.04-amd64.box
vagrant init
 
vi Vagrantfile 编辑配置文件,加入以下内容,在初始化的时候会用到

在这里介绍一下网络配置,一般情况单机开发用私有网络,只有自己可以访问这个IP,但是这个IP还可以联互联网,满足了我们大部分人的需求
PHP

config.vm.box = "lamp"
config.vm.hostname = "lamp"
config.vm.network "private_network", ip: "192.168.8.10"

1
2
3
4
config.vm.box = "lamp"
config.vm.hostname = "lamp"
config.vm.network "private_network", ip: "192.168.8.10"
 
vagrant ssh

vagrant package lamp –output D:/vagrant/lamp.box

———————————–分割线下面是一些命令————————————

PHP

vagrant box add lamp ~/dev/ubuntu-14.04-amd64.box #添加虚拟机 lamp
vagrant box list #列出所有虚拟机
vagrant box remove “web" #删除虚拟机web

vagrant init  # 初始化
vagrant up  # 启动虚拟机
vagrant halt  # 关闭虚拟机
vagrant reload  # 重启虚拟机
vagrant ssh  # SSH 至虚拟机
vagrant status  # 查看虚拟机运行状态
vagrant destroy  # 销毁当前虚拟机

注:如果网络模式中使用 private_network 的话,在打包之前需要清除一下private_network的设置,避免不必要的错误:
[centos]
sudo rm -f /etc/udev/rule.d/70-persistent-net.rules
[ubuntu]
sudo vim /etc/interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vagrant box add lamp ~/dev/ubuntu-14.04-amd64.box #添加虚拟机 lamp
vagrant box list #列出所有虚拟机
vagrant box remove “web" #删除虚拟机web
 
vagrant init  # 初始化
vagrant up  # 启动虚拟机
vagrant halt  # 关闭虚拟机
vagrant reload  # 重启虚拟机
vagrant ssh  # SSH 至虚拟机
vagrant status  # 查看虚拟机运行状态
vagrant destroy  # 销毁当前虚拟机
 
注:如果网络模式中使用 private_network 的话,在打包之前需要清除一下private_network的设置,避免不必要的错误:
[centos]
sudo rm -f /etc/udev/rule.d/70-persistent-net.rules
[ubuntu]
sudo vim /etc/interface
 
———————————–分割线下面是在windows下实际操作————————————

PHP

PS D:\vagrant> ls


    目录: D:\vagrant


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         2016/3/8     17:08             19 p.php
-a----         2016/3/9     13:10      436951040 trusty-server-cloudimg-amd64-vagrant-disk1.box
-a----         2016/3/9     21:15     1220018733 ubuntu-14.04-amd64.box
-a----        2016/3/10      8:52           3180 Vagrantfile


PS D:\vagrant> vagrant box add lamp D:\vagrant\ubuntu-14.04-amd64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'lamp' (v0) for provider:
    box: Unpacking necessary files from: file://D:/vagrant/ubuntu-14.04-amd64.box
    box: Progress: 100% (Rate: 42.3M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'lamp' (v0) for 'virtualbox'!
PS D:\vagrant> vagrant box list
lamp (virtualbox, 0)
PS D:\vagrant> vagrant init lamp
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.
#########################在此处有编辑配置文件的操作#############################
PS D:\vagrant> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'lamp'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1457571466457_85328
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
    default: Adapter 3: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.0
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => D:/vagrant
PS D:\vagrant>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
PS D:\vagrant> ls
 
 
    目录: D:\vagrant
 
 
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         2016/3/8     17:08             19 p.php
-a----         2016/3/9     13:10      436951040 trusty-server-cloudimg-amd64-vagrant-disk1.box
-a----         2016/3/9     21:15     1220018733 ubuntu-14.04-amd64.box
-a----        2016/3/10      8:52           3180 Vagrantfile
 
 
PS D:\vagrant> vagrant box add lamp D:\vagrant\ubuntu-14.04-amd64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'lamp' (v0) for provider:
    box: Unpacking necessary files from: file://D:/vagrant/ubuntu-14.04-amd64.box
    box: Progress: 100% (Rate: 42.3M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'lamp' (v0) for 'virtualbox'!
PS D:\vagrant> vagrant box list
lamp (virtualbox, 0)
PS D:\vagrant> vagrant init lamp
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.
#########################在此处有编辑配置文件的操作#############################
PS D:\vagrant> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'lamp'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1457571466457_85328
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
    default: Adapter 3: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.36
    default: VirtualBox Version: 5.0
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => D:/vagrant
PS D:\vagrant>
 
用xshell来连接vagrant

热门栏目