Run a Node.js App 24/7 on a VPS with PM2
Keep your Node.js bot or app running nonstop on an Ubuntu VPS with PM2: installing Node.js LTS, starting with PM2, auto-start on boot, the ecosystem file, logs, a memory limit and updates.
Running a Telegram or Discord bot, a small API or a scheduled job written in Node.js with node index.js on your own computer is fine for testing. But when you close the terminal, when the program crashes or when the computer restarts, the app stops too. PM2 is a process manager that runs Node.js apps in the background, restarts them when they crash and brings them back automatically when the server boots. In this guide we set up PM2 step by step on a VPS running Ubuntu 24.04.
Summary: install the LTS release of Node.js, add PM2 with
npm install -g pm2, start your app withpm2 start, then usepm2 startupandpm2 saveso it starts automatically on boot.
PM2 or systemd?
Both do the job. We covered the systemd approach in our run a Telegram bot 24/7 with systemd guide. PM2's advantages are its Node.js-specific conveniences: log tailing with one command, a live CPU/RAM screen, restarting when a memory limit is exceeded and managing several apps from one file. If you will run several Node.js apps on the same server, PM2 is more practical.
1. Prepare the server
Connect to your server over SSH and update the system:
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl git
Create a separate user so the app does not run as root:
sudo adduser --disabled-password --gecos "" app
2. Install the Node.js LTS release
The Node.js version in Ubuntu's own repository is usually outdated. Install the current LTS release from the NodeSource repository (Node.js 24 in this guide):
curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v
The output of node -v should start with v24.
3. Install PM2
Install PM2 system-wide:
sudo npm install -g pm2
pm2 -v
4. Example app: a simple Telegram bot
If you already have an app, clone it to the server with git clone and skip this step. As an example, let's build a small bot with the grammY library. Switch to the app user:
sudo -iu app
mkdir -p ~/bot && cd ~/bot
npm init -y
npm install grammy
nano index.js
const { Bot } = require("grammy");
const bot = new Bot(process.env.BOT_TOKEN);
bot.command("start", (ctx) => ctx.reply("Hello! This bot runs 24/7 on a VPS."));
bot.on("message:text", (ctx) => ctx.reply("Your message: " + ctx.message.text));
bot.start();
Do not put the bot token in the code; keep it in a separate file that only you can read:
echo 'BOT_TOKEN=123456:ABC-YOUR-TOKEN' > .env
chmod 600 .env
This bot uses polling: it connects to Telegram itself and asks for new messages. That means the server needs no open inbound port, no domain and no public IP address.
5. Start the app with PM2
Keeping settings in an ecosystem file makes life easier as the number of apps grows:
nano ~/bot/ecosystem.config.js
module.exports = {
apps: [
{
name: "bot",
script: "index.js",
cwd: "/home/app/bot",
node_args: "--env-file=.env",
max_memory_restart: "300M",
env: { NODE_ENV: "production" },
},
],
};
node_args: "--env-file=.env": loads the variables from.envusing a built-in Node.js feature; no extra package is needed.max_memory_restart: if the app exceeds this amount of memory, PM2 restarts it. This keeps a leaking app from locking up the server.
Start it and check the status:
pm2 start ecosystem.config.js
pm2 list
You should see the bot app with the status online. Send /start to your bot in Telegram to test it.
6. Start automatically on boot
So that PM2 brings your apps back after a server restart, run this as the app user:
pm2 startup systemd
The command prints a line starting with sudo env PATH=.... Go back to your privileged user with exit and copy and run that line exactly as shown. Then switch back to the app user and save the list of running apps:
sudo -iu app
pm2 save
To test, reboot the server (sudo reboot); after reconnecting, sudo -iu app pm2 list should show the app running on its own.
7. Everyday commands
| Command | What it does |
|---|---|
pm2 list |
Lists apps with their status, CPU and RAM usage |
pm2 logs bot |
Follows the app's logs live (--lines 100 for history) |
pm2 monit |
Live CPU/RAM and log screen |
pm2 restart bot |
Restarts the app |
pm2 stop bot / pm2 delete bot |
Stops it / removes it from the list |
pm2 describe bot |
Details such as restart count and log file paths |
Log files grow over time. For automatic rotation, install the PM2 module:
pm2 install pm2-logrotate
8. Updating the app
If your code is in Git, an update takes three steps:
cd ~/bot
git pull
npm install --omit=dev
pm2 restart bot
If you changed a setting in the ecosystem file, reload it with pm2 restart ecosystem.config.js --update-env and save the list again with pm2 save. After upgrading PM2 itself (sudo npm install -g pm2), run pm2 update to refresh the running daemon.
Running a web app or an API
If a web app such as Express must be reachable from outside, put a web server in front of it and use HTTPS; follow our Nginx and Let's Encrypt SSL setup guide. In that case the server needs its own public IPv4 address. If you prefer containers, see our Docker installation article.
Which plan fits?
- Bots and background jobs: bots that use polling open no inbound ports, so our VPS plans in Istanbul are enough. A simple bot uses a few tens of MB of RAM; together with PM2, several small bots fit in 512 MB–1 GB of RAM. Plans start at $0.93 per month. With the Node.js VPS option, Node.js, npm, PM2 and Git come preinstalled, so you can skip the install steps in this guide; it costs the same as the matching VPS plan.
- Apps under constant load: VDS plans, where CPU cores are reserved only for you, give more predictable performance at peak times; VDS Enterprise plans also include your own IPv4 address.
- If you are new to managing a server: the steps in this guide work as-is on every one of our VPS plans with root access; to get used to the command line, see our basic Linux commands post.
All plans have unmetered speed and include 3 TB of traffic per month, and payments are accepted in USDT (TRC20).
If you would rather publish a Node.js app without setting up a server yourself, we compare Node.js hosting and a VPS in What is Node.js hosting?
Frequently Asked Questions
Is PM2 free?
Yes. PM2 is open source and free to use on your server. Only the optional web-based monitoring service is paid; nothing in this guide requires it.
Why didn't my app start after the server rebooted?
Usually the sudo env PATH=... line printed by pm2 startup was not run, or pm2 save was not run after starting the app. Repeat both steps.
Where do I find the logs in PM2?
pm2 logs bot shows live logs. The files are stored by default in the user's ~/.pm2/logs/ folder; the exact path is shown in the output of pm2 describe bot.
Can I run more than one Node.js app on the same server?
Yes. Add a separate block for each app to the apps list in the ecosystem file, or use a separate file per app. Just keep an eye on total RAM usage with pm2 list.
How do I upgrade Node.js?
Run the NodeSource setup command with the new version number and update with sudo apt install -y nodejs, then run npm install in the app folder and restart the apps with pm2 restart all.
You might also like
Point your domain to your VPS, install Nginx, publish your site and secure it with a free Let's Encrypt certif...
Install Docker on an Ubuntu 24.04 VPS from the official repository, run your first container and deploy a pers...
Your bot shouldn't go silent when your laptop sleeps. Learn how to run a Python Telegram bot 24/7 on an Ubuntu...