Django 应用程序的 Procfile 中应该包含哪些内容?

What should go in my Procfile for a Django application?

对于 Heroku 上的 Django 应用程序,我的 Procfile 中应该包含哪些内容?

我试过了:

web: python appname.py

因为我为 python 个应用找到了类似的示例。

除了我可能需要使用 gunicorn 而不是 python 之外,进一步的搜索并没有使事情变得更清楚。我发现各种建议各种格式的帖子,例如:

web gunicorn
web:gunicorn
web: gunicorn

我不知道 gunicorn 之后应该是什么,有些帖子有编程语言,有些帖子有 IP 地址,有些帖子还有其他各种东西。

有人建议运行:

heroku ps:scale web=1

但这会导致错误:

Scaling dynos... !
 !    Couldn't find that process type (web).

我只是没有头绪,不知道该去哪里。

自发布以来,我观看了一些关于此的视频并尝试了:

web: gunicorn appname.wsgi

在我的 Procfile 中,但它仍然不起作用,仍然导致:

at=error code=H14 desc="No web processes running"

Heroku 的 Procfile 格式非常简单。 As described in the documentation:

A Procfile declares its process types on individual lines, each with the following format:

<process type>: <command>

可以看到进程类型后面应该有一个冒号,所以

web gunicorn

您问题中的示例无法正常工作。您需要以 web:.

开始该行

<command> indicates the command that every dyno of the process type should execute on startup, such as rake jobs:work

对于 Django,在开发中您通常会使用 python manage.py runserver 到 运行 应用程序,因此 Django 的合理尝试是

web: python manage.py runserver

这应该可以,但是 it's not appropriate for production work:

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)

相反,您应该在生产中使用生产级 Web 服务器。 Gunicorn 是一个常见的选择,you can run your Django application with Gunicorn like so:

gunicorn myproject.wsgi

将所有这些放在一起,Heroku 上的 Django Procfile 可能看起来像

web: gunicorn myproject.wsgi

其中 myproject 是您的 Django 项目的名称。这是 Django 应用程序的 exactly what Heroku's documentation suggests

请注意,您必须将 Gunicorn 添加到项目依赖项中,这样 Heroku 才会安装它。我还建议在本地安装它,这样您就可以使用 heroku local 以更类似于 Heroku 生产环境的方式在您的开发机器上测试您的应用程序。

heroku ps:scale 用于更改您已经定义的过程类型的测功机的数量和类型。它与定义那些进程类型无关。这就是你的 Procfile 的用途。

您需要 3 个文件才能成功将 Django 应用程序部署到 Heroku。

  • Procfile
  • runtime.txt
  • requirements.txt

这 2 个模块应该在您的 requirements.txt

Procfile中输入

release: python manage.py migrate
web: gunicorn yourprojectname.wsgi
  • 第一行解释了部署的类型 release 这意味着生产版本,然后是 migrate 我想你知道会做什么。

  • 第二行说明Gunicorn是the Python WSGI HTTP Server for UNIX

  • runtime.txt 中键入您的 python 版本,如下所示

    python-3.9.6

您可以使用此终端命令 python --version

查看您拥有哪个 python 版本
  • 最后你需要 requirements.txt,你可以用 pip freeze > requirements.txt 生成它,同时激活 venv 以防你在虚拟环境中操作。

项目启动步骤,最简单的方法:

  • 停用你的venv以防万一
  • 转到 heroku 仪表板
  • 创建一个应用程序并选择一个有意义的名称和免费计划,这样您就可以 运行 免费雇用 1 名员工
  • 复制其 url: herokuappname.herokuapp.com 然后在 settings.py 中粘贴到 ALLOWED_HOSTS = ['herokuappname.herokuapp.com']
  • 设置DEBUG = False,生产环境中不允许调试
  • 然后在您的终端导航到项目文件夹 并一一输入以下命令
  • Heroku login # 登录你的 Heroku 账户
  • heroku git:remote -a yourAppname # 连接到您已经创建的应用程序
  • git init # 初始化你的 repo
  • git add . # 将所有项目的文件添加到初始化的 repo
  • git commit -m "first push"#提交
  • git push heroku master # 将项目文件推送到远程 Heroku 应用程序仓库

部署成功后,输入-enter

  • heroku logout # 注销

您要使用哪个存储空间?

因为 Heroku 不托管静态文件。

  • 您可以免费使用Azure storage。完整教程here

这是我的 project 模型部署到 Heroku 与 azure 存储,你会找到所有需要的细节。