商城项目--1.Vagrant相关

发布于:2022-11-09 ⋅ 阅读:(443) ⋅ 点赞:(0)

折腾了一段时间的源码之后,最近特别想从头到尾做一个完整的项目。也是找了网上一个免费的项目视频,从头到尾拍着敲一遍。然后我会把每天做的部分,整理一下发到我的博客就当作我的笔记了(血的教训啊,原来做过这个项目,但是做了一半,电脑坏了,做的笔记以及项目都丢了,悔死我啦。。。。)

这一部分的博客,暂且作为一个小专栏吧。专栏名称《商城项目折腾日记》

今天开始记录第一部分,安装Vagrant工具以及相应的镜像

我们平时安装虚拟机,都会去安装一个虚拟机软件,比如virtualBox啥的,然后找一个自己需要的操作系统的镜像,然后固定的步骤进行安装,其实有一个比较简单的方式。当前有一款ruby语言开发的工具Vagrant,该工具下载完成后,可以通过几个命令完成镜像自动下载安装,然后安装到virtualBox中。

1. 工具下载

vagrant工具下载地址:Install | Vagrant | HashiCorp Developer

vagrant下载镜像的地址:Discover Vagrant Boxes - Vagrant Cloud

可以通过一下几个命令完成通过vagrant下载镜像:

(1) 初始化vagrantfile文件,其中centos/7 就是镜像中已经封装好的一个类型

vagrant init centos/7

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

  # 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
  # Ansible, Chef, Docker, Puppet and Salt 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

(2)通过一下命令完成镜像的下载以及安装

vagrant up

下载成功的页面如下:

GodShadow@276908823qqcomdeMacBook-Air mall % vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
    default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v2004.01) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box
==> default: Box download is resuming from prior download progress
Download redirected to host: cloud.centos.org
    default: Calculating and comparing box checksum...
==> default: Successfully added box 'centos/7' (v2004.01) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '2004.01' is up to date...
==> default: Setting the name of the VM: mall_default_1667840926534_98848
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (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:2222
    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!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Rsyncing folder: /Users/GodShadow/vms/mall/ => /vagrant

下载完成后,可以通过命令链接这个虚拟机。

vagrant ssh

就可以通过这个命令窗口操作这个虚拟机

GodShadow@276908823qqcomdeMacBook-Air mall % vagrant ssh
[vagrant@bogon ~]$ whoami
vagrant
[vagrant@bogon ~]$ ls
[vagrant@bogon ~]$ ls /
bin   dev  home  lib64  mnt  proc  run   srv       sys  usr      var
boot  etc  lib   media  opt  root  sbin  swapfile  tmp  vagrant
[vagrant@bogon ~]$ exit

后续,即使虚拟机关闭后,也可以使用vagrant up启动这个虚拟机

2.网络配置

通过vagrant安装完成后,操作系统的网络配置如下属于端口转发模式

也就是将virtualbox中的操作系统的端口映射到外部系统中的某一个接口,如上图,mysql的3306映射到外部系统的3333端口。

  

可以通过virtualbox中进行端口转发配置,实现端口转发功能,但是这种形式比较繁琐。 

我们可以通过修改Vagrantfile文件,改成网络方式为私有网络,具体配置如下:

config.vm.network "private_network", ip: "192.168.33.10"

对于图中windows的机器,可以直接查看virtualbox host-only的地址,然后配置项中填写属于同一个网段的ip即可。比如此处是192.168.56.1,上面的配置应该如下:

config.vm.network "private_network", ip: "192.168.56.10"

 然后使用命令 vargant reload进行虚拟机的重启,重新读取Vagrantfile进行重启。重启完成后,虚拟机就可以与windows真实机器互相之间可以ping通。

不知道其他同学的是啥情况,反正我的mac本地没有出现这个host-only的地址,我是通过virtualbox中为mac增加了一个host-only地址,vboxnet0地址。

在这里插入图片描述

配置完成后,选择管理下面的主机网络管理器

 配置网卡信息如下:

然后,查询mac地址如下:

vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
	ether 0a:00:27:00:00:00 
	inet 192.168.56.1 netmask 0xffffff00 broadcast 192.168.56.255

然后配置Vagrantfile如下:

config.vm.network "private_network", ip: "192.168.56.100"

配置完成后,执行vagrant reload

查看虚拟机ip

[vagrant@localhost ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:4d:77:d3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 85942sec preferred_lft 85942sec
    inet6 fe80::5054:ff:fe4d:77d3/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:80:cb:a5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.100/24 brd 192.168.56.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe80:cba5/64 scope link 
       valid_lft forever preferred_lft forever

可以看到有一个IP 192.168.56.100,可以ping通mac的192.168.218.237,

[vagrant@localhost ~]$ ping 192.168.218.237
PING 192.168.218.237 (192.168.218.237) 56(84) bytes of data.
64 bytes from 192.168.218.237: icmp_seq=1 ttl=63 time=0.425 ms
64 bytes from 192.168.218.237: icmp_seq=2 ttl=63 time=0.438 ms
64 bytes from 192.168.218.237: icmp_seq=3 ttl=63 time=0.533 ms
64 bytes from 192.168.218.237: icmp_seq=4 ttl=63 time=0.561 ms
64 bytes from 192.168.218.237: icmp_seq=5 ttl=63 time=0.554 ms
64 bytes from 192.168.218.237: icmp_seq=6 ttl=63 time=0.978 ms

当然虚拟机也可以正常访问外网。

[vagrant@localhost ~]$ curl http://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

至此,vagrant的配置全部完成

       我是程序员神域风云再起,欢迎大家关注我的公众号“风云编程录”,感谢大家🙏。

本文含有隐藏内容,请 开通VIP 后查看