vagrant特点:跨平台,可分发
关于vagrant 是什么?
请参考一下文章:点击打开链接
好处:我们可以把需要的环境软件安装打包进行分发 还可以随时迭代升级追加更多的软件
现在开始正文部分:本人mac电脑
一.安装 virtualbox(推荐使用 并且支持mac windows linux)
下载地址:点击打开链接
二.安装vagrant (推荐最新版本)
下载地址:点击打开链接
常用命令说明:
- vagrant init 初始化vagrantfile
- vagrant add box 添加box,自动帮你生成vagrantfile
- vagrant halt 关闭虚拟机
- vagrant destroy 销毁虚拟机
- vagrant ssh 连接虚拟机
- vagrant reload 重新加载vagarntfile文件
- vagrant suspend 暂时挂起虚拟机
- vagrant status 查看虚拟机运行状态
- vagrant box list box列表
四.个性化配置Vagrantfile
在进行 vagrant init 之后我们会发现对应的目录下有个Vagrantfile文件
直接上截图,就不一个一个解释了
五.导出自己的box
- $ cd ~/VirtualBox\ VMs/phponubuntu64
- $ vagrant package --output lamp5_5onubuntu64.box --base phponubuntu64
注意事项
问题一)使用 Apache/Nginx 时会出现诸如图片修改后但页面刷新仍然是旧文件的情况,是由于静态文件缓存造成的。需要对虚拟机里的 Apache/Nginx 配置文件进行修改:
#nginx配置
sendfile off
#apache 配置
EnableSendFile off
问题二:启动多个虚拟机提示错误“2222 端口 in use”
解决办法:(参考文章:Vagrant SSH errors with multiple VMs: Port 2222 in use)
- config.vm.network "forwarded_port", guest: 22, host: 2220 ,id: 'ssh'
问题三:如果遇到 php 代码也感觉被缓存起来了,请关闭apc,opcode(从php5.5及以后版本默认开启opcode)
问题四:config.vm.boot_timeout
- Timed out while waiting for the machine to boot. This means that
- Vagrant was unable to communicate with the guest machine within
- the configured ("config.vm.boot_timeout" value) time period.
- If you look above, you should be able to see the error(s) that
- Vagrant had when attempting to connect to the machine. These errors
- are usually good hints as to what may be wrong.
- If you're using a custom box, make sure that networking is properly
- working and you're able to connect to the machine. It is a common
- problem that networking isn't setup properly in these boxes.
- Verify that authentication configurations are also setup properly,
- as well.
- If the box appears to be booting properly, you may want to increase
- the timeout ("config.vm.boot_timeout") value.
类似如上的错误日志,本人解决方案是,设置=>网络=>高级=>选中 cable connected
问题五:GUI模式waiting for network configuration
- waiting for network configuration
- waiting an additional 60 seconds for network configuration
这个是网络配置有问题,直接进去虚拟机 编辑 /etc/network/interfaces 删除不需要的,
问题六:Warning: Authentication failure. Retrying...
那么把 https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub 公钥放到authorized_keys 文件中,重新vagrant reload
我个人操作步骤是
-
删除authorized_keys
-
vagrant reload
-
在上述链接的内容 放到 虚拟机 authorized_keys 中
-
vagrant reload