记录一下如何在Windwos 11 下利用 vagrant 运行Debian 11,因为之前的电脑环境是macOS 少说也用了七八年的时间,猛然间换到Windows 还有些不适应,这里记录一下在适应过程中遇到的问题。
换到Windwos 后需要配置网站开发环境,之前都是本机用Docker 来运行,今天尝试在Windows 11 上虚拟机配合 Vagrant 运行Debian 11 作为网站环境。
开始之前的准备工作,需要下载一些必要的文件:
VirtualBox: https://download.virtualbox.org/virtualbox/7.0.6/VirtualBox-7.0.6-155176-Win.exe
Vagrant: https://releases.hashicorp.com/vagrant/2.3.4/vagrant_2.3.4_windows_amd64.msi
debian/bullseye64: https://app.vagrantup.com/debian/boxes/bullseye64/versions/11.20221219.1/providers/virtualbox.box
其他镜像可以在下面找到:
Discover Vagrant Boxes – Vagrant Cloud (vagrantup.com)
创建一个文件夹用于保存虚拟机配置文件等,这里我在E盘下创建了一个Debian 目录,将virualbox.box 下载并保存为Debian.box。
在终端中进入到这个目录下,输入一下命令创建配置文件:
vagrant box add Debian.box
vagrant box list
vagrant init debian/bullseye64
这回在当前目录生成一个Vagrantfile配置文件,需要进行修改。
修改后的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 = "debian/bullseye64"
# 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"
config.vm.synced_folder "./wwwroot", "/data/wwwroot", create: true, owner: "www", group: "www", mount_options: ["dmode=755", "fmode=644"]
config.vm.synced_folder "./vhost", "/usr/local/nginx/conf/vhost"
config.vm.synced_folder "./ssl", "/usr/local/nginx/conf/ssl"
# 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
config.vm.provider "virtualbox" do |vb|
vb.memory = "4096"
vb.cpus = 4
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
echo "deb https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free" > /etc/apt/sources.list
echo "deb-src https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free" >> /etc/apt/sources.list
echo "deb https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free" >> /etc/apt/sources.list
echo "deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free" >> /etc/apt/sources.list
echo "deb https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free" >> /etc/apt/sources.list
echo "deb-src https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free" >> /etc/apt/sources.list
SHELL
end
启动虚拟机:
vagrant up
等待启动完毕后进入虚拟机
vagrant ssh
登录进去后修改root 密码之类的。
顺手记录一下macOS下的配置,以后可以参考。
Vagrant.configure("2") do |config|
#config.vm.box = "generic/debian11"
config.vm.box = "debian/bullseye64"
config.vm.hostname = "debian"
# config.vm.network "private_network", ip: "192.168.1.234"
config.vm.network "public_network", ip: "192.168.1.222"
config.vm.synced_folder "data/wwwroot/", "/data/wwwroot/", mount_options: ["uid=1002,gid=1002,dmode=775,fmode=664"]
#config.vm.synced_folder "data/wwwroot/", "/data/wwwroot/", owner: "www", group: "www", mount_options: ["dmode=775, fmode=664"]
config.vm.synced_folder "data/vhost/", "/usr/local/nginx/conf/vhost/"
config.vm.synced_folder "data/ssl", "/usr/local/nginx/conf/ssl/"
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = "2"
end
config.vm.provision "shell", path: "s.sh"
end