Homestead cant access second site remotely

2019-06-17 18:32发布

I have one site setup fine, but the second can't be accessed remotely:

My YAML:

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

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

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

sites:
    - map: app.app
      to: /home/vagrant/sites/app1/public
    - map: app2.app
      to: /home/vagrant/sites/app2/public
      port: 8100

ports:
    - host: 81
    - guest: 8100

I can access the first site using my IP easily - http://192.168.0.5/ but the second one cannot share the same port, so I've added 81 -> 8100.

When I try to reprovision my homestead vagrant box it says: There are errors in the configuration of this machine. Please fix the following errors and try again:

vm:
* Forwarded port definitions require a "host" and "guest" value
* Ports to forward must be 1 to 65535
* Ports to forward must be 1 to 65535

Any ideas?

3条回答
做个烂人
2楼-- · 2019-06-17 19:10

According to the documentation found at https://laravel.com/docs/5.4/homestead

you don't need to specify ports when adding multiple sites

sites:
    - map: homestead.app
      to: /home/vagrant/Code/Laravel/public
    - map: another.app
      to: /home/vagrant/Code/another/public

then add the following to your hosts file

192.168.10.10  homestead.app
192.168.10.10  another.app

make sure to run vagrant reload --provision

查看更多
贼婆χ
3楼-- · 2019-06-17 19:14

The service xip.io can make short work of this. Here's how:

As an example, I have the following Homesite app setup, 'rentmanager.test'. This is the first application I provisioned on Homestead, so it's the default route. To access this from my local network, all I have to do is go to:

http://rentmanager.test.192.168.5.124.xip.io:8000

Where 'rentmanager.test.' is the name of my Laravel application on Homestead, '192.166.5.124.' is my IP address on the local network, 'xip.io:8000' is the redirect service domain and the needed port to access the app (8000) outside of the default routing that Homestead sets up.

Now, this works fine out of the box with the first site you provision, but what happens if we have more than one site? That requires a little more legwork. Let's assume we've got a second app called 'sitescanner.test' that we want to access. If I try to access the same URL above with the new app URL, you'll see I'm still served the first application! Let's resolve that as well.

First, you're going to need to add an additional rule to your hosts file. So, assuming our new app is called 'sitescanner.test':

# Vagrant Sites External Access

192.168.10.10 sitescanner.test.192.168.5.124.xip.io

Once that's in place then we have to update the provisioning scripts in Homestead to create the correct virtual host entries so Homestead knows how to route the request. To do that, we'll edit your Homestead.yaml. Under the 'sites:' directive:

- map: sitescanner.test
  to: /home/vagrant/Code/sitescanner/public   
- map: sitescanner.test.192.168.5.124.xip.io
  to: /home/vagrant/Code/sitescanner/public 

Once this is complete, simply 'vagrant halt' (if it's already running), then 'vagrant up --provision' and you should be able to access your site from

http://sitescanner.test.192.168.5.124.xip.io:8000

Anywhere on your network! Hope this helps.

While I'm thinking about it, there is a final consideration for this configuration:

  • You may want to set a static IP address on your host machine if possible. If you're being assigned a dynamic IP address via DHCP it's possible (and likely) for this configuration to eventually break if you're ever assigned a new IP.
查看更多
劳资没心,怎么记你
4楼-- · 2019-06-17 19:21

I do not know which version of Laravel or Homestead you are using, but this is possible. With the Homestead versions documented for Laravel 5.6 or 5.7, you were 95% of the way to having this complete.

  1. Instead of using port: 8100 in your sites configuration, you should have used port: 81.
  2. Instead of guest and host, use send and to.

I used the exact same setup as you except for those two differences, and it works for me.

ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

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

sites:
    - map: app.app
      to: /home/vagrant/sites/app1/public
    - map: app2.app
      to: /home/vagrant/sites/app2/public
      port: 81

ports:
    - send: 8100
      to: 81

You would then be able to access your app via http://app2.app:8100 if your hosts file has an entry for app2.app or via http://192.168.0.5:8100 if you aren't using the hosts file.

查看更多
登录 后发表回答