How to Deploy Node.js on Linux VPS with Nginx, Systemd, and SSL
Deploying your Nodejs application to a live server is the most exciting part of development. It turns your code into a real product that the world can see. However, running an app in production requires more than just a node start command; you need a setup that is fast, secure, and stays online 24/7. In this guide, you will learn the complete process to deploy Nodejs on Linux VPS.
You will learn to set up a strong environment using Nginx as a reverse proxy and Systemd to keep your app running automatically.
To achieve optimal performance, we recommend using a reliable provider like Perlod Hosting, which offers the high-speed NVMe infrastructure necessary for seamless Node.js deployments.
Table of Contents
Prerequisites to Deploy Nodejs on Linux VPS
To complete this guide, you need a few basic things to access and manage your server, including:
- A Linux VPS with Ubuntu 22.04 or Ubuntu 24.04 is running.
- Root access or a user with sudo privileges.
- A domain name is pointed to your VPS IP address.
Once you are done with these prerequisites, follow the steps below to deploy Nodejs on Linux VPS by using Nginx as a reverse proxy and Systemd.
Install Nodejs LTS Version
For production, it is recommended to use the NodeSource repository to install the latest Long Term Support (LTS) version of Node.js, which avoids version conflicts common with nvm when using Systemd.
First, update your local package index with the command below:
sudo apt update && sudo apt upgrade -y
Install curl and git if they aren’t already installed:
sudo apt install curl git -y
Add the NodeSource LTS repository with the command below:
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
Then, install Nodejs by using the following command:
sudo apt install -y nodejs
Check the Node and NPM version to verify your installation:
node -v
npm -v
Set up Nodejs Application
Now that your server is ready, you can set up the actual application. In this step, we will create a simple Node.js app to test our deployment, but you can also use these same commands to upload and run your own project code.
Create a directory for your app and switch to it with the commands below:
mkdir -p /var/www/myapp
cd /var/www/myapp
Initialize a new project and install Express with the following NPM commands:
npm init -y
npm install express
Create the application file named app.js with your desired text editor:
nano app.js
Paste the following code into the file, which creates a server running on port 3000:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World! Node.js is running behind Nginx.');
});
app.listen(port, () => {
console.log(`App running on http://localhost:${port}`);
});
Save and exit the file.
Create a Systemd Service for Nodejs Application
Systemd ensures your app runs in the background and automatically restarts if it crashes or the server reboots. In this step, you can create a systemd unit file for your app with the command below:
sudo nano /etc/systemd/system/myapp.service
Paste the following configuration into the file:
Remember to replace User=root with your actual username if not running as root.
[Unit]
Description=Node.js App
After=network.target
[Service]
# Location of your app
WorkingDirectory=/var/www/myapp
# Command to start the app (find path with 'which node')
ExecStart=/usr/bin/node /var/www/myapp/app.js
# Restart policy
Restart=always
# Restart delay to prevent loop
RestartSec=10
# Run as a specific user (optional but recommended)
User=root
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
Reload the daemon to read the new file, enable, and start the service with the commands below:
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
Ensure your service is active and running with the command below:
sudo systemctl status myapp
Configure Nginx Reverse Proxy for Nodejs Application
In this step, you can use Nginx as a reverse proxy, which handles public traffic on port 80 or 443, and forwards it to your Node app on port 3000.
Install Nginx if it is not installed on your server:
sudo apt install nginx -y
Create an Nginx server block config file with your domain name:
sudo nano /etc/nginx/sites-available/myapp
Add the following configuration to the file:
server {
listen 80;
server_name your_domain.com www.your_domain.com;
location / {
# Forward traffic to Node.js port
proxy_pass http://localhost:3000;
# Standard headers for proxying
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;
# Pass real client IP
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Once you are done, enable the configuration by creating a symbolic link to the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
Text Nginx configuration for syntax errors and restart Nginx to apply the changes:
sudo nginx -t
sudo systemctl restart nginx
Note: Ensure your firewall allows SSH, HTTP, and HTTPS traffic:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
Secure Nodejs Application with SSL
Security is essential for any modern application. To protect your users’ data, you can secure your site with a free SSL certificate from Let’s Encrypt, which enables HTTPS-encrypted connections automatically.
Install Certbot with the command below to automatically obtain and install a free SSL certificate from Let’s Encrypt:
sudo apt install certbot python3-certbot-nginx -y
Run the command below to generate the certificate:
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
Follow the prompts and select 2 to redirect HTTP to HTTPS if asked.
Test and Verify Your Nodejs Deployment on Linux VPS
At this point, you can perform the final check to ensure your application is accessible, secure, and running smoothly for the world to see.
Open your browser and visit the following URL:
https://your_domain.com
You should see:
Hello World! Node.js is running behind Nginx.
The connection should be secure by showing a lock icon. Your application is now production-ready, auto-restarts on failure, and is served securely via Nginx.
FAQs
How do I update my Nodejs application after making changes?
Whenever you update your code, for example, from git pull, you must restart the application service for the changes to take effect.git pull origin main
npm install #Only if you added new dependencies)
sudo systemctl restart myapp
Can I host multiple Nodejs apps on the same VPS?
Yes. Simply run the new app on a unique port and create a separate Nginx server block pointing proxy_pass to that port.
Why do I get a 502 Bad Gateway error in my Nodejs app?
This usually means Nginx is running, but it cannot connect to your Node.js application. This happens if your Node app has crashed or if the port in your Nginx config doesn’t match the port in your app.js.
Conclusion
At this point, you have learned to deploy Nodejs on Linux VPS by implementing Systemd and Nginx, which ensures your application is not only fast and secure but also strong enough to handle real-world traffic. Your app will now automatically restart if it crashes, and your traffic is encrypted with HTTPS.
As your user base grows, reliable infrastructure becomes critical. Ensure your application remains fast and scalable by using high-performance solutions like Perlod Hosting.
We hope you enjoy this guide. Subscribe to our X and Facebook channels to stay up-to-date with the latest articles and updates on VPS hosting.
For further reading: