您的位置:首页 > 编程语言 > Python开发

快速打造跨平台开发环境 vagrant + virtualbox + box

2016-01-09 21:00 609 查看
工欲善其事必先利其器,开发环境 和 开发工具 就是 我们开发人员的剑,所以我们需要一个快并且好用的剑

关于本篇博文有视频讲解的:vagrant打造跨平台可移动的开发环境

刚开始做开发的时候的都是把开发环境 配置在 自己的电脑上,随着后面我们接触的东西越来越多,慢慢的电脑上都是各种环境了,php,java,python,nodejs等等,非常麻烦,并且经常由于某种原因就把电脑重装了,ORZ,所以环境都要重来。所以打造一个属于自己并且可以移动的环境是非常重要的。
vagrant就是一款构建虚拟开发环境的工具,支持window,linux,mac,总有一款适合你。并且vagrant 可以把配置好的环境打包成一个box,分享给其他人直接使用,非常方便



系统要求:(本人mac系统)
机器支持虚拟化,有时候需要调整bios开启cpu虚拟化。
第一步:安装virtualbox (本人virtualbox版本 4.3.30)
具体安装包请直接从官网下载:https://www.virtualbox.org/wiki/Downloads
第二步: 安装 vagrant (本人vagrant 版本 Vagrant 1.6.3)
$ vagrant -v
Vagrant 1.6.3
具体安装包请直接从官网下载:
第三步:vagrant 基本命令
官网文档:https://www.vagrantup.com/docs/getting-started/index.html

vagrant init        初始化vagrantfile
vagrant add box     添加box,自动帮你生成vagrantfile
vagrant halt        关闭虚拟机
vagrant destroy     销毁虚拟机
vagrant ssh         连接虚拟机
vagrant reload      重新加载vagarntfile文件
vagrant suspend     暂时挂起虚拟机
vagrant status      查看虚拟机运行状态
第四步:示例演示
4.1 下载box
下面是ubuntu的两个官方地址:
Ubuntu precise 32 VirtualBox http://files.vagrantup.com/precise32.box
Ubuntu precise 64 VirtualBox http://files.vagrantup.com/precise64.box
如果你要其他系统的镜像,可以来这里下载:http://www.vagrantbox.es/
$ mkdir -p  ~/vagrant/boxes
$ cd ~/vagrant/boxes && wget
$ vagrant box add ubuntu64 precise64.box //添加box
4.2 新建一个lnmp(linux + nginx + mysql + php-fpm)虚拟机
$ cd ~/vagrant && mkdir lnmp
$ cd lnmp
$ vagrant init ubuntu64 //初始化
$ vagrant up //启动虚拟机lnmp
$ vagrant ssh //进入虚拟机lnmp
这个虚拟机就新建好了,是不是很快!!熟悉linux的就可以修改源,安装软件了
$ sudo apt-get install  keychain nginx mysql-server mysql-client  php5-cli php5-fpm php5-gd php5-mysql
具体过程还可以参考我的博文:ubuntu12.04 安装 php5.4/php5.5

第五步:个性化配置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
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key


我个人操作步骤是
删除authorized_keys

vagrant reload

在上述链接的内容 放到 虚拟机 authorized_keys 中

vagrant reload

问题七:mount: unknown filesystem type 'vboxsf' centos ubuntu 处理方案
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant

The error output from the command was:

mount: unknown filesystem type 'vboxsf'


可能大家在使用vagrant的时候经常遇到以上提示,这个时候只是共享目录无法使用,虚拟机已经在运行了

下面是我处理的方法,贴出来与大家分享;

ubuntu系统处理方案
sudo apt-get update
sudo apt-get install virtualbox-guest-utils
以上两条代码,ubuntu系统我就处理好了。重启虚拟机

原文地址:快速打造跨平台开发环境 vagrant + virtualbox + box
标签:virtualbox 打造 快速 虚拟机 安装 box sendfile off 2222 制作box vagrant timeout waiting network 跨平台

智能推荐

开启 NFS 文件系统提升 Vagrant 共享目录的性能
virtualbox + ubuntu虚拟机 目录共享
Python 虚拟环境Virtualenv
MySQL:动态开启慢查询日志(Slow Query Log)
Virtualbox 虚拟机支持硬件摄像头
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 开发 python