Skip to main content
To install Node.js on Ubuntu, install nvm, run nvm install --lts, then install PM2 with npm install -g pm2. Start your app with pm2 start app.js --name myapp, run pm2 save, then run pm2 startup and paste the sudo command it prints. PM2 now restarts your app if it crashes and starts it again after a reboot. Put Nginx in front for your domain and HTTPS.

Before you start

  • Work as a normal user, not root, so your app does not run with full system rights. See create a sudo user. This guide uses the user alex.
  • Pick an LTS (long-term support) release of Node.js for servers. Today the LTS lines are 26, 24 and 22. Node.js 26 is the newest LTS.
Node.js runs JavaScript on the server. PM2 is a process manager: it keeps your app running in the background, restarts it on crashes and collects its logs.

How to install Node.js

nvm installs Node.js in your home folder, so you need no sudo and can switch versions easily.
nvm install --lts installs the newest LTS release. To pin a line, use for example nvm install 24.
Check the result:
The version distribution repositories ship (apt install nodejs) is often older than the current LTS. Check it with apt-cache policy nodejs before you rely on it.

How to install PM2 and start your app

1

Install PM2

With NodeSource Node.js, use sudo npm install -g pm2.
2

Start your app

From your app’s folder:
For an app started with npm start, use pm2 start npm --name myapp -- start.
3

Save the process list

PM2 stores the list in ~/.pm2/dump.pm2 and restores it at boot.
4

Start PM2 at boot

PM2 prints a command that starts with sudo env PATH=$PATH:…. Copy and run that exact command. It creates a systemd service named pm2-alex.

How to check it worked

Your app shows online (output shortened):
Test the app locally, for example curl http://127.0.0.1:3000. Reboot the server once to confirm the app comes back by itself.

Everyday PM2 commands

For several apps or environment variables, create a config file with pm2 init simple. It writes ecosystem.config.js, which you start with pm2 start ecosystem.config.js.

How to put your app online with Nginx

Keep the app listening on 127.0.0.1, not on a public port. Then add an Nginx reverse proxy for your domain and HTTPS. See set up Nginx as a reverse proxy.
Do not open your app’s port (for example 3000) in the firewall. Only ports 80 and 443 for Nginx, and your SSH port, need to be open.

Frequently asked questions

My app stops when I close the SSH window. Why?

You started it with node app.js in the terminal. Start it with PM2 instead, or use tmux or screen for quick tests only.

After a reboot my app is gone. What did I miss?

Either pm2 save was not run after starting the app, or the sudo env PATH=… command from pm2 startup was not run. Do both again.

How do I upgrade Node.js?

Install the new version (nvm install --lts), then refresh PM2’s boot script: run pm2 unstartup and the command it prints, then pm2 startup and the command it prints, and finally pm2 save.

Can I use systemd instead of PM2?

Yes. A plain systemd service also restarts your app and starts it at boot. See create a systemd service.

VPS and dedicated server guides

Every server guide in one place.

Nginx reverse proxy

Domain and HTTPS for your app.

Host a Discord bot

A common Node.js use.

Create a systemd service

The alternative to PM2.

Fix 502 Bad Gateway

When Nginx can’t reach the app.

Checked against the official Node.js releases page, the nvm README and the PM2 startup documentation on 4 November 2026; PM2 7.1.2 commands (start, save, startup, init simple) tested on Ubuntu 24.04 LTS with Node.js 22. nvm and NodeSource installs were not tested. Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026