首页 > 解决方案 > 如何在端口 443 和 80 linux - Ubuntu 或 Raspberry PI 上运行 Django Web 应用程序?

问题描述

我对 Django 很陌生。我认为我无法将 django 应用程序作为 sudo 运行,因为所有 pip 相关模块都是为用户安装的,而不是为 sudo 用户安装的。所以,这是一个基本问题,比如我如何运行可以监听端口 80 和端口 443 的 django 应用程序。

所以,到目前为止,我已经尝试了以下选项 - 即预路由 - NAT

我使用以下命令运行我的应用程序 -

$python manage.py runserver 
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

September 13, 2018 - 03:04:41
Django version 2.1.1, using settings 'WebBlogger.settings'
Starting development server at http://127.0.0.1:8000/

接下来,这是我的 iptables 设置,但对我没有任何作用

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000

$sudo iptables -t nat --line-numbers -n -L
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL
2    REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 redir ports 8000

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    DOCKER     all  --  0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    MASQUERADE  all  --  172.17.0.0/16        0.0.0.0/0           
2    RETURN     all  --  192.168.122.0/24     224.0.0.0/24        
3    RETURN     all  --  192.168.122.0/24     255.255.255.255     
4    MASQUERADE  tcp  --  192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
5    MASQUERADE  udp  --  192.168.122.0/24    !192.168.122.0/24     masq ports: 1024-65535
6    MASQUERADE  all  --  192.168.122.0/24    !192.168.122.0/24    

Chain DOCKER (2 references)
num  target     prot opt source               destination         
1    RETURN     all  --  0.0.0.0/0            0.0.0.0/0           

我做了 http:// 并且我看到连接被拒绝。我不知道如何调试 NAT 的东西,无论它是否真的命中 NAT。我该如何调试以及正确的解决方案是什么?

标签: pythondjangoubuntu

解决方案


在理想世界中,您将需要一个网络服务来与您的 Django 对话。

Web server (port 80/443) -> gunicorn (wigs) -> Django (port 8000)

如果你只是想让 Django 开发服务器在 80 上运行,那么试试

python manage.py runserver 0.0.0.0:80 

并确保没有其他进程正在使用端口 80。


推荐阅读