How to Deploy a Node.js Web App with Nginx & PM2 on Linux VPS
Production Node.js Deployment with Nginx & PM2
Platforms like Vercel and Heroku impose restrictive memory limits and high bandwidth pricing. Hosting your Node.js, Express, or Next.js app on an Ash Host NVMe VPS gives you full resource control, unmetered performance, and zero artificial request throttles.
Step 1: Install Node.js & Nginx
Connect to your VPS via SSH as root and install Node.js 20 LTS along with the Nginx web server:
# Update system
apt update && apt upgrade -y
# Install Node.js 20 LTS from NodeSource
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs nginx git
# Install PM2 globally to manage background Node processes
npm install -g pm2
Step 2: Upload Your App and Start with PM2
Place your project files in /var/www/myapp, install dependencies, and launch your server with PM2:
cd /var/www/myapp
npm install --production
# Start your app with PM2 (replace app.js with your entry file):
pm2 start app.js --name "my-node-app"
# Ensure PM2 restarts your app automatically if the VPS reboots:
pm2 startup systemd
pm2 save
Step 3: Configure Nginx as a Reverse Proxy
Nginx routes incoming web traffic on port 80/443 directly to your internal Node.js port (e.g. 3000):
nano /etc/nginx/sites-available/myapp
Paste the following configuration (replace yourdomain.com with your domain):
server {
listen 80;
server_name yourdomain.com www.yourdomain.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_cache_bypass $http_upgrade;
}
}
Enable the site and restart Nginx:
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx
Step 4: Enable Free SSL with Certbot
Secure your application with HTTPS using Let's Encrypt in one automated command:
apt install -y certbot python3-certbot-nginx
certbot --nginx -d yourdomain.com -d www.yourdomain.com
Deploy Your Full-Stack Apps on Ash Host Cloud VPS
High-frequency AMD cores, ultra-fast NVMe storage, and dedicated IPv4 addresses with instant UPI setup.
Order Cloud VPS for Node.js →Ha estat útil la resposta?
Articles Relacionats
Powered by WHMCompleteSolution