利用 虚拟机 搭建全平台的开发环境 (包括大部分语言和服务器管理面板)

我的需求

在 mac 和 linux 上 docker 是以虚拟 linux 网络模式运作的,这就导致我们没有办法直接访问容器,在电脑本身有环境的情况下不去影响当前环境搭建其他环境(其他语言环境可以按照这个思路来)

准备工作

为什么使用 vagrantup

我们通常在配置虚拟机的时候需要自己按照系统配置宿主机和虚拟机的网络等使用 vagrantup 能让我们快速并且简单的运行

注意

vagrantup 镜像下载非常慢,可以使用 外网加速 的方式

vagrantup 似乎已经上了CDN可以等待一会速度会上去的

终端可以使用类似命令 或者 全局

export http_proxy="http://127.0.0.1:20050"
export https_proxy="http://127.0.0.1:20050"

开始搭建

在安装好上述软件之后,打开命令行或终端

使用 vagrant up 查看是否安装成功,会输出版本号

新建环境目录

用处:存放需要挂载到虚拟机的文件夹或文件

/Users/cq/codeRunTime $: ls -al
drwxr-xr-x  4 chenquan staff  128 4 24 22:28 .vagrant
-rw-r--r--@ 1 chenquan staff 1203 4 26 14:29 Vagrantfile
drwxr-xr-x  5 chenquan staff  160 4 26 11:12 backup
drwxr-xr-x  9 chenquan staff  288 4 28 14:56 data
drwxr-xr-x  5 chenquan staff  160 4 26 01:28 wwwlogs
drwxr-xr-x  6 chenquan staff  192 4 28 15:01 wwwroot

注意我这里是使用了宝塔所以会出现 backup,data,wwwlogs,wwwroot 等目录

在当前目录初始化一个虚拟机

我是用的镜像是 centos/7 有其他需求的同志可以在 vagrantup search 寻找自己想要的

vagrant init centos/7 # 在当前目录初始化,目录下会多一个名为 Vagrantfile 的文件
vagrant up # 启动当前目录下的镜像

正常输出 vagrant init centos/7

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.

正常输出 vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'... # 这一步会下载镜像文件,由于我本地已经存在可以直接使用
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: Setting the name of the VM: ccc_default_1556587395244_89525
==> 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: 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: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!

配置固定 IP

上一步在目录下会生成 Vagrantfile 文件,我们打开它

# -*- 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 = "centos/7"  # 镜像名称

  # 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 # 转发端口暂时不需要用到,如果你不想工作在独立的IP上可以配置此项绑定到  127.0.0.1 上

  # 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" # 配置虚拟机的私有网络,这里就是关键处
  config.vm.network "private_network", ip: "192.101.101.101" # 记住这里设置的IP不要与当前网络环境有冲突!!

  # 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" # 挂载目录,这里看个人需求,如果可能经常变动你的虚拟机环境你可以只挂载你的代码目录上去
   config.vm.synced_folder "../data", "/vagrant_data" # 挂载当前系统的 ../data 文件夹 到 虚拟机的 /vagrant_data ,当然还有其他参数 owner:"www", group:"www" 指定用户组和用户 create:true  是否启用
   # 我的完整配置如下
   # config.vm.synced_folder "/Users/cq/codeRunTime/wwwroot", "/www/wwwroot" , create:true , owner:"www", group:"www"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:

  # 这里可以开始对虚拟的一些其他配置,比如 vb.gui 是否显示 主机的界面,memory 内存 ,cpus 这些大家都可以在vargent up的文档中找到
  # 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

在编辑好你的配置文件后使用 vagrant reload 进行重载配置并启动虚拟机

可能出现的问题

  1. Q: 镜像下载失败,无法初始化虚拟机

    A: 使用科学上网或者手动下载虚拟机的文件(路径在你执行 vargent up 下载的那个就是) 手动下载然后使用 vagrant box add 命令进行手动导入

  2. Q: 虚拟机IP无法访问

    A: 在重新配置 IP 在 virtualbox 或 vm 的管理软件中将网卡删除重新使用 vagrant reload 启动虚拟机

  3. Q: 一切正常后,我想泛解析测试域名到虚拟机上如 *.ddt.test => 192.101.101.101

    A: 使用 dnsmaq 或者其他的相关软件,当自己的dns解析改为 127.0.0.1 最后在 dnsmaq 的配置文件加入 address=/ddt.test/192.101.101.101
    我的 dnsmaq 配置文件

    resolv-file=/usr/local/etc/resolv.dnsmasq.conf # 配置前置dns用来解析 www.baidu.com 等网站
    strict-order # 按照上述文件的顺序一次解析,解析不到在用下方address配置
    listen-address=127.0.0.1 # 监听IP
    conf-dir=/usr/local/etc/dnsmasq.d # dnsmaq配置文件 这是自带的不需要更改!
    cache-size=10000 # 缓存大小
    address=/ddt.test/192.101.101.101 # 配置域名到IP
    address=/think.test/192.101.101.101
    address=/laravel.test/192.101.101.101
    address=/php.test/192.101.101.101
  4. Q: 每次都需要到目录去运行 vagrant up 等命令很麻烦,如何简化?

    A: 可用方法很多,Windows设置环境变量,类 linux 系统配置 bash_profile 这里贴出我的配置

    alias phper="cd /Users/cq/codeRunTime && vagrant"       # 配置phper 启动虚拟机在任意地方我可以使用 phper up 等来快速操作
    alias phperdns-re="sudo brew services restart dnsmasq" # 重启dns服务
    alias phperdns-conf="open /usr/local/etc/dnsmasq.conf" # 打开dns配置文件
    alias phperopen="export http_proxy='http://127.0.0.1:20050' && export https_proxy='http://127.0.0.1:20050'" #设置网络加速
未完待续...
本作品采用《CC 协议》,转载必须注明作者和本文链接
thanks.
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!