Skip to content

在 DigitalOcean 上托管 n8n#

¥Hosting n8n on DigitalOcean

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

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

  • Caddy(反向代理)允许从互联网访问 Droplet。Caddy 还会自动为你的 n8n 实例创建和管理 SSL/TLS 证书。

¥Caddy (a reverse proxy) to allow access to the Droplet from the internet. Caddy will also automatically create and manage SSL / TLS certificates for your n8n instance.

  • 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

创建 Droplet#

¥Create a Droplet

  1. 登录 指向 DigitalOcean。

¥Log in to DigitalOcean. 2. 选择用于托管 Droplet 的项目,或选择 创建新项目

¥Select the project to host the Droplet, or create a new project. 3. 在你的项目中,从“管理”菜单中选择“Droplets”。

¥In your project, select Droplets from the Manage menu. 4. 创建新的 Droplet 使用 Marketplace 选项卡中提供的 Docker 镜像

¥Create a new Droplet using the Docker image available on the Marketplace tab.

Droplet resources

创建 Droplet 时,DigitalOcean 会要求你选择一个套餐。对于大多数使用级别,基本的共享 CPU 方案就足够了。

¥When creating the Droplet, DigitalOcean asks you to choose a plan. For most usage levels, a basic shared CPU plan is enough.

SSH key or Password

DigitalOcean 允许你选择基于 SSH 密钥或密码的身份验证。SSH 密钥被认为更安全。

¥DigitalOcean lets you choose between SSH key and password-based authentication. SSH keys are considered more secure.

登录到你的 Droplet 并创建新用户#

¥Log in to your Droplet and create new user

本指南的其余部分要求你使用 SSH 终端登录到 Droplet。有关更多信息,请参阅 如何使用 SSH 连接到 Droplet?

¥The rest of this guide requires you to log in to the Droplet using a terminal with SSH. Refer to How to Connect to Droplets with SSH for more information.

你应该创建一个新用户,以避免以 root 用户身份工作。

¥You should create a new user, to avoid working as the root user:

  1. 以 root 用户身份登录

¥Log in as root. 2. 创建新用户:

¥Create a new user:

1
adduser <username>
3. 请按照 CLI 中的提示完成用户创建。

¥Follow the prompts in the CLI to finish creating the user. 4. 授予新用户管理权限:

¥Grant the new user administrative privileges:

1
usermod -aG sudo <username>
现在你可以通过在命令前加上 sudo 来以超级用户权限运行命令。

¥You can now run commands with superuser privileges by using sudo before the command. 5. 请按照以下步骤为新用户设置 SSH:添加公钥身份验证

¥Follow the steps to set up SSH for the new user: Add Public Key Authentication. 6. 退出服务器。

¥Log out of the droplet. 7. 使用 SSH 以新用户身份登录。

¥Log in using SSH as the new user.

克隆配置库#

¥Clone configuration repository

Docker Compose、n8n 和 Caddy 需要一系列文件夹和配置文件。你可以将这些工作流从 此代码库 克隆到 Droplet 上已登录用户的主文件夹。以下步骤将告诉你要更改哪个文件以及需要进行哪些更改。

¥Docker Compose, n8n, and Caddy require a series of folders and configuration files. You can clone these from this repository into the home folder of the logged-in user on your Droplet. The following steps will tell you which file to change and what changes to make.

使用以下命令克隆存储库:

¥Clone the repository with the following command:

1
git clone https://github.com/n8n-io/n8n-docker-caddy.git

并将目录切换到你克隆的存储库的根目录:

¥And change directory to the root of the repository you cloned:

1
cd n8n-docker-caddy

默认文件夹和文件#

¥Default folders and files

主机操作系统(DigitalOcean Droplet)会将你创建的两个文件夹复制到 Docker 容器中,以便 Docker 可以访问它们。两个文件夹是:

¥The host operating system (the DigitalOcean Droplet) 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 volumes

为了在重启后保留 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
sudo docker volume create caddy_data

为 n8n 数据创建 Docker 卷:

¥Create a Docker volume for the n8n data:

1
sudo docker volume create n8n_data

设置 DNS#

¥Set up DNS

n8n 通常在子域名上运行。使用你的 DNS 提供商为子域名创建一条 DNS 记录,并将其指向 Droplet 的 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 Droplet. 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 应用运行,因此 Droplet 需要允许通过 80 端口接收非安全流量,并通过 443 端口接收安全流量。

¥n8n runs as a web application, so the Droplet needs to allow incoming access to traffic on port 80 for non-secure traffic, and port 443 for secure traffic.

运行以下两条命令,在 Droplet 的防火墙中打开以下端口:

¥Open the following ports in the Droplet's firewall by running the following two commands:

1
2
sudo ufw allow 80
sudo ufw allow 443

配置 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
nano .env

该文件包含内联注释,可帮助你了解需要更改的内容。

¥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
nano docker-compose.yml

配置 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
nano caddy_config/Caddyfile

将占位符域名更改为你自己的域名。如果你按照步骤将子域名命名为 n8n,则你的完整域名与 n8n.example.com 类似。reverse_proxy 设置中的 n8n 指示 Caddy 使用 docker-compose.yml 文件中定义的服务定义:

¥Change the placeholder domain 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
n8n.<domain>.<suffix> {
    reverse_proxy n8n:5678 {
      flush_interval -1
    }
}

如果你之前使用的是 automate.example.com,你的 Caddyfile 可能如下所示:

¥If you were to use automate.example.com, your Caddyfile may look something like:

1
2
3
4
5
automate.example.com {
    reverse_proxy n8n:5678 {
      flush_interval -1
    }
}

启动 Docker Compose#

¥Start Docker Compose

使用以下命令启动 n8n 和 Caddy:

¥Start n8n and Caddy with the following command:

1
sudo docker compose up -d

可能需要几分钟时间。

¥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
sudo docker compose stop

正在更新#

¥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
# Navigate to the directory containing your docker compose file
cd </path/to/your/compose/file/directory>

# Pull latest version
docker compose pull

# Stop and remove older version
docker compose down

# Start the container
docker compose up -d

下一步步骤#

¥Next steps