Support article
How to Install and Run Node.js on Your VPS
Install Node.js on your VPS with NVM or from repository, run your app, configure it as a service with PM2 and publish with Nginx as reverse proxy.
Introduction
Node.js is an environment that lets you run applications written in JavaScript on the server. It’s widely used for APIs, real-time applications (chats, notifications), apps built with React, Vue or Angular, and microservices.
If you’ve developed an application with Node.js and want to publish it, a VPS is the ideal place. In this article you’ll see how to install Node.js, run your app and keep it running permanently.
Node.js doesn’t work the same as PHP. In PHP, the web server (Apache or Nginx) executes scripts. In Node.js, the application is the server: it keeps running and listening for requests.
Prerequisites
Before starting you need:
- A VPS with Ubuntu, Debian, AlmaLinux or Rocky Linux.
- SSH access as root or a user with
sudo. - The system updated.
Method 1: Install Node.js with NVM (recommended)
NVM (Node Version Manager) lets you install and switch between several Node.js versions. It’s the most flexible option.
Step 1: Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
Reload the terminal:
source ~/.bashrc
Step 2: Install Node.js
The LTS (Long Term Support) version is recommended for production:
nvm install --lts
Check it’s installed:
node -v
npm -v
NVM advantage
If your app needs a specific version, install it:
nvm install 20
nvm use 20
Method 2: Install Node.js from repository
If you only need one version and want something simpler:
apt install nodejs npm -y
On AlmaLinux/Rocky:
dnf install nodejs npm -y
Check the version:
node -v
The repository package may not be the latest version. If you need a specific version, use NVM.
Run your Node.js application
Upload your app files to the server (via FTP, Git or rsync). Then, enter the folder and install dependencies:
cd /var/www/myapp
npm install
Start the application:
node app.js
⚠️ If you close the terminal, the application stops. To keep it running you need PM2 or systemd (covered below).
Keep the app running with PM2
PM2 is a process manager for Node.js. It keeps your app running even after you close the terminal and restarts it if it crashes.
Install PM2
npm install -g pm2
Start your app with PM2
cd /var/www/myapp
pm2 start app.js --name "myapp"
Save the configuration and automatic startup
pm2 startup
pm2 save
PM2 will show you a command to run (copy and paste it). This way your app will start on its own when the server restarts.
[Imagen sugerida: screenshot of PM2 showing the application status]
Useful PM2 commands
pm2 status # see running apps
pm2 logs myapp # view logs
pm2 restart myapp # restart the app
pm2 stop myapp # stop the app
pm2 delete myapp # remove the app from PM2
Publish with Nginx as reverse proxy
Your Node.js app runs on an internal port (for example 3000). To make it accessible on port 80 or 443 (HTTP/HTTPS), configure Nginx as a reverse proxy.
Step 1: Install Nginx
apt install nginx -y
Step 2: Create the site configuration
nano /etc/nginx/sites-available/myapp
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Step 3: Enable the site
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx
Now your app is accessible from http://your-domain.com.
Step 4: Activate SSL with Let’s Encrypt
apt install certbot python3-certbot-nginx -y
certbot --nginx -d your-domain.com
[Imagen sugerida: diagram of Nginx as reverse proxy to Node.js]
Useful tips
- Always use the LTS version in production. It’s the most stable.
- Don’t run the app as root. Create a normal user for added security.
- Configure environment variables. Use a
.envfile for keys and configuration. - Keep Node.js updated. Updates fix vulnerabilities.
- Monitor logs. With PM2 you can see them in real time with
pm2 logs.
Common problems
The app crashes when closing the terminal
It’s normal. You need PM2 or systemd to keep it running as a service.
I can’t access the app from the browser
Check that the app runs on localhost:3000 (or whatever port), that Nginx is active, and that the firewall allows ports 80 and 443.
Error “Cannot find module”
You’re missing dependencies. Enter the app folder and run npm install.
Port 3000 is occupied
Change the port in your app or use another. Remember to also update the Nginx configuration.
The app is slow with many requests
Consider using more than one process (cluster mode) with PM2:
pm2 start app.js -i max
Frequently asked questions
Does Node.js need Apache or Nginx?
Not to run (the app itself is the server), but it’s highly recommended to put Nginx in front as a reverse proxy to serve HTTPS and balance the load.
Can I have several Node.js apps on the same VPS?
Yes. Each app runs on a different port and you configure an Nginx site for each domain.
Which Node.js version should I use?
The LTS (Long Term Support) version, which is the most stable for production.
Is PM2 free?
Yes, PM2 is free and open source. It has a paid version (PM2 Plus) for advanced monitoring.
Do I need to know programming to publish a Node.js app?
You need to know how to handle your app and configure the server. You don’t need to be a programmer, but you do need basic Linux knowledge.
Publish your Node.js app with confidence
Node.js is a fantastic option for modern applications, and a VPS is the ideal place to publish them. With NVM to manage versions, PM2 to keep the app running and Nginx as reverse proxy, you have a professional and stable setup.
If you prefer not to handle installation and configuration, at miHosting we offer managed VPS where we install Node.js, configure PM2 and Nginx, and maintain the server for you. Open a ticket from your client panel and we’ll help.