【问题标题】:How to run Django Web app on port 443 and 80 linux - Ubuntu or Raspberry PI?如何在端口 443 和 80 linux - Ubuntu 或 Raspberry PI 上运行 Django Web 应用程序?
【发布时间】:2018-09-13 03:13:41
【问题描述】:

我对 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。我该如何调试以及正确的解决方案是什么?

【问题讨论】:

  • 假设您想托管您的 django 应用程序,您应该搜索托管 django 应用程序 [使用 nginx、gunicorn、supervisord]。这方面的文章很多

标签: python django ubuntu


【解决方案1】:

您不会直接与 Django 应用程序对话。虽然 Django 通过 runserver 命令提供了一个简单的开发服务器,但它只用于开发工作。

你想要的是设置一个 WSGI 服务器来运行你的应用程序和一个 Web 服务器来接受实际的用户请求并将它们代理到 WSGI。常用的WSGI服务器(不用担心WSGI是什么)是gunicorn和uWSGI。两者都可以使用 PIP 安装,您不必以与您的应用相同的用户身份安装它们。 Gunicorn 更容易使用,所以我会推荐它。现在最常见的 Web 服务器是 Nginx。

此外,您应该将您的应用程序打包到一个虚拟环境中,这样您就可以将它与所有依赖项打包在一起,而无需依赖安装了所有东西的特定系统。

这里是关于如何执行此操作的有点过时的指南。它应该是最准确的,是一个很好的起点 https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04

【讨论】:

    【解决方案2】:

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

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

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

    python manage.py runserver 0.0.0.0:80 
    

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

    【讨论】:

      猜你喜欢
      • 2014-08-16
      • 2019-06-23
      • 2020-01-26
      • 1970-01-01
      • 2014-01-31
      • 2018-01-12
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多