【问题标题】:Trouble with Vagrant - "404 - Not Found"Vagrant 的问题 - “404 - 未找到”
【发布时间】:2015-09-06 18:07:26
【问题描述】:

我正在尝试使用 Vagrant 制作 LAMP 盒子。有人告诉我,它使用起来非常简单。我对网络和虚拟机完全陌生,对 Linux/Ubuntu 的经验很少。我目前已经尝试按照官方文档页面上的教程进行操作:http://docs.vagrantup.com/v2/getting-started/networking.html

我已阅读文档中的网络文章,但似乎无法正常工作。

现在的问题是,由于我对网络和基于 linux 的操作系统缺乏经验,我不知道从哪里开始解决问题。我会尽力提供尽可能多的信息。

我正在运行最新版本的 Vagrant 和最新版本的带有 Windows 8.1 的 Virtualbox。

根据教程,我当前的 Vagrantfile 如下所示:

Vagrant.configure(2) do |config|
  config.vm.box = "hashicorp/precise32"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, host: 4567, guest: 80
end

我的 bootstrap.sh 文件如下所示:

#!/usr/bin/env bash

apt-get update
apt-get install -y apache2
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -f /vagrant /var/www
fi

当我转到http://127.0.0.1:4567 时,它显示了一个包含此消息的错误页面:

Not Found

The requested URL / was not found on this server.
===================================================
Apache/2.2.22 (Ubuntu) Server at 127.0.0.1 Port 4567

除非有解释,否则我宁愿不编辑任何配置文件,因为我认为这是一种解决方法。但无论如何,我们将不胜感激任何帮助。如果我需要打开一个端口,那么我该如何考虑使用 XAMPP。

【问题讨论】:

    标签: linux vagrant virtualbox lamp portforwarding


    【解决方案1】:

    您可以从虚拟机内部访问您的网络服务器吗?

    例如,试试curl localhost:80

    如果没有安装 curl,请在 Ubuntu 上使用 sudo apt-get install curl 并重试。

    另外,您检查过您的 apache 虚拟主机吗? /etc/apache2/sites-available 中是否有 000-default 文件?

    【讨论】:

    • 我实际上从主机内部得到了完全相同的未找到消息。没有 000-default 但有 2 个其他默认文件。
    • 可以粘贴ls -ls /etc/apache2/sites-enabled的结果吗?
    • 它现在实际上正在工作。谢谢您的帮助。出于好奇,如果它没有出现,你会假设什么?
    • 我在想你的虚拟主机文件有问题。快乐编码!
    【解决方案2】:

    bootstrap.sh有两个问题

    1. 您需要启动 Web 服务。也可以vagrant ssh手动启动
    2. 您需要制作软链接,而不是硬链接。

    所以脚本会更新为

    $ cat bootstrap.sh
    #!/usr/bin/env bash
    
    apt-get update
    apt-get install -y apache2
    if ! [ -L /var/www ]; then
      rm -rf /var/www
      ln -s /vagrant /var/www
    fi
    
    service apache2 start
    

    【讨论】:

    • 这似乎不起作用。但是什么是软链接和硬链接呢?
    • 如果你不知道什么是软链接和硬链接,那就看这篇askubuntu.com/questions/108771/…
    • 并且修复确实有效,您需要在做出此结论之前检查您的环境。
    • 哦,你是对的!有效。我只是没有正确复制 bootstrap.sh 文件。我真的很困惑为什么我必须以这种方式修改 Vagrantfile 才能工作。理论上,根据官方教程,这应该“开箱即用”。他们给了我损坏的代码吗?我的平台是什么要求我更改这些设置。我只是想明白。
    • 您至少有任何提示或线索吗?
    【解决方案3】:

    我有同样的问题。我试图从 vagrant box 重新启动 apache,但在终端上收到以下警告。

    vagrant@vagrant-ubuntu-trusty-64:~$ sudo service apache2 restart
     * Restarting web server apache2   
    
    AH00112: Warning: DocumentRoot [/var/www/html] does not exist
    AH00558: apache2: Could not reliably determine the server's fully qualified 
    domain name, using 10.0.2.15. Set the 'ServerName' directive globally to suppress this message
    

    通过创建名为 /var/www/html 的目录来创建 DocumentRoot 以修复 404 问题

    【讨论】:

    • tldr:创建 html 文件夹
    • 谢谢鲁本。是的,我忘记创建html文件夹了,创建后就ok了!
    【解决方案4】:

    问题出在/etc/apache2/sites-enabled000-default 文件上。

    Apache2 指向 var/www/html 和 vagrant 示例指向 var/www 只需删除 de /html 并创建一个 sudo service apache2 restart

    【讨论】:

      【解决方案5】:

      我尝试了两种可行的解决方案:

      首先是更改文件/etc/apache2/sites-enabled/000-default.conf修改/var/www中的DocumentRoot而不是/var/www/html

      第二种是将Vagrant文件bootstrap.sh改成如下方式:

      #!/usr/bin/env bash
      
      apt-get update
      apt-get install -y apache2
      if ! [ -L /var/www/html ]; then
        rm -rf /var/www/html
        ln -fs /vagrant /var/www/html
      fi
      

      除此之外,由于某种原因,我还不得不更改 Vagrantfile 中的端口转发配置,添加 host_ip 键,如下所示:

      Vagrant.configure(2) do |config|
        config.vm.box = "hashicorp/precise32"
        config.vm.provision :shell, path: "bootstrap.sh"
        config.vm.network :forwarded_port, host: 4567, guest: 80, host_ip: "127.0.0.1"
      end
      

      【讨论】:

        猜你喜欢
        • 2013-08-24
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2016-10-09
        • 1970-01-01
        • 1970-01-01
        • 2020-09-10
        • 2012-06-05
        相关资源
        最近更新 更多