Laravel Homestead IP地址无法工作

22

我使用 Laravel Homestead 2.0 作为我的虚拟机,并尝试在 YAML 文件中默认的 IP 地址 192.168.10.10 上提供我的站点。

我的 /etc/hosts 文件如下:

# Homestead
192.168.10.10   beta.dev
192.168.10.10   deploy.dev

我的 Homestead.yaml 文件看起来像这样:

---
ip: "192.168.10.10"
memory: 2048
cpus: 1

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Projects
      to: /home/vagrant/Projects

sites:
    - map: beta.dev
      to: /home/vagrant/Projects/emorybeta/public
    - map: deploy.dev
      to: /home/vagrant/Projects/deploy/public

...

当我将我的域名链接到127.0.0.1时,网站会出现,但我必须在URL末尾附加端口8000(这并不是什么大问题,我只是想让指定的IP地址正常工作)。

有人知道为什么我将我的域名指向192.168.10.10后不能连接到服务器吗?


更新:

当我ping deploy.dev时,正确的IP地址显示出来,但我的浏览器仍然无法连接到服务器。我认为这可能与Yosemite中的DNS问题有关。


1
无心之问,你运行了vagrant provision命令吗? - rigobcastro
可能是重复的问题:Vagrant - Homestead设置多个站点 - Matt Burrow
对于那些仍在苦苦挣扎的人,你需要将你的PHP版本与适合的Homestead Box版本相匹配,参见这里。我也曾为此苦苦挣扎了一个星期,但根据说明操作后终于解决了问题。 - aomanansala
根据我的经验,在某些设备上,IP地址有时可能会变得“陈旧”,因此更改配置以使用新的IP地址会带来好处:https://superuser.com/a/1214376/74576 - Ryan
11个回答

8
我几周前也遇到了同样的问题。
首先,请确保您的文件夹路径正确,如果是这样,请再次进行双重检查。
接下来,请运行 homestead destroy 和 homestead up 以重新初始化虚拟机。
如果所有这些都没有解决问题: 检查家中是否有其他设备也在使用 192.168.10.10 的IP地址。
如果所有这些都无法解决问题,那么您的问题可能更难修复,我建议为此在Github上提出一个问题。

7
我建议使用 vagrant reload --provision 命令,而不是销毁并重新启动。 - parker_codes
@GoogleMac 对我很有帮助 - 而且不需要破坏镜像。我猜我的电脑由于长时间未使用而变得陈旧了。 - thomthom

3
这是我为我的Windows 10机器提供的解决方案:
确保您向vagrant盒子添加了一个持久路由 在管理员命令提示符中输入 route -p add 192.168.10.0 mask 255.255.255.0 192.168.10.1 并确保您能够ping通192.168.10.10 还要检查虚拟机host-only适配器配置的IP地址是否为192.168.10.1,掩码为255.255.255.0

2
经过数小时的搜索,这才解决了我的问题。必须将虚拟盒子host-only适配器更改为192.168.10.1(原本使用的是192.168.10.10)。谢谢! - Greg

3

嗨,我曾经遇到过同样的问题。最终我通过安装废弃的net-tools软件包来解决了这个问题: sudo apt-get updatesudo apt-get install gnome-nettool

之后, homestead destroyhomestead up

这使我可以使用主机文件中指定的“domains”从本地机器的浏览器访问我的虚拟机站点 - 在您的情况下是beta.dev和deploy.dev - 无需引用localhost或端口8000。祝好运,希望这可以帮到您。


这个方法也解决了我在Ubuntu Gnome 17.04上的问题。 - Phillip Hartin

2

我曾经遇到过这个问题,后来发现是nginx在加载TLS证书时出错导致无法启动。但是vagrant up命令并没有报告nginx启动失败或任何端口绑定失败的情况。

为了诊断这个问题,我按照以下步骤操作:

$ nmap homestead.app
Starting Nmap 7.01 ( https://nmap.org ) at 2017-10-03 16:16 NZDT
Nmap scan report for homestead.app (192.168.10.10)
Host is up (0.00077s latency).
Not shown: 995 closed ports
PORT     STATE SERVICE
22/tcp   open  ssh
1025/tcp open  NFS-or-IIS
3306/tcp open  mysql
5432/tcp open  postgresql

列表中没有80端口。让我们仔细检查一下80端口。

$ nmap homestead.app -p 80
...
Host is up (0.00020s latency).
PORT   STATE  SERVICE
80/tcp closed http

所以它肯定是关闭的。客人日志上说了什么?vagrant ssh然后...

    $ systemctl status nginx.service
    ...
    Active: failed (Result: exit-code) since Tue 2017-10-03 03:06:12 UTC; 1min 30s ago
    ...
 homestead systemd[1]: Starting A high performance web server and a reverse proxy server...
 homestead nginx[1250]: nginx: [emerg] PEM_read_bio_X509_AUX("/etc/nginx/ssl/homestead.app.crt") failed (SSL: error:0906D06C:PEM routines:PEM_read_bio:no start line:Expecting: TRUSTED CERTIFICA
 homestead nginx[1250]: nginx: configuration file /etc/nginx/nginx.conf test failed
 homestead systemd[1]: nginx.service: Control process exited, code=exited status=1
 homestead systemd[1]: Failed to start A high performance web server and a reverse proxy server.
 homestead systemd[1]: nginx.service: Unit entered failed state.
homestead systemd[1]: nginx.service: Failed with result 'exit-code'.

由于配置错误,Nginx启动失败。 PEM_read_bio_X509_AUX 错误指向文件 /etc/nginx/ssl/homestead.app.crt。该文件在配置中的使用位置是什么?
$ sudo vim /etc/nginx/sites-enabled/homestead.app

我将相关行注释掉了:

@@ -1,6 +1,6 @@
 server {
     listen 80;
-    listen 443 ssl http2;
+#    listen 443 ssl http2;
     server_name homestead.app;
     root "/home/vagrant/Code/public";

@@ -42,7 +42,7 @@ server {
         deny all;
     }

-    ssl_certificate     /etc/nginx/ssl/homestead.app.crt;
-    ssl_certificate_key /etc/nginx/ssl/homestead.app.key;
+#    ssl_certificate     /etc/nginx/ssl/homestead.app.crt;
+#    ssl_certificate_key /etc/nginx/ssl/homestead.app.key;
 }

使用$ sudo service start nginx启动nginx,并从主机上再次运行nmap

$ nmap homestead.app -p 80,443
...
PORT   STATE SERVICE
80/tcp open  http
443/tcp closed https

端口80已经打开,现在应该可以从http://homestead.app访问。当然,TLS不起作用,但您应该能够通过生成新的证书来解决它。我不确定证书无法加载的原因。


2
昨天我在使用Homestead 5时遇到了类似的问题。经过排查,我发现问题的原因是我运行了错误的Vagrantfile文件。
实例应该对IP地址192.168.10.10做出响应。
在我的情况下,我通过遵循以下说明来纠正问题:http://laravel.com/docs/5.0/homestead 总之:
在终端中运行:
vagrant box add laravel/homestead

然后。
git clone https://github.com/laravel/homestead.git Homestead

然后
cd ./Homestead  

并在新的“Homestead”文件夹内运行此命令

bash init.sh

然后在 ./Homestead 目录下运行命令 vagrant upvagrant provision,这样就会创建一个 Vagrantfile 文件。

以下是我系统上 vagrant provision 命令的输出结果:

    vagrant provision
==> default: Running provisioner: file...
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: Running provisioner: shell...
    default: Running: /var/folders/q6/pgygb1ln4rg7_nvv0p8n1v300000gn/T/vagrant-shell20150714-94163-17xwlzk.sh
==> default: Running provisioner: shell...
    default: Running: /var/folders/q6/pgygb1ln4rg7_nvv0p8n1v300000gn/T/vagrant-shell20150714-94163-rncs6l.sh
==> default: nginx stop/waiting
==> default: nginx start/running, process 1751
==> default: php5-fpm stop/waiting
==> default: php5-fpm start/running, process 1766
==> default: Running provisioner: shell...
    default: Running: /var/folders/q6/pgygb1ln4rg7_nvv0p8n1v300000gn/T/vagrant-shell20150714-94163-e9qxwn.sh
==> default: Warning: Using a password on the command line interface can be insecure.
==> default: Warning: Using a password on the command line interface can be insecure.
==> default: Running provisioner: shell...
    default: Running: /var/folders/q6/pgygb1ln4rg7_nvv0p8n1v300000gn/T/vagrant-shell20150714-94163-14rlz2c.sh
==> default: Running provisioner: shell...
    default: Running: /var/folders/q6/pgygb1ln4rg7_nvv0p8n1v300000gn/T/vagrant-shell20150714-94163-16ethaq.sh
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: php5-fpm stop/waiting
==> default: php5-fpm start/running, process 1861
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: You are already using composer version 92faf1c7a83a73794fb914a990be435e1df373ca.
==> default: Running provisioner: shell...
    default: Running: /var/folders/q6/pgygb1ln4rg7_nvv0p8n1v300000gn/T/vagrant-shell20150714-94163-btuuhg.sh

2

在Ubuntu上安装net-tools可以解决这个问题。

$ sudo install net-tools
$ vagrant destroy
$ vagrant up

1
为什么要踩?如果它对你没用,我不明白为什么要踩?它对其他人和我自己都有效。 - Giovanni S

1
在我的情况下,问题通过让vagrant up安装客户机附加组件来解决:
~/Homestead (master|✔) $ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'laravel/homestead' is up to date...
==> 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
[...]
GuestAdditions versions on your host (5.0.26) and guest (5.0.20) do not match.
[...]
Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
mount: /dev/loop0 is write-protected, mounting read-only
Installing Virtualbox Guest Additions 5.0.26 - guest version is 5.0.20
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.0.26 Guest Additions for Linux............
VirtualBox Guest Additions installer
Removing installed version 5.0.20 of VirtualBox Guest Additions...
Removing existing VirtualBox DKMS kernel modules ...done.
Removing existing VirtualBox non-DKMS kernel modules ...done.
Copying additional installer modules ...
Installing additional modules ...
Removing existing VirtualBox DKMS kernel modules ...done.
Removing existing VirtualBox non-DKMS kernel modules ...done.
Building the VirtualBox Guest Additions kernel modules ...done.
Doing non-kernel setup of the Guest Additions ...done.
You should restart your guest to make sure the new modules are actually used

在这一点上,我得到了一个新的界面,具有期望的IP地址:

enp0s8    Link encap:Ethernet  HWaddr 08:00:27:e8:04:04
          inet addr:192.168.8.10  Bcast:192.168.8.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fee8:404/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:996 (996.0 B)

1
我在Windows机器上遇到了这个问题,后来发现我不小心颠倒了文件夹部分的"map"和"to"。当我发现该文件夹不在vagrant ssh会话中的~/code/下时,很明显它没有正常工作,但仔细检查路径并没有发现任何问题。
这是在Windows机器上映射文件夹部分的正确方法:
folders:
    - map: D:/Development/PHP/projects/
      to: /home/vagrant/code

1
我有过相同的情况,但我进行了更改。
我用这个 IP 地址 127.0.0.1 更改了我的 /etc/hosts 文件。

我正在尝试避免使用 127.0.0.1,这样我就不必在每个 URL 后面添加 :8000。:/ - evcohen

0
唯一的原因是需要在主机文件中放置主机名和IP地址
C:\ Windows \ System32 \ drivers \ etc
  • 以管理员身份打开记事本
  • 点击打开文件并选择“主机”文件
  • 放置您的域(例如192.168.10.10 homestead.app)
  • 保存(确保您正在保存为管理员)

完成!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接