🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
环境: Docker Vagrant https://xueyuanjun.com/post/354.html Laragon https://xueyuanjun.com/post/9609 参考文章: [https://www.cnblogs.com/hafiz/p/9175484.html](https://www.cnblogs.com/hafiz/p/9175484.html) [https://segmentfault.com/a/1190000000264347](https://segmentfault.com/a/1190000000264347) https://blog.csdn.net/qq_38962739/article/details/103517520 安装和使用 [https://blog.csdn.net/woqianduo/article/details/81091154](https://blog.csdn.net/woqianduo/article/details/81091154) [TOC] ### 原理 vagrant介绍 vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。使用Oracle的开源virtualBox虚拟化系统,使用Chef创建自动化虚拟环境。同时,vagrant也支持其他类型的虚拟机系统,如:vmware、kvm、qemu,甚至是容器系统,如docker等,当然,用的比较多的还是virtralBox。 &nbsp; &nbsp; vagrant与virtualBox原理 这里我们简述一下vagrant与virtualBox的原理,virtualBox(本身也可以创建虚拟机,只是相对麻烦)会开放一个创建虚拟机的接口,Vagrant会利用这个接口创建虚拟机,并且通过Vagrant来管理,配置和自动安装虚拟机。 &nbsp; &nbsp; 使用Vagrant来创建虚拟机 前提,首先确保本机已经安装好两者。 新建一个空文件夹:mkdir centos7 ,并进入该文件夹:cd centos7/ 在该目录下初始化一个创建centos7的Vagrantfile文件,执行命令:vagrant init centos/7 创建centos7虚拟机,执行命令:vagrant up,如果本地有base box,会使用本地的,否则将会从网络中下载,此过程可能偏久,会生成名字为如下图所示的虚拟机: 接下来,我们将在virtualBox中看到刚刚创建的虚拟机: 注:在vagrant中box概念,是一个打包的单一文件,其中包含了一个完整系统的虚拟机相关数据。 vagrant的基本使用 执行命令:vagrant ssh ,就会通过ssh连接至刚刚我们所创建的虚拟机 如果需要退出当前连接,执行命令:exit,将会回到本地目录 查看虚拟机状态,在本地目录里执行命令:vagrant status 停掉虚拟机,执行命令:vagrant halt 开启虚拟机,执行命令:vagrant up 删除虚拟机:vagrant destroy 查看目前已有的box:vagrant box list 新增加一个box:vagrant box add 删除指定box:vagrant box remove 重启虚拟机:vagrant reload &nbsp; &nbsp; ### 一、准备工作 准备以下软件: 1.virtualbox [https://www.virtualbox.org/wiki/Downloads](https://www.virtualbox.org/wiki/Downloads) 2.vagrant [https://www.vagrantup.com/downloads.html](https://www.vagrantup.com/downloads.html) 3.git【包含gitbash】 4.已开启vt-x【硬件虚拟化加速】 首先确认自己的CPU支持虚拟化,可以拿CPU型号去对应的厂商官网去查 重启开机按F2或者Del进入BIOS界面。 `如果是华硕主板EZmode模式,选择=》高级=》cpu选项,拉到最底部,选择开启intel虚拟化` `如果是一般的BIOS 参见:https://blog.csdn.net/sunrise_zhu/article/details/78889793 重启后进入startup menu,选择Computer Setup(F10) 菜单栏中选择Security —> System Security Virtualization technology(VTx) 设置为enable` 5.准备好要安装的box 官网下载链接:https://app.vagrantup.com/laravel/boxes/homestead/versions/8.2.1/providers/virtualbox.box 网盘下载链接:https://pan.baidu.com/s/1oj9g9nAORLswYSvKrhOIaA 密码 kvf4 &nbsp; &nbsp; ### 二、安装各种软件和插件 安装 vagrant 安装 vagrant的插件来支持nfs文件共享 vagrant plugin install vagrant-winnfsd --plugin-clean-sources --plugin-source https://gems.ruby-china.com/ &nbsp; &nbsp; ### 三、安装box【这里以官方box里的centos7为例】 找一个盘,这里是F盘,创建目录vagrantBox/{box名称} 这里是vagrantBox/centos7,即F:\vagrantBox\centos7 在centos7目录下,右击gitbash,打开bash命令行界面,进行box初始化 ``` $ vagrant init centos7 #执行完会生成一个vagrant启动虚拟机的配置文件 Vagrantfile ``` 然后将box导入vagrant ``` $ vagrant box add centos7 {已下载好的centos7的box文件路径,可以放到F:\vagrantBox\centos7目录下,则只需要填文件名} $ vagrant box list centos7 (virtualbox, 0) $ vagrant up #这里会出现无法挂载共享目录的提示,可以通过安装 vagrant插件解决 vagrant plugin install vagrant-vbguest --plugin-clean-sources --plugin-source https://gems.ruby-china.com/ # 1.成功后再执行启动命令 vagrant up # 2.有可能用启动命令进行下载会比较慢【不知为何】,可以先vagrant halt 关闭box,打开Oracle VM VirtualBox,手动双击打开box, #使用默认的帐号密码 user:root password:vagrant 进行登录,并使用yum update 进行更新 再vagrant halt 并重新vagrant up,最终就快了很多 #这样会下载安装一些软件依赖, 最终Copy iso file E:\virtualBox\VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso并完成安装和挂载 ``` 以下为我的启动配置文件项 ``` # -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://vagrantcloud.com/search. config.vm.box = "centos7" #ssh账户和密码,默认都为vagrant,注意root权限密码也是vagrant config.ssh.username = "vagrant" config.ssh.password = "vagrant" #开启下面的配置 #网络三种模式之一:较为常用是端口映射,就是将虚拟机中的端口映射到宿主机对应的端口直接使用 #guest: 80 表示虚拟机中的80端口, host: 8080 表示映射到宿主机的8080端口。 config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true" config.vm.network "forwarded_port", guest: 80, host: 80 config.vm.network "forwarded_port", guest: 15672, host: 15672 config.vm.network "forwarded_port", guest: 22, host: 2221 #配置共享目录 #原理是window共享目录的内容,都会时时同步到虚拟机上 #"E:/phpstudy_pro/WWW":代表window路径 ,SVN代码checkout这里的 #"/home/www":代表linux路径,会将window 中项目同步到这里 config.vm.synced_folder "E:/phpstudy_pro/WWW", "/home/www" config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine vb.gui = true # Customize the amount of memory on the VM: vb.memory = "4096" vb.cpus = 4 end # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. # NOTE: This will enable public access to the opened port # config.vm.network "forwarded_port", guest: 80, host: 8080 # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # Create a private network, which allows host-only access to the machine # using a specific IP. # config.vm.network "private_network", ip: "192.168.33.10" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. # config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # # config.vm.provider "virtualbox" do |vb| # # Display the VirtualBox GUI when booting the machine # vb.gui = true # # # Customize the amount of memory on the VM: # vb.memory = "1024" # end # # View the documentation for the provider you are using for more # information on available options. # Enable provisioning with a shell script. Additional provisioners such as # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the # documentation for more information about their specific syntax and use. # config.vm.provision "shell", inline: <<-SHELL # apt-get update # apt-get install -y apache2 # SHELL end ```