Accesing Homestead Vagrant from LAN via URL - vagrant

I'm trying to set up the hosts file in my laptop to allow connecting to my Vagrant box on my PC via URL, instead of just IP.
The IP part worked by itself just fine, I had to enter the IP of my PC, for example: 192.168.1.15:8000 and I got connected to the default website. Now here's my first question actually, why do I get connected to one particular website, even though I have two.
My homestead.yaml file looks as such:
---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ssh_key.pub
keys:
- ssh_key
folders:
- map: /HomesteadCode
to: /home/vagrant/Code
sites:
- map: homestead.app
to: /home/vagrant/Code/public
- map: phpmyadmin.dev
to: /home/vagrant/Code/phpmyadmin
But I get connected to what is homestead.app. How would I then connect to phpmyadmin.dev?
And my other question is, how do I set up the hosts file on my laptop to allow connecting using the URL? I set up the hosts as such:
192.168.1.21 homestead.app
192.168.1.21 phpmyadmin.dev
and it didn't work, even if i entered homestead.app:8000 into the browser.
And the last question, why does it work on ym local PC, even if I set up the hosts file like so:
192.168.10.10 homestead.app
192.168.1.21 homestead.app
192.168.1.21 phpmyadmin.dev
192.168.10.10 phpmyadmin.dev
Even if there are two entries for homestead, one seemingly wrong, it still works.

Related

Laravel project wont serve to given ip via Homestead

I am having trouble setting up my laravel project on my mac. I have (As far as I can tell) set up the homestead.yaml config correctly within my Homestead directory:
---
ip: "192.168.10.10"
memory: 2048
cpus: 2
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: ~/Documents/GitHub/project-darwin
to: /home/vagrant/code
sites:
- map: homestead.test
to: /home/vagrant/code/public
php: "7.2"
databases:
- homestead
After I have used vagrant up, on using vagrant ssh I can see my project inside of vagrant/code/project-darwin.
My hosts file is as follows:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
192.168.10.10 homestead.test
Which looks correct as is no different to what I normally do when running other laravel projects on virtualbox, however on going to my url, I get a 500 error and the same applies if I go directly to the ip (198.168.10.10).
A further thing I find suspicious is the following. When I use vagrant up, this is part of what shows in my terminal:
homestead-7: Waiting for machine to boot. This may take a few minutes...
homestead-7: SSH address: 127.0.0.1:2222
homestead-7: SSH username: vagrant
Should this ip be the same as the ip I supplied? Anyway, I have run out of ideas as to how to go about fixing this, any suggestions?
So it turns out my mac had auto selected . hyperv as my provider when I ran vagrant box add laravel/homestead, oh well..
In your Homestead.yaml config file you are doing the following:
Making a shared folder mapping project-darwin to /home/vagrant/code
folders:
- map: ~/Documents/GitHub/project-darwin
to: /home/vagrant/code
But under sites you are mapping the folder /home/vagrant/code/public to homestead.test, meaning when you are requesting homestead.test or the IP of your Vagrant Box, you are not calling upon project-darwin but rather the "empty" public folder.
sites:
- map: homestead.test
to: /home/vagrant/code/public
I would recommend either moving your code to the public folder or mapping sites to your desired destination.
Hope this helps :)

Second Vagrant Site Returns Empty Response

I have a vagrant/homestead setup with two sites.
The first one works as expected, but when I started up the server today the second one is just returning a 200 status with no body:
My Homestead file is as follows
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: ~/code
to: /home/vagrant/code
sites:
- map: site1.test
to: /home/vagrant/code/site1-backend/public
- map: site2.test
to: /home/vagrant/code/site2-backend/public
databases:
- dbname
My hosts file is
192.168.10.10 site1.test
192.168.10.10 site2.test
Notes / Things I've Tried
If I stop the vagrant I no longer get the 200 response so it is definitely connecting to it
Any URL I go to at site2.test returns the empty 200 result
Going to site3.test does not connect
I am using Laravel in both projects, but there is no recent output in the Laravel logs
On Vagrant /var/log/nginx/site2.test-error.log is empty as is /var/log/nginx/error.log
Both projects have identical .htaccess files
Both projects have vagrant as the owner for all files
I have run
> vagrant destroy && vagrant up
To no avail.
I'm not sure how to further debug this issue.
This turned out to be an error in my Laravel .env file
Changing
APP_NAME=App Name
to
APP_NAME="App Name"
Fixed it. I was unable to find any logging to this effect and worked it by comparing my setup with my live server.

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?
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.
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
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.
Instead of using port: 8100 in your sites configuration, you should have used port: 81.
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.

Homestead project runs only when used 127.0.0.1 in the hosts file

I am new to the Homestead and virtualbox/vagrant world. I went through some video tutorials on how to strat working with Homestead.
Everything went smoothly until the last step. It says we need to add the IP address mention in the Homestead.yaml file into the hosts file.
I did that, but it always gave me the following error:
This site can’t be reached
dev.test refused to connect.
Search Google for dev test 8000
ERR_CONNECTION_REFUSED
My hosts file looks something like this:
192.168.10.10 dev.test
But when I use 127.0.0.1 dev.test it works perfectly fine.
I don't know what's happening.
Even the official documentation says we should 192.168.10.10 dev.test in the /etc/hosts file.
Where am I going wrong??
Here's my Homestead.yaml file
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/laravel.pub
keys:
- ~/.ssh/laravel
folders:
- map: /var/www/Laravel
to: /home/vagrant/Laravel
sites:
- map: dev.test
to: /home/vagrant/Laravel/test/public
databases:
- homestead
Looks like I found my mistake.
I was trying to access my web app using port 8000 even after using 192.168.10.10 dev.test in the hosts file. I should have simply used dev.test in the browser to access the web app.
Silly Mistake.
I makes perfect sense to use dev.test:8000 in the browser if the hosts file reads 127.0.0.1 dev.test

Vagrant Laravel new project set up

I am now ready to start a new project with Laravel using Vagrant. My initial project worked fine but I can't seem to get the new project to show in the browser with its unique name loginauth.app.
My very first app was called PaulsApp and if I typed paulsapp.app in the browser the laravel started page showed fine.
I followed the instructions to create a new project using composer create project in my VM in the same directory as my first app. I can see this in both the local and on the VM.
Next I updated the homestead.yaml file to include the new sitemap and run vagrant provision to let the VM know of the updates.
I believe I must be overlooking something as my .yaml file has the map as homestead.app but that does not work only paulsapp.app which is in my etc/hosts file.
Please could somebody explain how the .yaml file and the hosts file are interacting. If I completely remove the mapping to PaulsApp in the yaml file, paulsapp.app in the browser still shows up the correct page.. as such I am a tad confused. I thought one simply added a mapping to the yaml and then could access that mapping through the browser. I want to type loginauth.app in the browser and have it point to the index file in the public directory of /home/vagrant/code/loginauth/ thanks.
homestead.yaml
---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: ~/code
to: /home/vagrant/code
sites:
- map: homestead.app
to: /home/vagrant/code/PaulsApp/public
- map: loginauth.app
to: /home/vagrant/code/loginauth/public
databases:
- homestead
variables:
- key: APP_ENV
value: local
etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
192.168.10.10 Paulsapp.app
#127.0.0.1 PaulsApp.dev
OK so thanks to some help from the guys over at #laravel irc channel..
I needed to add the correct line to the hosts file. I tried 192.168.10.10 loginauth.app but got no joy so I used 127.0.0.1 loginauth.app.
Then the key was to vagrant halt the VM, Vagrant UP it again then vagrant provision it so the VM could see the changes.
Now loginauth.app:8000/ brings up the page I was hoping for...

Resources