Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Homestead cant access second site remotely

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?

like image 849
userqwert Avatar asked Jun 09 '17 15:06

userqwert


2 Answers

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.
like image 175
Michael A. Avatar answered Nov 13 '22 16:11

Michael A.


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

like image 39
prola Avatar answered Nov 13 '22 15:11

prola