How to Deploy Node.js on Ubuntu 24.04 (Production Setup)

Node.js is a powerful JavaScript runtime for building scalable applications. When moving apps into production, you need to configure your server securely and efficiently. In this guide, you’ll learn how to set up a production-ready Node.js app on Ubuntu 24.04 using PM2, Nginx, and Let’s Encrypt.


Prerequisites

Before starting, make sure you have:

  • An Ubuntu 24.04 server.
  • A non-root user with sudo privileges.
  • A domain name pointed to your server’s IP.

For background on Node.js, visit the official Node.js website.


Step 1 — Install Node.js

Ubuntu 24.04 provides updated repositories. However, to get the latest LTS version, use NodeSource:

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

Check installation:

node -v
npm -v

Once complete, Node.js and npm should be available.


Step 2 — Create a Sample Node.js App

First, make a directory and initialize your project:

mkdir ~/myapp
cd ~/myapp
npm init -y
npm install express

Now create app.js:

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send('Hello from Node.js on Ubuntu 24.04!');
});

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

Run the app:

node app.js

Then visit http://your_server_ip:3000 to confirm it works.


Step 3 — Manage Processes with PM2

Keeping apps running in production is essential. Therefore, install PM2:

sudo npm install -g pm2
pm2 start app.js
pm2 status

Enable PM2 startup on reboot:

pm2 startup systemd
pm2 save

For more details, check the PM2 documentation.


Step 4 — Configure Nginx as a Reverse Proxy

A reverse proxy ensures smooth traffic management. Begin by installing Nginx:

sudo apt install nginx -y

Next, create a new config file:

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

Insert the following:

server {
    listen 80;
    server_name your_domain.com www.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;
    }
}

Enable and restart:

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

Now your domain should display the Node.js app. For further tuning, see the Nginx documentation.


Step 5 — Secure the App with SSL

It’s crucial to encrypt traffic. Let’s use Certbot and Let’s Encrypt.

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Obtain and install SSL certificates:

sudo certbot --nginx -d your_domain.com -d www.your_domain.com

Finally, test auto-renewal:

sudo certbot renew --dry-run

Step 6 — Harden and Monitor the Server

Security is a must. Therefore, configure UFW firewall:

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

Monitor the app with PM2:

pm2 monit

Additionally, you can integrate server monitoring tools for deeper insights.


Conclusion

You now have a production-ready Node.js setup on Ubuntu 24.04. With PM2 handling processes, Nginx serving as a reverse proxy, and Let’s Encrypt securing traffic, your application is both scalable and secure. For more guides like this, check out my resume page to see related infrastructure projects.

Posted in TutorialsTags:
Write a comment