Nodejs

How to Deploy Node.js Applications with Systemd and Nginx Like a Pro

Node.js is a powerful JavaScript runtime that’s ideal for building fast, scalable web apps. But once your app is production-ready, how do you deploy it reliably?

In this tutorial, you’ll learn how to deploy a Node.js application using Systemd to manage your app as a service, and Nginx to reverse proxy and serve it securely.

Let’s go step-by-step from code to a fully running service.


🧰 Prerequisites

You’ll need:

  • A Linux server (Ubuntu, Debian, CentOS, etc.)
  • Node.js (v18+ recommended)
  • Nginx installed (sudo apt install nginx or sudo yum install nginx)
  • Root or sudo access
  • A domain name (optional but recommended)

📁 Step 1: Create a Sample Node.js App

Let’s start by creating a basic Express server.

bashCopyEditmkdir myapp && cd myapp
npm init -y
npm install express

Then create the app file:

jsCopyEdit// index.js
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (_, res) => res.send('Hello from Node.js!'));

app.listen(port, () => console.log(`Server running on port ${port}`));

Test it:

bashCopyEditnode index.js

Visit http://your-server-ip:3000 to confirm it’s working.


🛠️ Step 2: Create a Systemd Service File

Let’s make the app run as a background service.

Create the service definition:

bashCopyEditsudo nano /etc/systemd/system/myapp.service

Paste the following:

iniCopyEdit[Unit]
Description=My Node.js App
After=network.target

[Service]
ExecStart=/usr/bin/node /home/youruser/myapp/index.js
Restart=always
User=youruser
Environment=PORT=3000
WorkingDirectory=/home/youruser/myapp

[Install]
WantedBy=multi-user.target

✅ Replace /home/youruser/myapp and youruser with your actual path and username.

Enable and start the service:

bashCopyEditsudo systemctl daemon-reexec
sudo systemctl enable myapp
sudo systemctl start myapp
sudo systemctl status myapp

🌐 Step 3: Set Up Nginx as a Reverse Proxy

Now route HTTP traffic through Nginx.

Create a config file:

bashCopyEditsudo nano /etc/nginx/sites-available/myapp

Paste:

nginxCopyEditserver {
    listen 80;
    server_name yourdomain.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;
    }
}

Enable the site and restart Nginx:

bashCopyEditsudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Visit http://yourdomain.com — your app should load!


🔐 Step 4: Secure with HTTPS (Optional but Recommended)

Install Certbot for free SSL from Let’s Encrypt:

bashCopyEditsudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com

Your app is now running on HTTPS.


🧠 Final Thoughts

By combining Systemd and Nginx, you get:

  • Robust background service management (Systemd)
  • HTTP routing, SSL, and performance (Nginx)
  • A stable, production-ready Node.js deployment

This method is scalable and suitable for microservices, APIs, and full-stack apps.


🌿 Node.js Hosting at Green Host

At GreenHost, we provide high-performance Node.js hosting with built-in Nginx reverse proxy, SSL management, and Systemd-based service control. Launch your apps with peace of mind and lightning-fast response times.