按照年初计划对于AI领域下半年开始多在应用层面做一些研究。很早就有不少群里在推荐n8n,也试过用免费方案搭建过一次,免费方案是利用Huggingface的免费资源(Workspace)+Supabase数据库完成的搭建,但一段时间不使用资源就会被回收,我由于不太关注甚至Supabase数据库都遭到了暂停。
于是索性手动搭建一套,用于日后研究。首先数据库还是选用PostgreSQL,考虑到有可能会使用向量功能,所以会开启pgvector插件。
系统准备
1.首先系统包更新
1 |
sudo apt update && sudo apt upgrade -y |
2.添加PostgreSQL GPG Key用于校验:
1 2 |
sudo apt install curl ca-certificates -y sudo curl -o /etc/apt/trusted.gpg.d/postgresql.gpg https://www.postgresql.org/media/keys/ACCC4CF8.asc |
3.添加PostgreSQL仓库
1 |
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list |
4.更新包列表:
1 |
sudo apt update |
安装PostgreSQL and pgvector
1.命令行安装
1 |
sudo apt install postgresql-16 postgresql-16-pgvector -y |
2.切换至postgres用户
1 |
sudo -i -u postgres |
3.访问PostgreSQL shell命令行:
1 |
psql |
4.创建数据库用户和数据库;
1 2 |
CREATE USER n8n_user WITH PASSWORD 'mypassword'; CREATE DATABASE n8n_db OWNER n8n_user; |
5.通过\c n8n
连接刚刚创建的n8n数据库,然后激活向量数据库功能并验证扩展安装是否成功:
1 2 |
CREATE EXTENSION vector; \dx |
执行\q
退出pg的shell。
安装n8n
1.安装Node.js
1 2 |
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt install -y nodejs build-essential |
2.安装n8n:
1 |
sudo npm install n8n -g |
3.配置环境:
1 2 |
sudo mkdir /etc/n8n sudo vi /etc/n8n/n8n.env |
配置示例内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# --- Database Configuration --- DB_TYPE=postgresdb DB_POSTGRESDB_HOST=localhost DB_POSTGRESDB_PORT=5432 DB_POSTGRESDB_DATABASE=n8n_db DB_POSTGRESDB_USER=n8n_user DB_POSTGRESDB_PASSWORD=your_strong_password # --- n8n Webhook & Domain Configuration --- # Use your actual domain name here WEBHOOK_URL=https://n8n.yourdomain.com/ N8N_HOST=n8n.yourdomain.com N8N_PROTOCOL=https NODE_ENV=production # smtp N8N_EMAIL_MODE=smtp N8N_SMTP_HOST=smtp.xxx.com N8N_SMTP_PORT=465 N8N_SMTP_USER=xxx@xxx.com N8N_SMTP_PASS=your_password N8N_SMTP_SENDER=N8N<xxx@xxx.com> N8N_SMTP_SSL=true |
1 |
sudo vi /etc/systemd/system/n8n.service |
示例配置内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[Unit] Description=n8n Requires=postgresql.service After=network.target postgresql.service [Service] Type=simple User=YOUR_USERNAME Group=YOUR_USERNAME # Path to the n8n binary ExecStart=/usr/bin/n8n # Load environment variables from our file EnvironmentFile=/etc/n8n/n8n.env Restart=on-failure RestartSec=10s [Install] WantedBy=multi-user.target |
启动服务:
1 2 3 |
sudo systemctl daemon-reload sudo systemctl enable n8n sudo systemctl start n8n |
检查服务启动状态:
1 |
sudo systemctl status n8n |
Nginx反代及SSL
1.安装Nginx
1 |
sudo apt install nginx -y |
2.配置文件示例,基本都是常规配置,主要是注意n8n会用到Websocket连接:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
server { listen 80; server_name n8n.yourdomain.com; location / { proxy_pass http://127.0.0.1:5678; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; chunked_transfer_encoding off; proxy_buffering off; proxy_cache off; } } |
3.SSL我直接使用了LetsEncrypt,可以参照官网直接安装Certbot进行操作。
1 2 |
sudo snap install --classic certbot sudo certbot --nginx |
此时即可打开https://n8n.yourdomain.com/配置管理员账号。
日常升级:
1 2 3 4 5 6 |
# Stop the n8n service sudo systemctl stop n8n # Update n8n using npm sudo npm install -g n8n@latest # Start the n8n service again sudo systemctl start n8n |
常见问题
1.Failed to register community edition: connect EHOSTUNREACH 2606:4700:20::681a:cbb:443
注册可以免费开启部分功能,但似乎n8n自身IPv6连接就有问题,可以直接使用sudo systemctl edit n8n.service
增量修改或直接修改服务配置文件优先使用IPv4进行连接:
1 2 |
[Service] Environment="NODE_OPTIONS=--dns-result-order=ipv4first" |
重启服务即可。