Automatically translated.View original post

PM2. In the ubuntu server???

# Trending # Drug sign with lemon8 # webserver # Web Site Writing Basics

PM2 (Process Manager 2) is the process manager for Node.js (but also supports other languages) most popular on Linux Server like Ubuntu. To explain it simply, PM2 is the "app administrator" who keeps your app running 24 / 7, even if you have closed the terminal screen, or even when the Server is rebuilt. How does PM2 work? The principle of PM2 has four key things: 1. Background Process (Daemon) Normally, if you run the app with the node app.js command, when you close the terminal window, the app will die immediately, but PM2 will turn the app into a Background Service, making the app always run behind it 2.Auto Restart (Watch & Restart) If your app "Crash" or Error goes off, PM2 will detect it immediately and automatically restore the app within a second. 3. Cluster Mode (Power Expansion) This is a great feature. PM2 can command your apps to run multiple Instances based on the amount of CPU Core you have to help prevent the impact of many users without having to write any more code. 4. Startup ScriptPM2 can create a script for itself to run when Ubuntu turns on (Boot), making sure that if the power goes out, your website or API will come back online without typing the command to run the frequently used command in Ubuntu, the command to describe pm2, start app.js, start running the application2, list all apps running and state (CPU / RAM used) pm2, stop < id / name >, pm2, restart < id / name >, restart (often after editing the code) pm2, logs, Log, Work or Error that happens Real-metipm2, beautiful Dashboard screen For a detailed pm2 state, the startup creates a command for running on automatic power-up. In a nutshell, if you do the web with Node.js on Ubuntu and want it to be "persistent and online all the time," you need to use PM2.

1/30 Edited to

... Read moreถ้าคุณกำลังค้นหาแนว ๆ “endpointbasecamp” แล้วมาเจอโพสต์นี้ บางทีคุณอาจกำลังทำ API/endpoint บน Ubuntu Server แล้วอยากให้ระบบมัน “นิ่ง” และออนไลน์ตลอด (ไม่หลุดเวลา SSH หลุด หรือเครื่องรีบูต) — ตรงนี้ PM2 ช่วยได้มากครับ ผมสรุปเป็นแนวทางใช้งานจริงที่เจอบ่อย ๆ เวลาเอา endpoint ขึ้นเซิร์ฟเวอร์ 1) ติดตั้ง PM2 ให้เรียบร้อยก่อน ส่วนใหญ่ผมติดตั้งผ่าน npm แบบ global: - npm i -g pm2 จากนั้นเช็กว่าใช้งานได้ด้วย pm2 -v 2) เริ่มรันแอปให้ถูกแบบ Production ถ้าแอปคุณมีไฟล์เริ่มต้นเช่น app.js หรือ index.js ให้เริ่มด้วย: - pm2 start app.js --name api-endpoint ตั้งชื่อไว้จะสั่งง่ายขึ้นมาก (แทนการจำ id) 3) เช็กสถานะเหมือนในภาพเทอร์มินัลที่หลายคนคุ้น คำสั่งที่ผมใช้แทบทุกวัน: - pm2 list (ดูว่า online / stopped พร้อม CPU และ RAM) - pm2 stop api-endpoint - pm2 restart api-endpoint เวลามีอัปเดตโค้ดแล้วอยากให้ endpoint รีโหลดเร็ว ๆ ก็ restart ได้เลย 4) ดู error และ log เพื่อไล่ปัญหา endpoint ตอบช้า/ล่ม - pm2 logs api-endpoint ช่วยมากตอน endpoint มี error เฉพาะบางเคส หรือมี request หนัก ๆ แล้วค้าง ถ้าอยากดูภาพรวมแบบเรียลไทม์: - pm2 monit 5) ทำให้รันอัตโนมัติหลัง reboot (สำคัญมากสำหรับงานจริง) อันนี้คือจุดที่ทำให้ server ดู “มืออาชีพ” ขึ้นทันที: - pm2 startup มันจะโชว์คำสั่งอีกบรรทัดให้เรา copy ไปรันต่อ (ตาม systemd ของเครื่อง) จากนั้นอย่าลืมบันทึกรายการโปรเซส: - pm2 save ทีนี้ต่อให้เครื่องดับ/รีสตาร์ต endpoint ของคุณก็กลับมา online เอง 6) ทริคเล็ก ๆ สำหรับงาน endpoint/API ที่คนเรียกเยอะ ถ้าเครื่องมีหลายคอร์และต้องการรับโหลดมากขึ้น สามารถใช้ cluster mode: - pm2 start app.js -i max --name api-endpoint มันจะสร้างหลาย instance ตามจำนวนคอร์ ช่วยกระจายโหลดได้โดยไม่ต้องแก้โค้ดเยอะ โดยรวมถ้าคุณกำลังทำเว็บหรือ API (endpoint) บน Ubuntu Server แล้วอยากให้บริการเสถียร PM2 คือเครื่องมือที่คุ้มที่สุดตัวหนึ่งครับ: สั่ง start/list/stop ได้ง่าย เห็นสถานะ online ชัดเจน มี logs ให้ไล่ปัญหา และตั้ง startup ให้บูตติดเองได้จริง