Node.js Production Setup: Guide to PM2 and Nginx on Ubuntu
What Will You Learn in This Guide?
In this guide, you will learn how to prepare a Node.js application running on an Ubuntu server for production (live environment).
Process management with PM2, reverse proxy configuration with Nginx, and HTTPS setup with Let's Encrypt are discussed step by step.
The goal is to create a seamless, secure and scalable Node.js infrastructure.
Technical Summary
- Subject: Node.js production installation on Ubuntu
- Solved Problem: Secure, continuous operation of Node.js application over HTTPS
- Tools Used: Node.js, PM2, Nginx, Let's Encrypt, UFW
- Result: Professional production environment that works even if the server is restarted
Preliminary Preparations
Before you start, make sure the following requirements are met:
- A server with Ubuntu installed
- A user with authority
sudo(rooting is not recommended) - A domain name pointed to the server IP address
- Example:
uygulama.ornek.com
- Example:
1. Node.js Setup (LTS)
The Node.js version in Ubuntu repositories may be out of date.
Therefore, the LTS version will be installed using the NodeSource repository.
cd ~
curl -sL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
- This command adds the Node.js repository to the system and updates the package list.
sudo apt install nodejs
node -v
- For some npm packages that require compilation, additional tools must be installed:
sudo apt install build-essential
2. Creating a Sample Node.js Application
- It is important for security that the application is only accessible from within the server.
nano hello.js
const http = require('http');
const server = http.createServer((req, res) => {
res.end('Merhaba GenixNode!\n');
});
server.listen(3000, '127.0.0.1');
- This application works only accessible via localhost.
To test:
node hello.js
3. Process Management with PM2
- PM2 allows Node.js applications to run continuously in the background.
sudo npm install -g pm2
pm2 start hello.js
- For the application to start automatically when the server is restarted:
pm2 startup systemd
pm2 save
4. Nginx Reverse Proxy Configuration
- Node.js application should not be opened directly to the internet.
- All web traffic is routed through Nginx.
sudo nano /etc/nginx/sites-available/uygulama.ornek.com
server {
listen 80;
server_name uygulama.ornek.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
}
}
- Enable configuration:
sudo ln -s /etc/nginx/sites-available/uygulama.ornek.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
5. Firewall (UFW) Settings
- Only necessary ports should be allowed on the server.
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
- Only SSH, HTTP and HTTPS traffic is accepted with these settings.
6. HTTPS (Let's Encrypt) Setup
- HTTPS is essential for both security and SEO.
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d uygulama.ornek.com
- To test automatic renewal:
sudo certbot renew --dry-run
7. Security and Performance Improvements
Environment Variables
- Sensitive information should not be kept in code.
npm install dotenv
- The .env file should not be included in version control.
Nginx Gzip Compression
gzip on;
gzip_types text/plain text/css application/javascript application/json;
- This setting reduces page load time.
Frequently Asked Questions
1. Why use Nginx? Nginx is more efficient in SSL, caching and traffic management than Node.js.
2. What should be done when the code is updated? The PM2 process must be restarted.
pm2 restart hello
3. Will the application run when the server is restarted? Yes. It starts automatically thanks to the pm2 startup and pm2 save commands.
4. What does EADDRINUSE error mean? The specified port is being used by another process.
Result
With this guide:
Node.js application runs without interruption
Secure connection provided with HTTPS
Performance is increased with Nginx
Thanks to PM2, process management becomes automated
GenixNode Platform provides a solid and sustainable foundation for all Ubuntu-based production environments.

