Vagrant port forwarding 80 to 8000 with Laravel Homestead

21,892

Solution 1

You should continue to use ports above 1024 since they are non-privileged ports, BUT if you do want you can run as port 80 on the Homestead VM, as long as you don't have anything holding on to that port on the host machine. Just tried it and it worked, with a few gotchas. First, you change that line in the .rb file from:

config.vm.network "forwarded_port", guest: 80, host: 8000

to

config.vm.network "forwarded_port", guest: 80, host: 80

When you fire your VM up after saving you will get a warning from vagrant:

==> default: You are trying to forward to privileged ports (ports <= 1024). Most

==> default: operating systems restrict this to only privileged process (typically

==> default: processes running as an administrative user). This is a warning in case

==> default: the port forwarding doesn't work. If any problems occur, please try a

==> default: port higher than 1024.

==> default: Forwarding ports...

default: 80 => 80 (adapter 1)

But it worked for me. Now, to actually get to the VM I had to use it's private IP instead of the localhost name:

http://192.168.10.10/

But sure enough my site was there and everything was working. If you decide to keep it that was you can add that IP address to your hosts file to give it a nice short name.

Hope this helps.

Solution 2

I see there is an accepted answer, but this alternative may also help someone. If I understand correctly you really dislike the port "8000"!

Have you tried setting a private network?

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    /*other config stuff here */
    config.vm.network :private_network, ip: "192.168.33.22"

This way you can simply use that IP address, or edit you hosts file to map the local domain to that IP.

Take a look at the Vagrant docs:Vagrant Private Networks

BTW, You shouldn't need to shutdown your IIS local server as that is running on a totally different IP range. I have Apache running locally while also accessing the VM server. This allows you to use tools like composer (to pull in laravel) on your local if needed.

Solution 3

I'm not sure what the confusion is - this is the way it's supposed to work.

The web server on the VM listens on port 80. Vagrant/VirtualBox forwards that port from 80 (on the VM) to 8000 (on localhost) so that you can access the site at http://localhost:8000.

Port 80 on the VM's domain name is not going to be available - that domain name probably resolves to localhost.

Try the following: dig local.kujif.com (or nslookup or even ping - I don't know what tools are available on Windows) to find out what IP address that name is resolving to. You will probably find that it's 127.0.0.1 (localhost).

You could try using the IP address set in the homestead file instead: http://192.168.10.10/ - this might work, but it will depend on how networking is configured in the VM.

Ideally, you need to set networking to "bridged" in the VM - this will make the VM look (to your network) like any other device on the network. Other networking options in the VM (sorry, I'm not familiar with the options in VirtualBox) will set the VM up with its own network that is not accessible outside the VM - this is why port forwarding is used to expose network services on the VM.

Share:
21,892
Tyler
Author by

Tyler

I have been programming since I was in high school which got me hooked. I started by building websites and doing graphic design until I made my first online game to play Risk in my early 20’s. It was a small community of around 2000 members, but I was the sole creator using my friends custom built MVC framework he called Tru. Later building more complex websites to manage as many tasks as possible in different organizations to help automate mundane tasks. I’ve lived in Germany, Thailand, Cambodia, and South Korea. I’ve spent much of my life traveling as a missionary and training others on how to develop things, whether websites or games. I speak Thai fluently and very basic German. My passion is automation and organization. I thrive in tasks where I can accomplish one of these passions. Organizing massive amounts of information into easy to read and process data or simplifying tasks to allow programs to do the majority of the work. I am very focused on usability and I highly depend on tests to take away the stress of worrying that changes will break something else. The more situations a test can account for the better.

Updated on July 09, 2022

Comments

  • Tyler
    Tyler almost 2 years

    My Problem:

    I can only access my sites through port 8000, but not 80, which makes me think it is not redirecting 80 to 8000 as it says it should be. I want to simply type local.kujif.com into my browser and it loads the site, which I read was port 80 by default. I am using curl to check it and it returns:

    curl 'http://local.kujif.com'
    curl: (7) Failed connect to local.kujif.com:80; No error
    

    However if I add :8000 to the url then it works; it returns my index.php which simply prints 'test':

    curl 'http://local.kujif.com:8000'
    test
    

    My Details:

    I am using Laravel Homestead and Vagrant with Oracle VM VirtualBox.

    In the Homestead.rb it has the port forwarding. I haven't edited it at all:

    config.vm.network "forwarded_port", guest: 80, host: 8000
    config.vm.network "forwarded_port", guest: 3306, host: 33060
    config.vm.network "forwarded_port", guest: 5432, host: 54320
    

    I also have Microsoft IIS installed for my work stuff. I obviously stop that service whenever I need vagrant to use the localhost.

    "vagrant up" shows:

    enter image description here

    My Homestead.yaml file:

    ---
    ip: "192.168.10.10"
    memory: 2048
    cpus: 1
    
    authorize: /Users/Tyler/.ssh/id_rsa.pub
    
    keys:
        - /Users/Tyler/.ssh/id_rsa
    
    folders:
        - map: C:\DEV\Linux
          to: /var/www/
    
    sites:
        - map: homestead.app
          to: /home/vagrant/Code/Laravel/public
        - map: local.kujif.com
          to: /var/www/kujif
    
    variables:
        - key: APP_ENV
          value: local
    
  • Tyler
    Tyler almost 10 years
    hmmm I will try that stuff when I get off work, thank you. Shouldn't I just be able to type local.kujif.com into the browser and it loads my local site rather than having to add :8000 every time?
  • Kryten
    Kryten almost 10 years
    It depends on what IP address local.kujif.com resolves to. If it's 127.0.0.1, then it's the same as typing http://localhost, which would require port 8000.
  • Tyler
    Tyler almost 10 years
    Yeah it resolves to 127.0.0.1 which is how it is setup in my hosts file. I've checked my VM box and there are two hosts connections with different IPs. I tried pinging the IP's and both work, so I tried changing my hosts file to resolve local.kujif.com to each of those IPs and neither worked. If I can get it to use one of those VM IPs would it bypass the need for :8000?
  • Kryten
    Kryten almost 10 years
    Yes, it should. At this point, you're probably going to get deep into the configuration of your VM and I have to admit I'm not that familiar with VirtualBox (I'm more familiar with VMWare ESXi).
  • Tyler
    Tyler almost 10 years
    Hey, but I really appreciate the help up to this point, that is something I can work with. Thank you!
  • Tyler
    Tyler almost 10 years
    Thanks for the help! It was somewhat what you mentioned in your answer. It was that forwarding, the default forwarding was pushing it to 8000. I needed to forward 80 to 80. If you put that somewhere in your answer I can set as correct answer, otherwise I will answer my own question.
  • jagzviruz
    jagzviruz over 8 years
    This should have been the accepted answer. Mapping a domain path to this configured local IP in the hosts file, allows me to access multiple projects hosted on my homestead box.