在 Hetzner 云上托管 n8n(Hosting n8n on Hetzner cloud)#
本托管指南向你展示如何在 Hetzner 云服务器上自托管 n8n。它使用了:
🌐 This hosting guide shows you how to self-host n8n on a Hetzner cloud server. It uses:
- Caddy(一个反向代理)以允许从互联网访问服务器。
- Docker Compose 用于创建和定义应用程序组件及它们如何协同工作。
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.
Stable and Beta versions
n8n releases a new minor version most weeks. The stable version is for production use. beta is the most recent release. The beta version may be unstable. To report issues, use the forum.
Current stable: 2.4.7
Current beta: 2.6.2
创建服务器(Create a server)#
- 登录到 Hetzner 云控制台。
- 选择要托管服务器的项目,或通过选择 + 新建项目 来创建新项目。
- 在你想要添加它的项目块上选择 + 创建服务器。
你可以更改大多数设置以满足你的需求,但由于本指南使用 Docker 来运行应用,在 Image 部分,请从 APPS 标签中选择 "Docker CE"。
🌐 You can change most of the settings to suit your needs, but as this guide uses Docker to run the application, under the Image section, select "Docker CE" from the APPS tab.
类型
在创建服务器时,Hetzner 会要求你选择一个套餐。对于大多数使用需求,CPX11 类型就足够了。
SSH 密钥
Hetzner 允许你在 SSH 和基于密码的身份验证之间进行选择。SSH 更加安全。本指南的其余部分假设你正在使用 SSH。
登录到你的服务器(Log in to your server)#
本指南的其余部分需要你使用带有 SSH 的终端登录服务器。更多信息,请参阅 使用 SSH/rsync/BorgBackup 访问。你可以在项目中的服务器列表中找到公网 IP。
🌐 The rest of this guide requires you to log in to the server using a terminal with SSH. Refer to Access with SSH/rsync/BorgBackup for more information. You can find the public IP in the listing of the servers in your project.
安装 Docker Compose(Install Docker Compose)#
Hetzner Docker 应用镜像没有安装 Docker Compose。请使用以下命令进行安装:
🌐 The Hetzner Docker app image doesn't have Docker compose installed. Install it with the following commands:
1 2 | |
克隆配置库(Clone configuration repository)#
Docker Compose、n8n 和 Caddy 需要一系列文件夹和配置文件。你可以从 这个仓库 克隆到服务器的 root 用户文件夹中。接下来的步骤将告诉你需要修改哪个文件以及需要做哪些更改。
🌐 Docker Compose, n8n, and Caddy require a series of folders and configuration files. You can clone these from this repository into the root user folder of the server. The following steps will tell you which file to change and what changes to make.
使用以下命令克隆存储库:
🌐 Clone the repository with the following command:
1 | |
并将目录切换到你克隆的存储库的根目录:
🌐 And change directory to the root of the repository you cloned:
1 | |
默认文件夹和文件(Default folders and files)#
主机操作系统(服务器)将你创建的两个文件夹复制到 Docker 容器中,以便 Docker 可以使用它们。这两个文件夹是:
🌐 The host operating system (the server) copies the two folders you created to Docker containers to make them available to Docker. The two folders are:
caddy_config:保存 Caddy 配置文件。local_files:用于存储您通过 n8n 上传或添加的文件的文件夹。
创建 Docker 卷(Create Docker volume)#
为了在重启之间保留 Caddy 缓存并加快启动时间,请创建一个 Docker 卷,Docker 会在重启之间重复使用它:
🌐 To persist the Caddy cache between restarts and speed up start times, create a Docker volume that Docker reuses between restarts:
1 | |
为 n8n 数据创建 Docker 卷:
🌐 Create a Docker volume for the n8n data:
1 | |
设置 DNS(Set up DNS)#
n8n 通常在子域上运行。请在您的提供商处为该子域创建一个 DNS 记录,并将其指向服务器的 IP 地址。具体步骤取决于您的 DNS 提供商,但通常您需要为 n8n 子域创建一个新的 “A” 记录。DigitalOcean 提供了DNS 术语、组件和概念简介。
🌐 n8n typically operates on a subdomain. Create a DNS record with your provider for the subdomain and point it to the IP address of the server. The exact steps for this depend on your DNS provider, but typically you need to create a new "A" record for the n8n subdomain. DigitalOcean provide An Introduction to DNS Terminology, Components, and Concepts.
打开端口(Open ports)#
n8n 作为 Web 应用运行,因此服务器需要允许通过 80 端口接收非安全流量,并通过 443 端口接收安全流量。
🌐 n8n runs as a web application, so the server needs to allow incoming access to traffic on port 80 for non-secure traffic, and port 443 for secure traffic.
运行以下两条命令,在服务器的防火墙中打开以下端口:
🌐 Open the following ports in the server's firewall by running the following two commands:
1 2 | |
配置 n8n(Configure n8n)#
n8n 需要设置一些环境变量,以传递给在 Docker 容器中运行的应用程序。示例 .env 文件包含占位符,你需要用自己的值来替换这些占位符。
🌐 n8n needs some environment variables set to pass to the application running in the Docker container. The example .env file contains placeholders you need to replace with values of your own.
使用以下命令打开文件:
🌐 Open the file with the following command:
1 | |
该文件包含内联注释,可帮助你了解需要更改的内容。
🌐 The file contains inline comments to help you know what to change.
有关 n8n 环境变量的详细信息,请参阅 环境变量。
🌐 Refer to Environment variables for n8n environment variables details.
Docker Compose 文件(The Docker Compose file)#
Docker Compose 文件(docker-compose.yml)定义了应用所需的服务,在本例中是 Caddy 和 n8n。
🌐 The Docker Compose file (docker-compose.yml) defines the services the application needs, in this case Caddy and n8n.
- Caddy 服务定义定义了它使用的端口以及要复制到容器的本地卷。
- n8n 服务定义中说明了它使用的端口、n8n 运行所需的环境变量(其中一些在
.env文件中定义),以及需要复制到容器的卷。
Docker Compose 文件使用 .env 文件中设置的环境变量,因此你不需要更改其内容,但如果想查看,可以运行以下命令:
🌐 The Docker Compose file uses the environment variables set in the .env file, so you shouldn't need to change it's content, but to take a look, run the following command:
1 | |
配置 Caddy(Configure Caddy)#
Caddy 需要知道它应该提供哪些域名,以及向外部开放哪个端口。编辑 caddy_config 文件夹中的 Caddyfile 文件。
🌐 Caddy needs to know which domains it should serve, and which port to expose to the outside world. Edit the Caddyfile file in the caddy_config folder.
1 | |
将占位符子域更改为你的子域。如果你按照步骤将子域命名为 n8n,那么你的完整域名类似于 n8n.example.com。在 reverse_proxy 设置中的 n8n 告诉 Caddy 使用 docker-compose.yml 文件中定义的服务配置:
🌐 Change the placeholder subdomain to yours. If you followed the steps to name the subdomain n8n, your full domain is similar to n8n.example.com. The n8n in the reverse_proxy setting tells Caddy to use the service definition defined in the docker-compose.yml file:
1 2 3 4 5 | |
启动 Docker Compose(Start Docker Compose)#
使用以下命令启动 n8n 和 Caddy:
🌐 Start n8n and Caddy with the following command:
1 | |
可能需要几分钟时间。
🌐 This may take a few minutes.
测试你的设置(Test your setup)#
在浏览器中,打开先前定义的子域名和域名组成的 URL。输入先前定义的用户名和密码,然后你应该能够访问 n8n。
🌐 In your browser, open the URL formed of the subdomain and domain name defined earlier. Enter the user name and password defined earlier, and you should be able to access n8n.
停止 n8n 和 Caddy(Stop n8n and Caddy)#
你可以使用以下命令停止 n8n 和 Caddy:
🌐 You can stop n8n and Caddy with the following command:
1 | |
正在更新(Updating)#
If you run n8n using a Docker Compose file, follow these steps to update n8n:
1 2 3 4 5 6 7 8 9 10 11 | |
下一步步骤(Next steps)#
- Learn more about configuring and scaling n8n.
- Or explore using n8n: try the Quickstarts.