Skip to content

在 Heroku 上托管 n8n#

¥Hosting n8n on Heroku

本托管指南将向你展示如何在 Heroku 上自行托管 n8n。使用:

¥This hosting guide shows you how to self-host n8n on Heroku. It uses:

  • Docker Compose 用于创建和定义应用组件及其协同工作方式。

¥Docker Compose to create and define the application components and how they work together.

¥Heroku's PostgreSQL service to host n8n's data storage.

  • 一个“部署到 Heroku”按钮,只需单击一下即可完成部署,且配置量极小。

¥A Deploy to Heroku button offering a one click, with minor configuration, deployment.

Self-hosting knowledge prerequisites

Self-hosting n8n requires technical knowledge, including:

  • Setting up and configuring servers and containers
  • Managing application resources and scaling
  • Securing servers and applications
  • Configuring n8n

n8n recommends self-hosting for expert users. Mistakes can lead to data loss, security issues, and downtime. If you aren't experienced at managing servers, n8n recommends n8n Cloud.

Latest and Next versions

n8n releases a new minor version most weeks. The latest version is for production use. next is the most recent release. You should treat next as a beta: it may be unstable. To report issues, use the forum.

Current latest: 1.122.5 Current next: 1.123.2

使用部署模板创建 Heroku 项目#

¥Use the deployment template to create a Heroku project

将 n8n 部署到 Heroku 的最快方法是使用“部署到 Heroku”按钮:

¥The quickest way to get started with deploying n8n to Heroku is using the Deploy to Heroku button:

这将在 Heroku 上打开“创建新应用”页面。为项目设置名称,并选择项目部署区域。

¥This opens the Create New App page on Heroku. Set a name for the project, and choose the region to deploy the project to.

配置环境变量#

¥Configure environment variables

Heroku 会预先填充 app.json 文件中 env 部分定义的配置选项,该文件还会设置 n8n 使用的环境变量的默认值。

¥Heroku pre-fills the configuration options defined in the env section of the app.json file, which also sets default values for the environment variables n8n uses.

你可以根据需要更改这些值。你必须更改以下值:

¥You can change any of these values to suit your needs. You must change the following values:

¥N8N_ENCRYPTION_KEY, which n8n uses to encrypt user account details before saving to the database.

  • WEBHOOK_URL 应与你创建的应用名称匹配,以确保 Webhook 具有正确的 URL。

¥WEBHOOK_URL should match the application name you create to ensure that webhooks have the correct URL.

部署 n8n#

¥Deploy n8n

选择“部署应用”。

¥Select Deploy app.

Heroku 构建并部署应用后,会提供“管理应用”或“查看应用”的链接。

¥After Heroku builds and deploys the app it provides links to Manage App or View the application.

Heroku and DNS

请参阅 Heroku 文档 了解如何将你的域名连接到 Heroku 应用。

¥Refer to the Heroku documentation to find out how to connect your domain to a Heroku application.

更改部署模板#

¥Changing the deployment template

你可以通过 fork repository 并从你的 fork 进行部署来更改部署模板。

¥You can make changes to the deployment template by forking the repository and deploying from you fork.

Dockerfile#

默认情况下,Dockerfile 会拉取最新的 n8n 镜像。如果你想使用其他版本或固定版本,请更新 Dockerfile 顶部的镜像标签。

¥By default the Dockerfile pulls the latest n8n image, if you want to use a different or fixed version, then update the image tag on the top line of the Dockerfile.

Heroku 和端口暴露#

¥Heroku and exposing ports

Heroku 不允许基于 Docker 的应用使用 EXPOSE 命令定义暴露端口。Heroku 提供了一个 PORT 环境变量,并在应用运行时动态填充该变量。entrypoint.sh 文件会覆盖默认的 Docker 镜像命令,转而设置 Heroku 提供的端口变量。然后,你可以通过 Web 浏览器访问 n8n 的 80 端口。

¥Heroku doesn't allow Docker-based applications to define an exposed port with the EXPOSE command. Instead, Heroku provides a PORT environment variable that it dynamically populates at application runtime. The entrypoint.sh file overrides the default Docker image command to instead set the port variable that Heroku provides. You can then access n8n on port 80 in a web browser.

Docker limitations with Heroku

有关在 Heroku 中使用 Docker 的限制的更多详细信息,请参阅 阅读指南

¥Read this guide for more details on the limitations of using Docker with Heroku.

配置 Heroku#

¥Configuring Heroku

heroku.yml 文件定义了你要在 Heroku 上创建的应用。它由两部分组成:

¥The heroku.yml file defines the application you want to create on Heroku. It consists of two sections:

  • setup > addons 定义要使用的 Heroku 插件。在这种情况下,使用 PostgreSQL 数据库插件。

¥setup > addons defines the Heroku addons to use. In this case, the PostgreSQL database addon.

  • build 部分定义了 Heroku 如何构建应用。在这种情况下,它使用 Docker 构建包基于提供的 Dockerfile 构建 web 服务。

¥The build section defines how Heroku builds the application. In this case it uses the Docker buildpack to build a web service based on the supplied Dockerfile.

下一步步骤#

¥Next steps