210

I followed the instructions here to run Django using the built-in webserver and was able to successfully run it using python manage.py runserver. If I access 127.0.0.1:port locally from the webserver, I get the Django page indicating it worked.

I realize the Django webserver is not a production server, but it's important for me for testing purposes to be able to access it from the outside world -- i.e. not from a web browser on the server, but from a different computer.

I tried:

http://mywebserver:port_django_runs_on

but it did not work. I also tried using the IP instead (based on ifconfig) to access:

http://myipaddress:port_django_runs_on 

which did not work either.

The web server is running so it must be accessible from the outside, I'm just not sure how. I am running Linux with Apache, though I have not configured Django with Apache.

Any ideas on how to do this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 3
    Why not configure Django with Apache and mod_wsgi? Why not do this properly? – S.Lott Feb 15 '10 at 03:05
  • @S.Lott What if you have many different projects on the server that you want to run at different times and you and you don't want to re-configure Apache every time you add a new project? – robenkleene Feb 16 '20 at 19:49
  • Does this answer your question? [How to make Django's devserver public ? Is it generally possible?](https://stackoverflow.com/questions/3328926/how-to-make-djangos-devserver-public-is-it-generally-possible) – alexdlaird May 07 '20 at 14:13

11 Answers11

363

You have to run the development server such that it listens on the interface to your network.

E.g.

python manage.py runserver 0.0.0.0:8000

listens on every interface on port 8000.

It doesn't matter whether you access the webserver with the IP or the hostname. I guess you are still in your own LAN.
If you really want to access the server from outside, you also have to configure your router to forward port e.g. 8000 to your server.


Check your firewall on your server whether incoming connections to the port in use are allowed!

Assuming you can access your Apache server from the outside successfully, you can also try this:

  • Stop the Apache server, so that port 80 is free.
  • Start the development server with sudo python manage.py runserver 0.0.0.0:80
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 2
    Using "python manage.py runserver 0.0.0.0:8000" still gives the same result. Could you say more about how I can configure Apache to handle this port? –  Feb 14 '10 at 10:38
  • 1
    @user248237 : You run the development server, this is totally unrelated to the Apache web server. The development server is a standalone web server. – Felix Kling Feb 14 '10 at 10:43
  • It requires special privileges to use port 80. You can try a `sudo python manage.py runserver 80`. – S.Lott Feb 14 '10 at 11:57
  • @S.Lott: Oh thank you. I know, it was in my mind as wrote it but somehow didn't make it to the keyboard ;) – Felix Kling Feb 14 '10 at 12:07
  • this worked, but the files loaded through the media url are not found? – a11hard Nov 16 '12 at 17:53
  • @FelixKling When I use runserver with `0.0.0.0:8000` instead of `127.0.0.1` & I don't have control over my router firewall, am I opening my development server to be accessed by anyone outside? What security precautions do I need to be aware of since there are lots of security holes in a development website. – user Apr 12 '14 at 04:58
  • @buffer: No, as I said in my answer, it makes the devserver only accessible to all devices in the same network (LAN). – Felix Kling Apr 12 '14 at 05:03
  • in Pycharm, edit the default Configuration for the Django webserver by inserting 0.0.0.0 for "Host:" – randalv Oct 27 '14 at 17:39
  • it's giving me `Invalid HTTP_HOST header: '192.168.0.107:8000'. You may need to add '192.168.0.107' to ALLOWED_HOSTS.` – abbood Jul 27 '17 at 12:48
  • 4
    Allowing everyone `ALLOWED_HOSTS = ['*']` worked for me to access Django app on a Mac from my Android phone – Anupam Dec 28 '17 at 06:02
  • @FelixKling, I can open with IP address from mobile which is in same local network, but not able to use with hostname. Why? With Hostname in same machine it's working. Hostname was assigned to IP in `host file`. Can you please help? – shaik moeed Feb 16 '19 at 11:24
  • @shaikmoeed: The host file only has effect on the machine it is located. If you want to be able to access a computer by host name from any device in your network, you'd have to setup a local DNS server for your network. – Felix Kling Feb 17 '19 at 08:55
  • @FelixKling, I'm getting `csrf_token is not set` when i run on other machine with my ip address. Can you please tell what will be the reason? – shaik moeed Mar 01 '19 at 09:31
  • I have `ALLOWED_HOST = [*] `, I allowed incoming connections to the port I use (9595), my phone is connected to the same router that my laptop, I run `python manage.py runserver 0.0.0.0:9595`, I check ipaddres with `ipconfig` and then on my mobile in the browser :9595 and it still doesn't work... What else can be a problem? (When I check IP address on my phone it is a bit different than on my laptop, but it is connected to the same router - can it be a problem?) – Sygol Apr 05 '20 at 11:02
37

I had to add this line to settings.py in order to make it work (otherwise it showed an error when accessed from another computer)

ALLOWED_HOSTS = ['*']

then ran the server with:

python manage.py runserver 0.0.0.0:9595

Also ensure that the firewall allows connections to that port

Bonfix Ngetich
  • 1,087
  • 11
  • 15
15

Pick one or more from:

  • Your application isn't successfully listening on the intended IP:PORT
    • Because you haven't configured it successfully
    • Because the user doesn't have permission to
  • Your application is listening successfully on the intended IP:PORT, but clients can't reach it because
    • The server local iptables prevents it.
    • A firewall prevents it.

So, you can check that your application is listening successfully by running lsof -i as root on the machine and look for a python entry with the corresponding port you've specified.

Non-root users generally cannot bind to ports < 1024.

You'll need to look at iptables -nvL to see if there's a rule that would prevent access to the ip:port that you are trying to bind your application to.

If there is an upstream firewall and you don't know much about it, you'll need to talk to your network administrators.

MattH
  • 37,273
  • 11
  • 82
  • 84
  • can you explain `you haven't configured it successfully`? – Chillar Anand Jul 20 '15 at 06:39
  • @ChillarAnand: simply that you check that the `django runserver` is actually listening where you intend it to be, e.g. using `lsof`. – MattH Jul 20 '15 at 08:53
  • @ChillarAnand I went through the steps you instructed. Using `lsof` I see a python entry for my port. But my link i.e myip:8000 is not responding. I know its late but any suggestions will help. – WaterRocket8236 Feb 18 '17 at 07:24
14

just do this:

python manage.py runserver 0:8000

by the above command you are actually binding it to the external IP address. so now when you access your IP address with the port number, you will be able to access it in the browser without any problem.

just type in the following in the browser address bar:

<your ip address>:8000

eg:

192.168.1.130:8000

you may have to edit the settings.py add the following in the settings.py in the last line:

ALLOWED_HOSTS = ['*']

hope this will help...

Joish
  • 1,458
  • 18
  • 23
7

For AWS users.

I had to use the following steps to get there.

1) Ensure that pip and django are installed at the sudo level

  • sudo apt-get install python-pip
  • sudo pip install django

2) Ensure that security group in-bound rules includ http on port 80 for 0.0.0.0/0

  • configured through AWS console

3) Add Public IP and DNS to ALLOWED_HOSTS

  • ALLOWED_HOSTS is a list object that you can find in settings.py
  • ALLOWED_HOSTS = ["75.254.65.19","ec2-54-528-27-21.compute-1.amazonaws.com"]

4) Launch development server with sudo on port 80

  • sudo python manage.py runserver 0:80

Site now available at either of the following (no need for :80 as that is default for http):

  • [Public DNS] i.e. ec2-54-528-27-21.compute-1.amazonaws.com
  • [Public IP] i.e 75.254.65.19
datavoredan
  • 3,536
  • 9
  • 32
  • 48
3

I'm going to add this here:

  1. sudo python manage.py runserver 80

  2. Go to your phone or computer and enter your computers internal IP (e.g 192.168.0.12) into the browser.

At this point you should be connected to the Django server.

This should also work without sudo:

python manage.py runserver 0.0.0.0:8000
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
James111
  • 15,378
  • 15
  • 78
  • 121
3

open terminal
type : ifconfig
check results of ifconfig command
use the inet IP .. should look like this.. 192.168.1.121 or similar 192.168.x.x.

now runserver like you normally do but this time specify the inet IP
python3 manage.py runserver 192.168.x.x:8000 (replace the x with your inet)
also
on settings.py
ALLOWED_HOSTS = ['*']

  • This is the answer that finally worked for me. If you're on Windows like me, run `ipconfig` instead of `ifconfig`. You'll probably have a few that match the 192.168.x.x pattern. For me the one that worked was the one under `Wireless LAN adapter Wi-Fi:` labeled `IPv4 Address`. – GlenVaughan Mar 04 '22 at 04:36
2

UPDATED 2020 TRY THIS WAY

python manage.py runserver yourIp:8000

ALLOWED_HOSTS = ["*"]
Kashif
  • 1,364
  • 17
  • 21
2

You need just to allow any hosts : settings.py :

ALLOWED_HOST = ['*']

Run your server :

python3 manage.py runserver 0.0.0.0:8000

If you want to connect android app just add internet permission in AndroidManifest It's work for me ;)

Abidi Mohamed
  • 723
  • 7
  • 7
1

install ngrok in terminal

sudo apt-get install -y ngrok-client

after that run:

ngrok http 8000
or 
ngrok http example.com:9000 
0

If you are using Docker you need to make sure ports are exposed as well