How to access Laravel Homestead on other devices?

2020-03-25 08:34发布

问题:

My machine and Android devices are on the same network.

  • Machine IP: 192.168.0.11
  • The IP where Homestead is running: 192.168.10.10

If I enter 192.168.0.11:8000 on my Android device I can use only one site.

How can I enter all sites stored in Homestead?

This is my Homestead.yaml file:

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

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

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

sites:
    - map: site1.com
      to: /home/vagrant/Homestead-Projects/Site1/public
    - map: site2.app
      to: /home/vagrant/Homestead-Projects/Site2/public

databases:
    - homestead
    - db_site1
    - db_site2

variables:
    - key: APP_ENV
      value: local

# blackfire:
#     - id: foo
#       token: bar
#       client-id: foo
#       client-token: bar

# ports:
#     - send: 93000
#       to: 9300
#     - send: 7777
#       to: 777
#       protocol: udp

This is my hosts file:

127.0.0.1   localhost
127.0.1.1   host

#Virtual Hosts on Homestead
192.168.10.10   site1.com
192.168.10.10   site2.com

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

回答1:

tl;dr

You have to add a row for each site in your host file on the remote machine (just like in the server where Homestead is running) but with the server's IP adress. Then just type sitename.local:8000.

Example

In your case the server's IP is 192.168.0.11. Within this server, Homestead serves requests on 192.168.10.10. So in the host file of the server you have these lines:

192.168.10.10   site1.com
192.168.10.10   site2.com

You have to copy these rows to the remote PC's host file and then replace the IP address with the server's IP: 192.168.0.11.

192.168.0.11   site1.com
192.168.0.11   site2.com

Depending on your OS you may have to restart the PC or the DNS service. After that, you can access the sites from the remote PC in these addresses:

site1.com:8000
site2.com:8000

Note

Keep in mind that editing (writing) the hosts file requires superuser/administrator permissions. This is easy to solve in a PC, if you have access to the administrator account, but can be complicated on a mobile device. For Android check out these questions:

  • Android Enthusiasts: How to edit 'etc/hosts' file?
  • Stack Overflow: How to change the hosts file on android?


回答2:

I believe this is the right answer acess homestead from external

all you need to just add default server on your nginx.conf

listen 80 default_server;


回答3:

You can use http://xip.io/ to setup a magic domain name and you can use these domains to access virtual hosts on your development web server from devices on your local network.

Installation is also easy too https://github.com/basecamp/xip-pdns

If you are using vagrant you can also use vagrant share.