使用vagrant+virtualbox搭建跨平台开发环境

相关参数

  • 目录地址: (本机目录) D:\web => /www/ (对应虚拟机目录)html

准备工做

安装vagrant

https://www.vagrantup.com/downloads.htmllinux

更改vagrant配置文件的位置

若是C盘空间足够,此步可不操做git

将 C:\Users\user_name\.vagrant.d 移动到新的位置

新建环境变量VAGRANT_HOME,并指向新路径

安装virtualbox

https://www.virtualbox.org/wiki/Downloadsgithub

下载centos7的vagrant box到你想挂载的目录(好比 D:\web),用vagarant镜像下载会很是慢,推荐用下面的连接下载
下载地址:
cloud.centos.orgweb

这个box是纯净版的
其它镜像:
http://cloud.centos.org/centos/7/vagrant/x86_64/images/shell

更改virtaulbox虚拟机默认位置

  • 打开 VirtualBox 程序,点击管理/全局设定菜单项windows

  • 将常规栏里的默认虚拟电脑位置改成其余磁盘下的路径centos

  • 从新启动VirtualBox程序app

使用vagrant

vagrant plugin install vagrant-vbguest

vagrant  box  add  centos7.2  virtualbox.box
#命令解释:vagrant box add为载入镜像命令,centos7.2为载入后的镜像名称,virtualbox.box为box文件名

vagrant  init  centos7.2  #根据镜像初始化一个虚拟机

# 修改本机目录下的Vagrantfile文件 , 将config.vm.network "public_network"前的注释删除并保存

vagrant  up    #在本机目录下执行该命令,启动虚拟机

vagrant vbguest --auto-reboot

vagrant package # 打包系统生成package.box文件

默认帐号 vagrant 密码 vagrant ;
root帐号的默认密码vagrant;ssh

执行完vagrant up后,虚拟机就启动了

虚拟机能够直接用VirtualBox管理,也能够用Xshell链接虚拟机进行管理

也能够在该目录下执行 vagrant ssh 连入虚拟机(windows须要安装shell工具)

此时你已经有了一个centOS7.2的虚拟机,接下来就能够用它来搭建各类服务来运行你的项目。

以后每次只需在挂载目录下执行vagrant up就能够了

vagrantfile配置

建议在vagrantfile中配置

config.vm.network "private_network", type: "dhcp"

config.vbguest.auto_update = false

# do NOT download the iso file from a webserver
config.vbguest.no_remote = true

config.vm.synced_folder ".", "/vagrant",owner: "vagrant",group: "vagrant",mount_options:["dmode=777","fmode=777"]

其它配置详见
《官方文档》 《Vagrantfile 配置》

常见问题

共享目录没法挂载

报错信息

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:

/sbin/mount.vboxsf: mounting failed with the error: No such device

解决方案

方法1 安装vagrant插件

vagrant plugin install vagrant-vbguest
vagrant vbguest --auto-reboot
vagrant vbguest --status   #查看vbguest运行状态

方法2 更新vagrant和virtualbox

更新vagrant以后须要更新vagrant插件

vagrant plugin update

方法三 从新安装vboxadd

VBoxGuestAdditions的位置

for linux : /usr/share/virtualbox/VBoxGuestAdditions.iso
for Mac : /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso
for Windows : %PROGRAMFILES%/Oracle/VirtualBox/VBoxGuestAdditions.iso

使用vitualbox挂载上面的iso虚拟光驱

#进入虚拟机
vagrant ssh
#若是这个命令没法进入的话,就用virtualbox进入

#*号为版本号
cd /opt/VBoxGuestAdditions-*/init
sudo ./vboxadd setup # 若是不能setup就yum update

#退出虚拟机
exit

#重启虚拟机
vagrant reload
相关文章
相关标签/搜索