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.
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 running.
- Root access or a user with sudo privileges.
- A domain name 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 are not 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:
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/myappcd /var/www/myapp
Initialize a new project and install Express with the following NPM commands:
npm init -ynpm install express
Create the application file named app.js with your desired text editor:
Paste the following code into the file, which creates a server running on port 3000:
1const express = require('express');2const app = express();3const port = 3000;4 5app.get('/', (req, res) => {6 res.send('Hello World! Node.js is running behind Nginx.');7});8 9app.listen(port, () => {10 console.log(`App running on http://localhost:${port}`);11});
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.
1[Unit]2Description=Node.js App3After=network.target4 5[Service]67WorkingDirectory=/var/www/myapp89ExecStart=/usr/bin/node /var/www/myapp/app.js10 1112Restart=always1314RestartSec=1015 1617User=root18Environment=NODE_ENV=production19 20[Install]21WantedBy=multi-user.target
Reload the daemon to read the new file, enable, and start the service with the commands below:
sudo systemctl daemon-reloadsudo systemctl enable myappsudo 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:
1server {2 listen 80;3 server_name your_domain.com www.your_domain.com;4 5 location / {6 7 proxy_pass http://localhost:3000;8 9 10 proxy_http_version 1.1;11 proxy_set_header Upgrade $http_upgrade;12 proxy_set_header Connection 'upgrade';13 proxy_set_header Host $host;14 proxy_cache_bypass $http_upgrade;15 16 17 proxy_set_header X-Real-IP $remote_addr;18 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;19 }20}
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/
Test Nginx configuration for syntax errors and restart Nginx to apply the changes:
sudo nginx -tsudo systemctl restart nginx
Note: Ensure your firewall allows SSH, HTTP, and HTTPS traffic:
sudo ufw allow OpenSSHsudo 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:
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.
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:
How to Fix Upstream Timed Out Errors in Nginx
Setting Up Docker on Ubuntu VPS