Run n8n as a Service with PM2

Great post Thanks @Tephlon . I managed to get my self hosted n8n on AWS lightsail, running pm2 and caddyserver. Assigned A record on DNS and have it working on my subdomain.domain.com. Was able to post some CLI commands to set user/password, and change the webhook default url (subd.domain…). Now my only issue is I cant seem to find a way to persist the DB so that if the server restarts I can get back my credentials, workflows. Also had a bit of trouble when updating (had to reinstall the whole thing). Any help the the persistance of DB would be much appreciated.

Hey @pradilla,

You need to have some persistent storage mounted in the container so the database and config file live on that. You could in theory use an external database instead like Postgres or MySQL and set the encryption key as an environment option.

Hi all, trying here to see if maybe one of you experts know how to do this.
I’m successfully running n8n latest version, installed on AWS, Lightsail, Debian, using PM2 and CaddyServer.
Lightsail instance has 2GB RAM available, but in the PM2 monitor I can only see it reach 1GB and then if some heavy processing is happening it can crash and restart.
Does anybody know how to make it use the 2GB available?

Thanks
Andres

Hey @pradilla,

I have not tried it but have you tried adding "node_args": "--max_old_space_size=2048" to your pm2 config? Just don’t forget that the OS will need some memory just to idle and so will Caddy so it would be worth slowly increasing the limit.

1 Like

Hi @Tephlon,

I have problem for my n8n activate.
My workflow working perfectly with n8n keep running and the data insert perfectly.
But when I stop run n8n, the data not insert into database, means that if I stop running n8n, it will stop the activate process.
I want to always activate my workflow in company server, even at the background, even I stop from run n8n.

I was use some solution, and my problem not solve:

  • pm2 unstartup
  • pm2 restart n8n

Any suggestion step for this problem?

I am running windows 10. I have tried to get the n8n run via PM2 and followed article and advise here (used the path, …etc). Still, the command does not run with the reason that it attempts to interpret the n8n.cmd as a Javascript code it seems:

SyntaxError: Invalid or unexpected token
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1055:15)
    at Module._compile (node:internal/modules/cjs/loader:1090:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Object.<anonymous> (C:\Users\test\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:33:23)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
C:\Users\test\AppData\Roaming\npm\node_modules\n8n\bin\n8n.cmd:1
@echo off

The other n8n script delivered in the same directory is a shell command

Hey @Phonitel,

Can you create a new thread and fill out the template it provides.