Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access web server on VirtualBox/Vagrant machine from host browser?

I have a Django web server on a VirtualBox/Vagrant machine running Ubuntu.

I have followed this guide to create a Django project: https://docs.djangoproject.com/en/dev/intro/tutorial01/

I have a web server running at http://127.0.0.1:8000/ inside my guest machine. This is the first time I am running a Django web server. It is supposed to be a hello world app.

How can I access this web application from my host browser?

I have tried running ifconfig in the guest to get the IP that I should visit I found a promising IP address in inet addr.

But I have tried entering the following into my host browser and it didn't work. http://inetaddrnumbers:8000/

How can I access the web server from my browser?

like image 300
user1283776 Avatar asked Oct 14 '15 15:10

user1283776


People also ask

What is the command to connect to a running virtual machine using vagrant?

Command: vagrant ssh [name|id] [-- extra_ssh_args] This will SSH into a running Vagrant machine and give you access to a shell.

How do I access vagrant from another computer?

I found how to connect to another computer locally by entering the vagrant environment, vagrant up --> vagrant ssh . And connected to another computer by typing in ssh [email protected] where 192.168. x.x is the local address to the computer.


2 Answers

Try this.

  1. Open the vagrant file (should be in the directory where you specified to create a new vagrant machine).
  2. Search for config.vm.network. If you didn't setup the file earlier, it should be commented.
  3. Change it to look something like this config.vm.network "private_network", ip: "55.55.55.5". Here ip address (55.55.55.5) can be any ip address you want.
  4. Now logout from the vagrant machine and reload your vagrant machine by this command vagrant reload.
  5. Again ssh to your vagrant machine and restart your django server by this command python manage.py runserver 0.0.0.0:80. Again the port address (80) can be 8000 if you want so.
  6. After that, in your browser, enter the following address 55.55.55.5, and hopefully you should see your webapp.

Now if you would like to go further, you can edit your host file, and add this line

55.55.55.5 mynewdomain.com

Then in your browser, enter the follow address,

mynewdomain.com

And you should see your web app. Note that, www is not added in the domain name inside the host file, so only mynewdomain.com can be accessed. You can however add it.

Hope this helps. Cheers.

like image 166
Kakar Avatar answered Oct 17 '22 15:10

Kakar


Complementing @Kakar answer, this configuration can also be done using this:

config.vm.network "private_network", type: "dhcp"

This will assign an IP automatically.

For further reading: https://www.vagrantup.com/docs/networking/private_network.html

like image 32
Fagner Fonseca Avatar answered Oct 17 '22 13:10

Fagner Fonseca