Wael
Wael

Reputation: 87

Cannot Connect to Django from outside the Local Server

I built a basic Django project and app on Centos7, but I cannot access it externally. Check the configuration below.

[root@pytoncentos7 myproject]# cat settings.py | grep ALLOWED
ALLOWED_HOSTS = ['*']

And I run my server as below.

[root@pytoncentos7 myproject]#  python3.6 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
May 24, 2018 - 15:03:04
Django version 2.0.5, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Also, the firewall is not running

[root@pytoncentos7 myproject]# firewall-cmd --state
not running

Below are the list of my ports.

[root@pytoncentos7 myproject]# netstat -lptun
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
PID/Program name
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      
1643/python3.6
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      
876/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      
982/master
tcp6       0      0 :::3306                 :::*                    LISTEN      
986/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      
876/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      
982/master
udp        0      0 127.0.0.1:323           0.0.0.0:*                           
636/chronyd
udp6       0      0 ::1:323                 :::*                                
636/chronyd

Also, when I try to connect from the outside to port 8000, it fails !!. But ofcourse I can telnet locally. And at the same time, I have Apache running on 80 and reachable from internal/external, but when I disable Apache and run Django on port 80, I can still connect locally but not external

The Django server is just a VM running on my local computer

Upvotes: 3

Views: 9498

Answers (2)

Kal
Kal

Reputation: 1717

runserver without specifying an IP will only listen to the loopback interface.

You need python3.6 manage.py runserver 123.123.123.123 (assuming the IP of your network interface is "123.123.123.123").

Upvotes: 2

ryan
ryan

Reputation: 1463

From https://docs.djangoproject.com/en/2.0/ref/django-admin/#runserver

Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0 or :: (with IPv6 enabled).

You must run with the command python3.6 manage.py runserver 0.0.0.0:8000

Then figure out the VM's local IP and navigate to it

http://192.168.0.XXX:8000   <--- Use local IP of VM

Upvotes: 12

Related Questions