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:
Bash
sudoaptupdate&&sudoaptupgrade-y
Install curl and git if they are not already installed:
Bash
sudoaptinstallcurlgit-y
Add the NodeSource LTS repository with the command below:
Then, install Nodejs by using the following command:
Bash
sudoaptinstall-ynodejs
Check the Node and NPM version to verify your installation:
Bash
node-vnpm-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:
Bash
mkdir-p/var/www/myappcd/var/www/myapp
Initialize a new project and install Express with the following NPM commands:
Bash
npminit-ynpminstallexpress
Create the application file named app.js with your desired text editor:
Bash
nanoapp.js
Paste the following code into the file, which creates a server running on port 3000:
JavaScript
1const express =require('express');2const app =express();3const port =3000;45app.get('/',(req, res)=>{6 res.send('Hello World! Node.js is running behind Nginx.');7});89app.listen(port,()=>{10 console.log(`App running on http://localhost:${port}`);11});
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:
Bash
sudonano/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.
INI
1[Unit]2Description=Node.js App3After=network.target45[Service]6# Location of your app7WorkingDirectory=/var/www/myapp8# Command to start the app (find path with 'which node')9ExecStart=/usr/bin/node /var/www/myapp/app.js1011# Restart policy12Restart=always13# Restart delay to prevent loop14RestartSec=101516# Run as a specific user (optional but recommended)17User=root18Environment=NODE_ENV=production1920[Install]21WantedBy=multi-user.target
Reload the daemon to read the new file, enable, and start the service with the commands below:
Ensure your service is active and running with the command below:
Bash
sudosystemctlstatusmyapp
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:
Bash
sudoaptinstallnginx-y
Create an Nginx server block config file with your domain name:
Bash
sudonano/etc/nginx/sites-available/myapp
Add the following configuration to the file:
NGINX
1server{2listen80;3server_nameyour_domain.comwww.your_domain.com;45location / {6# Forward traffic to Node.js port7proxy_passhttp://localhost:3000;89# Standard headers for proxying10proxy_http_version1.1;11proxy_set_headerUpgrade$http_upgrade;12proxy_set_headerConnection'upgrade';13proxy_set_headerHost$host;14proxy_cache_bypass$http_upgrade;1516# Pass real client IP17proxy_set_headerX-Real-IP$remote_addr;18proxy_set_headerX-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:
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:
Bash
sudoaptinstallcertbotpython3-certbot-nginx-y
Run the command below to generate the certificate:
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:
Bash
https://your_domain.com
You should see:
Bash
HelloWorld!Node.jsisrunningbehindNginx.
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.
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
Yes. Simply run the new app on a unique port and create a separate Nginx server block pointing proxy_pass to that port.
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.
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.