Guide to Bringing Node.js Application to Live Environment on Ubuntu
What Will You Learn in This Guide?
This guide explains how to move a Node.js application to live on Ubuntu.
Aim; is to ensure that the application runs stable, secure and scalable.
Technical Summary
This content covers the process of preparing the Node.js application for the production environment.
Steps; Includes Node.js installation, process management with PM2, Nginx reverse proxy setting, SSL and UFW configuration.
1. Node.js Installation and Verification
Node.js is installed with the current LTS version via the NodeSource repository.
curl -sL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install nodejs
- These commands install Node.js and npm on the system.
node -v
npm -v
- These commands verify that the installation was successful.
2. Creating a Sample Node.js Application
- The application should only be listened to via localhost.
const http = require('http');
const hostname = 'localhost';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.end('Merhaba Dunya!');
});
server.listen(port, hostname);
- This code creates a simple HTTP server.
node hello.js
- This command tests the application.
3. Process Management with PM2
- PM2 ensures that the application runs continuously in the background.
sudo npm install -g pm2
pm2 start hello.js
- This command starts the application under PM2.
pm2 startup systemd
pm2 save
- These steps enable automatic startup after reboot.
4. Reverse Proxy Configuration with Nginx
- Node.js does not open directly to the internet. All traffic is routed through Nginx.
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;
}
- This structure runs Nginx as a reverse proxy.
sudo nginx -t
sudo systemctl reload nginx
- These commands verify the configuration.
5. Security and SSL Configuration
- With UFW, only necessary ports are opened.
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
- These steps protect the server from external access.
SSL certificate is installed with Let's Encrypt.
sudo certbot --nginx -d ornek.com
- This command automatically enables HTTPS.
sudo certbot renew --dry-run
- This command tests automatic renewal.
Frequently Asked Questions
1. What happens if the app crashes? PM2 automatically restarts the app.
2. Why doesn't Node.js open directly to the internet? Nginx makes SSL and traffic management more efficient.
3. What to do when SSL expires? Certbot automatically renews.
Result
With this guide, your Node.js application becomes ready for production. PM2 provides stability, Nginx manages traffic, SSL increases security.
You can directly implement this configuration in the GenixNode infrastructure.

