Running your Node.js application locally is easy; the real work is serving it from a server 24/7 without interruption, under your domain and with SSL. In this guide we'll install Node.js on an Ubuntu VDS, manage the application with PM2 and configure Nginx as a reverse proxy.
Requirements
- A VDS running Ubuntu 22.04/24.04 and a user with sudo privileges.
- A domain pointed to the server (A record).
- A ready Node.js application in a Git repository or on your computer.
Step 1: Install Node.js
Let's install the current LTS version from the NodeSource repository:
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs
node -v && npm -vStep 2: Get your application onto the server
cd ~
git clone https://github.com/kullanici/uygulamam.git
cd uygulamam
npm ci --omit=devAs an example, a simple application file (app.js):
const http = require('http');
const PORT = process.env.PORT || 3000;
http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end('Merhaba MerkezHost!');
}).listen(PORT, '127.0.0.1', () => console.log(`Port ${PORT} dinleniyor`));Having the application listen on 127.0.0.1 ensures the port can only be reached through Nginx, which improves security.
Step 3: Run it with PM2
sudo npm install -g pm2
pm2 start app.js --name uygulamam
pm2 statusTo have the application start automatically when the server reboots:
pm2 startup systemd
# Copy and run the sudo command printed on screen
pm2 saveFrequently used PM2 commands
pm2 logs uygulamam # Live logs
pm2 restart uygulamam # Restart
pm2 reload uygulamam # Zero-downtime reload
pm2 monit # CPU/RAM monitoringStep 4: Configure Nginx as a reverse proxy
sudo apt install -y nginx
sudo nano /etc/nginx/sites-available/uygulamamserver {
listen 80;
server_name alanadiniz.com www.alanadiniz.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}sudo ln -s /etc/nginx/sites-available/uygulamam /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo ufw allow 'Nginx Full'The Upgrade and Connection headers are required for applications that use WebSockets, such as Socket.IO, to work.
Step 5: Add SSL
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d alanadiniz.com -d www.alanadiniz.comFor details, see our Let's Encrypt SSL guide.
Step 6: Update (deploy) workflow
When you update your code, the following steps refresh the application without downtime:
cd ~/uygulamam
git pull
npm ci --omit=dev
pm2 reload uygulamamConclusion
Thanks to PM2, your Node.js application now runs continuously and is served securely with SSL behind Nginx. Node.js's single-threaded nature benefits greatly from high-frequency processors, which makes our AMD Ryzen 9 performance servers ideal for Node.js projects.
Frequently Asked Questions
Why use PM2?
PM2 runs your Node.js application in the background, restarts it automatically if it crashes and starts your application automatically when the server boots.
Can I run a Node.js application directly on port 80?
It's possible but not recommended. Using Nginx in front as a reverse proxy is much better in terms of SSL, compression, caching and security.
