作者:_卟苴 | 来源:互联网 | 2022-12-16 11:20
我在Centos7上构建了一个基本的Django项目和应用程序,但无法从外部访问它。检查以下配置。
[root@pytoncentos7 myproject]# cat settings.py | grep ALLOWED
ALLOWED_HOSTS = ['*']
然后按以下方式运行服务器。
[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.
另外,防火墙没有运行
[root@pytoncentos7 myproject]# firewall-cmd --state
not running
以下是我的端口列表。
[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
另外,当我尝试从外部连接到端口8000时,它将失败!但是我当然可以在本地进行远程登录。同时,我的Apache运行在80上,并且可以从内部/外部访问,但是当我禁用Apache并在端口80上运行Django时,我仍然可以在本地连接,但不能在外部连接
Django服务器只是在我的本地计算机上运行的VM
1> ryan..:
从https://docs.djangoproject.com/en/2.0/ref/django-admin/#runserver
请注意,无法从网络上的其他计算机访问默认IP地址127.0.0.1。为了使您的开发服务器对网络上的其他计算机可见,请使用其自己的IP地址(例如192.168.2.1)或0.0.0.0或::(启用IPv6)。
您必须使用命令运行 python3.6 manage.py runserver 0.0.0.0:8000
然后找出虚拟机的本地IP并导航到它
http://192.168.0.XXX:8000 <--- Use local IP of VM