2024-01-04
服务器
vagrant、windows
894

一、安装软件

1.官网下载
virtulbox 官网: https://www.virtualbox.org/wiki/Downloads
vagrant 官网: https://www.vagrantup.com/downloads.html
vagrant box 官网: https://app.vagrantup.com/boxes/search
2.安装 virtualbox
如果是 windows11 家庭中文版,先打开 Hyper V
# 创建文件并写入以下内容,选中文件右键选择“以管理员身份运行文件”,重启计算机
vim hyper.bat

pushd "%~dp0"
dir /b %SystemRoot%\servicing\Packages\*Hyper-V*.mum >hyper-v.txt
for /f %%i in ('findstr /i . hyper-v.txt 2^>nul') do dism /online /norestart /add-package:"%SystemRoot%\servicing\Packages\%%i"
del hyper-v.txt
Dism /online /enable-feature /featurename:Microsoft-Hyper-V -All /LimitAccess /ALL
pause
3.安装命令
# vagrant 软件安装成功后,可查看版本信息(不用手动添加环境变量)
vagrant -v 

# win+R cmd (默认目录 C:\Users\jdzor),操作如下命令
vagrant box add centos7 D:/software-develop/vagrant/centos7.box # 添加 box 命令
vagrant init centos7 # 初始化 box

vagrant up          # 启动本地环境
vagrant halt        # 关闭本地环境
vagrant ssh         # 通过 ssh 登录本地环境所在虚拟机

vagrant suspend     # 暂停本地环境
vagrant resume      # 恢复本地环境
vagrant reload      # 修改了 Vagrantfile 后,使之生效(相当于先 halt,再 up)
vagrant destroy     # 彻底移除本地环境

vagrant box list    # 显示当前已经添加的box列表
vagrant box remove  # 删除相应的 box
vagrant status      # 获取当前虚拟机的状态

二、软件设置

1.修改 C:\Users\jdzor\Vagrantfile 文件
# 在文件末尾,end 前追加配置

# 公有 ip(桥接模式:局域网中的一台独立的机器,可以被其他机器访问)
# 首次连接时不填写 ip, netmask 和 gateway
# 虚拟机 ifconfig 命令查看 eth1 分配的 ip
# 宿主机 ipconfig 命令查看 “无线局域网适配器 WLAN” 分配的子网掩码、默认网关
# config.vm.network "public_network", ip: "172.19.9.126", netmask: "255.255.252.0", gateway: "172.19.11.254" 

# 私有 ip(NAT 模式:只有主机可以访问虚拟机,其他机器无法访问) 
config.vm.network "private_network", ip: "192.168.56.30"

# 共享文件夹 windows 目录,vagrant 目录
config.vm.synced_folder "E:/php", "/www/web"

# 虚拟机配置
config.vm.provider "virtualbox" do |vb|
  # 设置内存,单位MB(占用宿主机内存,最大设置2G即可)
  vb.memory = "2048"

  # 设置CPU个数(最大设置2C即可)
  vb.cpus = 2

  # vagrant up启动时,是否自动打开virtual box的窗口,缺省为false
  vb.gui = true

  # 解决启动超时问题
  vb.customize ["modifyvm", :id, "--nictype1", "82540EM"]
  vb.customize ["modifyvm", :id, "--nictype2", "82540EM"]
  vb.customize ["modifyvm", :id, "--rtcuseutc", "on"]
end

# shell 脚本
config.vm.provision "shell", inline: <<-SHELL
  # 切换为 root 用户运行
  sudo su
SHELL
2.修改 sshd 配置
vagrant ssh

sudo su
vi /etc/ssh/sshd_config

# 修改以下参数
PermitRootLogin yes # 允许 root 身份登录
PasswordAuthentication yes # 允许使用密码登录

# 重启 sshd 服务
systemctl restart sshd
3.安装软件
yum install -y net-tools tree vim autoconf
timedatectl set-timezone Asia/Shanghai # 设置时区
4.xshell 设置
# 执行命令:vagrant up,找到此行:default: SSH address: 127.0.0.1:2222
ip:127.0.0.1
port: 2222   

# 账号:vagrant 
# 密码:vagrant
5.检查 SElinux
# 如果修改php, nginx, redis, mysql 等配置文件后,启动失败
cat /var/log/messages # 查看日志

sestatus # 检查 SElinux 是否关闭
vim /etc/selinux/config
# 修改文件中 SELINUX="" 为 SELINUX=disabled,然后重启 linux
6.centos7 yum 安装失败 (HTTP Error 404 - Not Found) 的解决方法
# 替换Centos-Base.repo前可以备份一下
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

# 下载阿里云的yum源并替换
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

# 生成新的yum缓存
yum clean all
yum makecache

三、使用流程

1.win + R 执行命令:
vargrant up
2.打开 xshell 软件
sudo su # root 登录,不用输入密码

四、报错处理

1.Your VM has become "inaccessible." Unfortunately, this is a critical error with VirtualBox that Vagrant can not cleanly recover from. Please open VirtualBox and clear out your inaccessible virtual machines or find a way to fixthem.
打开文件:C:\Users\jdzor\VirtualBox VMs\jdzor_default_1652512563126_40181

有个文件的后缀为 .vbox-tmp
把该文件后缀修改为 .vbox,去掉-tmp
2.Virtual-box 无法启动 VM VERR_INTNET_FLT_IF_NOT_FOUND
C:\Users\jdzor>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "13e041cb-cc0a-45dd-9934-de202ec4fbc3", "--type", "headless"]

Stderr: VBoxManage.exe: error: Failed to open/create the internal network 'HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter' (VERR_INTNET_FLT_IF_NOT_FOUND).
VBoxManage.exe: error: Failed to attach the network LUN (VERR_INTNET_FLT_IF_NOT_FOUND)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component ConsoleWrap, interface IConsole

解决方法:
打开计算机控制面板网络适配器;
选择为该虚拟机创建的适配器,如:VirtualBox Host-Only Ethernet Adapter;
只需禁用一次,然后再次启用即可。
3.Timed out while waiting for the machine to boot. This means that Vagrant was unable to communicate with the guest machine withinthe configured ("config.vm.boot_timeout" value) time period.
# 关机
vagrant halt

# windows cmd 命令行网络重启
netsh advfirewall reset
netsh int ip reset
netsh int ipv6 reset
netsh winsock reset

# 重新启动
vagrant up --provision
4.default: SSH auth method: private key 卡住了...
# Restart the VM's Network from GUI: If you can log in via the GUI but SSH still fails, the network might be misconfigured. Restart it from inside the VM :

vagrant
vagrant
sudo su
sudo /etc/init.d/network restart
5.Vagrant的机器索引文件已经损坏
The machine index which stores all required information about
running Vagrant environments has become corrupt. This is usually
caused by external tampering of the Vagrant data folder.

Vagrant cannot manage any Vagrant environments if the index is
corrupt. Please attempt to manually correct it. If you are unable
to manually correct it, then remove the data file at the path below.
This will leave all existing Vagrant environments "orphaned" and
they'll have to be destroyed manually.

Path: C:/Users/jdzor/.vagrant.d/data/machine-index/index

删除该目录中文件:C:\Users\jdzor\.vagrant.d\data\machine-index