如何在 Ubuntu 虚拟机上使用 VMware 和 Nginx 托管网站,并在网站无法加载时解决问题(分步指南)

发布: (2026年2月8日 GMT+8 14:08)
5 分钟阅读
原文: Dev.to

Source: Dev.to

技术栈

  • 宿主操作系统: Windows 10/11
  • 客体虚拟机: Ubuntu Linux
  • 虚拟化平台: VMware Workstation
  • Web 服务器: Nginx
  • 远程访问: SSH(通过 PowerShell)

为什么我安装了 Nginx

我安装 Nginx 的目的:

  • 练习在 Linux 服务器上托管 Web 服务
  • 测试 Windows(宿主)Ubuntu VM(客体) 之间的网络
  • 学习真实场景的故障排除:防火墙、端口、虚拟机网络
  • 掌握可直接迁移到 AWS EC2 和 DevOps 工作的技能

设置概览

  • 宿主操作系统: Windows
  • 虚拟机: 在 VMware Workstation 上运行的 Ubuntu
  • 访问方式: 从 PowerShell 使用 SSH
  • 目标: 在 Windows 浏览器中访问虚拟机上的网页

步骤详解

1️⃣ 通过 SSH 连接到 Ubuntu VM(在 PowerShell 中)

ssh alok@

2️⃣ 在 Ubuntu 上安装并启动 Nginx

sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

验证 Nginx 是否在运行:

sudo systemctl status nginx

在虚拟机内部本地测试:

curl http://localhost

✅ Nginx 在虚拟机内部工作正常。

3️⃣ 查找虚拟机的 IP 地址

hostname -I

示例输出: 192.168.80.129

4️⃣ 在 Windows 浏览器中尝试访问

在 Chrome 中打开 http://192.168.80.129

问题: 页面一直加载 → ERR_CONNECTION_TIMED_OUT

5️⃣ 检查 Ubuntu 防火墙(UFW)

sudo ufw status

输出显示:

  • 已允许端口 22(SSH)
  • 已允许端口 3000、 5000、 8000
  • 端口 80 未被允许

解决办法:允许 HTTP(端口 80)

sudo ufw allow 80/tcp
sudo ufw reload

浏览器仍然超时 → 防火墙并不是唯一原因。

6️⃣ 确认 Nginx 本身没有问题

sudo systemctl status nginx
curl http://localhost

结果:Nginx 正常运行,curl 成功。
结论: 应用层没有问题,问题出在 Windows ↔ VM 的网络之间。

7️⃣ 验证 VMware 网络模式

  • VMware → VM SettingsNetwork Adapter
  • 模式: NAT(已选中)

NAT 配置没有错误,但 NAT 服务可能出现故障。

8️⃣ 修复 VMware NAT 网络(宿主侧)

在 Windows 上:

  1. Win + R,运行 services.msc
  2. 重启以下服务:
    • VMware NAT Service
    • VMware DHCP Service

这恢复了 Windows 与虚拟机之间的网络通路。

9️⃣ 再次在 Windows 上测试

在 Chrome 中打开 http://192.168.80.129

✅ Nginx 页面成功加载 – 问题已解决! 🎉

根本原因汇总

层级修复前状态修复后状态
应用(Nginx)✅ 正常✅ 正常
本地访问(curl✅ 正常✅ 正常
Ubuntu 防火墙(UFW)❌ 80 端口被阻止✅ 已放行 80 端口
VMware NAT 服务(宿主)❌ 损坏✅ 已重启
浏览器访问❌ 超时✅ 成功

使用的故障排除命令

在 Ubuntu VM 上

sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
curl http://localhost
hostname -I
sudo ufw status
sudo ufw allow 80/tcp
sudo ufw reload

在 Windows 上

ssh alok@
ping 

Windows 界面: services.msc → 重启 VMware NAT ServiceVMware DHCP Service

这次实践让我学到的(真实世界技能)

  • Linux 上 Web 服务器的运行方式
  • 端口和防火墙如何影响可访问性
  • NAT 虚拟机网络的工作原理
  • 如何按层级(应用 → 防火墙 → 网络 → 宿主服务)定位问题

这种系统化方法同样适用于:

  • 无法加载的 EC2 实例
  • 无法访问的 Docker 容器
  • 生产环境中超时的服务
0 浏览
Back to Blog

相关文章

阅读更多 »