Skip to content

在 DigitalOcean 上托管 n8n(Hosting n8n on DigitalOcean)#

本托管指南将向你展示如何在 DigitalOcean 的虚拟服务器上自托管 n8n。它使用了:

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

  • Caddy(一个反向代理)允许从互联网访问 Droplet。Caddy 还会自动为你的 n8n 实例创建和管理 SSL/TLS 证书。
  • 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

创建 Droplet(Create a Droplet)#

  1. 登录到 DigitalOcean。
  2. 选择托管 Droplet 的项目,或创建新项目
  3. 在你的项目中,从 管理 菜单中选择 Droplets
  4. 使用Marketplace选项卡中提供的 Docker 镜像 创建一个新的 Droplet。

Droplet 资源

在创建 Droplet 时,DigitalOcean 会要求你选择一个计划。对于大多数使用需求,基础共享 CPU 套餐就足够了。

SSH 密钥或密码

DigitalOcean 让你可以在 SSH 密钥和密码认证之间进行选择。SSH 密钥被认为更安全。

登录到你的 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 用户身份登录
  2. 创建新用户:

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

  4. 授予新用户管理权限:

    1
    usermod -aG sudo <username>
    
    You can now run commands with superuser privileges by using sudo before the command.

  5. 按照以下步骤为新用户设置 SSH:添加公钥认证

  6. 退出服务器。
  7. 使用 SSH 以新用户身份登录。

克隆配置库(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 配置文件。
  • local_files:用于存储您通过 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 记录,并将其指向 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 服务定义定义了它使用的端口以及要复制到容器的本地卷。
  • 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
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.comreverse_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)#