【问题标题】:VirtualBox: How to access virtual host on VM from host?VirtualBox:如何从主机访问 VM 上的虚拟主机?
【发布时间】:2016-07-23 02:45:58
【问题描述】:

如何从主机访问在虚拟机上创建的 Apache 虚拟域。

VirtualBox 上的虚拟机设置:

  • VirtualBox 上的 VM ubuntu/precise32 使用来自 How to setup a LAMP development environment in Vagrant by Sanchit Jain Rasiya 的 vagrantfile 创建
  • 已安装 Apache/2.2.22
  • 已安装 Lynx 浏览器
  • 虚拟主机 /etc/apache2/sites-available/example.com

    ServerAdmin webmaster@example.com 服务器名称 example.com DocumentRoot /home/vagrant/www/example.com 允许覆盖所有 命令允许,拒绝 允许所有人 错误日志“/home/vagrant/www/example.com/logs/example.com-error.log” CustomLog "/home/vagrant/www/example.com/logs/example.com-access.log" 合并

从主机上的浏览器我可以访问虚拟机的localhosthttp://192.168.205.10/index.html 如何访问 VM 的 example.com?

在主持人的/etc/hosts我添加了example.com这一行

127.0.0.1   localhost
127.0.0.2   tmp.loc
127.0.0.3   temp.loc
192.168.205.10  example.com

当我尝试http://example.com/mj.html 时,我从 Apache/2.2.22 (Ubuntu) 服务器的 example.com 端口 80 获得了 404 Not Found 页面

在虚拟机上我可以使用 Lynx 访问页面

lynx http://example.com/mj.html

这在/home/vagrant/www/example.com/logs/example.com-access.log 文件中记为

127.0.0.1 - - [03/Apr/2016:15:53:49 +0000] "GET /mj.html HTTP/1.0" 200 344 "-" "Lynx/2.8.8dev.9 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/2.12.14"

但是当我尝试从主机访问 example.com 时,access.log 和 /home/vagrant/www/example.com/logs/example.com-error.log 中都没有日志

主机的Server文件/opt/lampp/logs/access_log和/opt/lampp/logs/error_log中没有日志

【问题讨论】:

    标签: apache2 virtualbox virtualhost


    【解决方案1】:

    在 Ubuntu /etc/hosts 上:

    127.0.0.1       localhost
    127.0.2.2       example.com
    

    在 Windows (VBox) 上 C:\Windows\System32\drivers\etc\hosts:

    10.0.2.2 localhost
    10.0.2.2 example.com
    

    然后只需重新启动您的 VBox Windows 并在浏览器中输入(在 Windows 上): http://example.com/

    【讨论】:

      【解决方案2】:

      我有同样的问题,我一直在尝试从我的 virtualbox vm 访问我的 drupal 项目,但它无法正常工作。我在 Internet 上找到的一种解决方案是在 VM 中的 hosts 文件中使用 outer 关键字。

      以下对我来说很好用:

      1. 在您的主机(假设是 ubuntu)中输入:ifconfig,然后获取 wlan0的inet地址(例如:192.168.1.15)
      2. 将此地址添加到您的virtualobox机器中的主机文件并使用outer关键字

        [192.168.1.15 outer]
        

      现在您可以通过输入 wlan0 的地址(例如:192.168.1.15)从虚拟机浏览器访问您的本地主机。

      • 如果您需要访问主机上的特定端口,请将端口号添加到 地址示例 192.168.1.15:8001

      【讨论】:

        猜你喜欢
        • 2020-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-05
        • 2016-10-16
        • 1970-01-01
        • 1970-01-01
        • 2011-05-20
        相关资源
        最近更新 更多