Deploying Node.js & Next.js with PM2 Process Manager
Node.js နှင့် Next.js အက်ပ်များကို PM2 ဖြင့် Production တင်နည်း
Manage Node.js, Express, and Next.js applications in production with PM2. Configure cluster mode, auto-restart on crashes, and system reboots.
Node.js အက်ပ်များ crash ဖြစ်သွားပါက အလိုအလျောက် ပြန်ပွင့်စေမည့် PM2၊ CPU cores အားလုံးကို အသုံးချမည့် Cluster Mode နှင့် Ecosystem file ရေးသားပုံ။
1. Why use PM2 in Production? (PM2 ကို ဘာကြောင့် သုံးရသလဲ)
Node command တိုက်ရိုက် run ခြင်းနှင့် PM2 ကွာခြားချက်
စမ်းသပ်ချိန်တွင် `node server.js` ဟု terminal တွင် run လေ့ရှိသော်လည်း terminal window ပိတ်လိုက်ပါက သို့မဟုတ် uncaught error ကြောင့် crash ဖြစ်သွားပါက ဝဘ်ဆိုက် ရပ်တန့်သွားမည် ဖြစ်ပါသည်။
PM2 (Process Manager 2) သည် background daemon အဖြစ် အမြဲတမ်း run ပေးထားပြီး error ဖြစ်ပါက မီလီစက္ကန့်ပိုင်းအတွင်း အလိုအလျောက် restart ပြုလုပ်ပေးကာ server ပြန်ပွင့်ချိန်တွင်လည်း auto-start ဖြစ်စေပါသည်။
2. Essential PM2 Commands & Ecosystem Config
လက်တွေ့သုံး PM2 Command များနှင့် Ecosystem ဖိုင်
Project ၏ root folder တွင် `ecosystem.config.js` ဖိုင်ကို ရေးသားထားခြင်းဖြင့် Environment variables များ (DB URLs, Ports, Secret Keys) ကို စနစ်တကျ ထိန်းချုပ်နိုင်ပါသည်။
module.exports = {
apps: [
{
name: 'my-node-app',
script: './dist/index.js',
instances: 'max', // Cluster mode: use all available CPU cores
exec_mode: 'cluster',
autorestart: true,
watch: false,
max_memory_restart: '500M',
env_production: {
NODE_ENV: 'production',
PORT: 3000
}
}
]
};
Command ရှင်းလင်းချက် (ဖတ်ရှုရန် နှိပ်ပါ)
Cluster mode ဖြင့် CPU core အားလုံးပေါ်တွင် load balancing ပြုလုပ်ပေးပြီး memory 500MB ကျော်ပါက memory leak မဖြစ်အောင် အလိုအလျောက် recycle restart ပြုလုပ်ပေးမည် ဖြစ်ပါသည်။
# Install PM2 globally
sudo npm install -g pm2
# Start app using ecosystem config
pm2 start ecosystem.config.cjs --env production
# View real-time logs and monitoring
pm2 logs
pm2 monit
# Save PM2 state to resurrect on server reboot
pm2 save
pm2 startup
Command ရှင်းလင်းချက် (ဖတ်ရှုရန် နှိပ်ပါ)
pm2 startup command သည် Linux systemd service ထဲသို့ PM2 ကို ထည့်သွင်းပေးပြီး ဆာဗာ reboot ဖြစ်သွားသည့်တိုင် အက်ပ်များကို အလိုအလျောက် ပြန်လည် run ပေးပါသည်။
Which PM2 command saves the list of running processes so they automatically restore after a VPS reboot?
VPS Server ပြန်လည် reboot ဖြစ်သွားသည့်အခါ လက်ရှိ run နေသော PM2 app များကို မပျောက်ပျက်စေဘဲ အလိုအလျောက် ပြန်ပွင့်စေရန် မည်သည့် command ဖြင့် မှတ်သားရသလဲ?