在 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(反向代理)允许从互联网访问服务器。
¥Caddy (a reverse proxy) to allow access to the Server from the internet.
- Docker Compose 用于创建和定义应用组件及其协同工作方式。
¥Docker Compose to create and define the application components and how they work together.
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
创建服务器#
¥Create a server
- 登录 用于访问 Hetzner 云控制台。
¥Log in to the Hetzner Cloud Console. 2. 选择用于托管服务器的项目,或选择“+ 新建项目”创建新项目。
¥Select the project to host the server, or create a new project by selecting + NEW PROJECT. 3. 在要添加服务器的项目磁贴上,选择“+ 创建服务器”。
¥Select + CREATE SERVER on the project tile you want to add it to.
你可以根据需要更改大多数设置,但由于本指南使用 Docker 运行应用,因此请在“镜像”部分下的“应用”选项卡中选择 "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.
Type
创建服务器时,Hetzner 会要求你选择一个套餐。对于大多数使用级别,CPX11 类型就足够了。
¥When creating the server, Hetzner asks you to choose a plan. For most usage levels, the CPX11 type is enough.
SSH keys
Hetzner 允许你选择 SSH 或基于密码的身份验证。SSH 更安全。本指南的其余部分假设你使用 SSH。
¥Hetzner lets you choose between SSH and password-based authentication. SSH is more secure. The rest of this guide assumes you are using 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 需要一系列文件夹和配置文件。你可以将这些工作流从 此代码库 克隆到服务器的根用户文件夹。以下步骤将告诉你要更改哪个文件以及需要进行哪些更改。
¥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 配置文件。
¥caddy_config: Holds the Caddy configuration files.
local_files:用于存放你使用 n8n 上传或添加的文件的文件夹。
¥local_files: A folder for files you upload or add using 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 提供商为子域名创建一条 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 服务定义定义了它使用的端口以及要复制到容器的本地卷。
¥The Caddy service definition defines the ports it uses and the local volumes to copy to the containers.
- n8n 服务定义定义了它使用的端口、n8n 运行所需的环境变量(部分在
.env文件中定义)以及需要复制到容器的卷。
¥The n8n service definition defines the ports it uses, the environment variables n8n needs to run (some defined in the .env file), and the volumes it needs to copy to the containers.
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.