3
votes

I have a Django web server on a VirtualBox/Vagrant machine running "bento/centos-6.7-i386". 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 tried adding this line - config.vm.network "private_network", ip: "55.55.55.5" in the vagrant file and then tried to run the python manage.py runserver 0.0.0.0:80 command as per 1 of the solutions explained in previous discussions by others but I couldn't access the site from my host browser using 55.55.55.5:8000.

How can I access the web server from my browser?

Following given is my Vagrant File:

# -- mode: ruby -- 
# vi: set ft=ruby : 
VAGRANTFILE_API_VERSION = "2" 
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| 
config.vm.box = "bento/centos-6.7-i386" 
config.vm.network "forwarded_port", guest: 8000, host: 8000 
config.vm.network "forwarded_port", guest: 8080, host: 8080 
config.vm.network "forwarded_port", guest: 5000, host: 5000 
config.vm.network "private_network", ip: "10.10.10.10" 
end
3
Try python manage.py runserver 0.0.0.0:8000 and access port 8000 of your host machine.Klaus D.
if you did run python manage.py runserver 0.0.0.0:80 make sure to add a line config.vm.network "forwarded_port", guest: 80, host: 8800 so you forward the port 80 from your VM (where you have the server running) to the host port 8800 so you can access localhost:8800 from your host web browser (sometime you need to change localhost to 127.0.0.1 depending browser and configuration)Frederic Henri

3 Answers

8
votes

You're forwarding port 8000 on the guest to 8000 on the host. Try:

python manage.py runserver 0:8000

Then in your browser visit: http://localhost:8000

That'll leave port 80 free if you end up wanting to run a web server for testing as well. Good luck!

2
votes

if you are using below line in your Vagrantfile for port forwarding

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

It means you will be able to access your guest application which running at port 8000 in your host browser at port 8000. so you will be able access by hitting http://127.0.0.1:8000 or http://localhost:8000 in your host.

why you are forcing your app to run on 0.0.0.0 ? . it is not required , or if you want to access with guest IP address then there is no sense to use port forwarding.

if you changed this configuration in Vagrantfile after provision then don`t forget to reload

vagrant reload
0
votes

I got a problem with Django and port forwarding insider VB.

Django doesn't allow external hosts to access it.

So change the start command to python3 ./manage.py runserver 0.0.0.0:8000

and everything worked