Tìm Hiểu Về PM2 – Trình Quản Lý Tiến Trình Ứng Dụng Node.js Cho Người Mới Bắt Đầu

PM2 là công cụ quản lý tiến trình mạnh mẽ giúp các ứng dụng Node.js luôn hoạt động ổn định trong môi trường production. Bài viết này hướng dẫn chi tiết cách cài đặt, chạy, giám sát và triển khai ứng dụng Node.js với PM2 trên VPS, phù hợp cho người mới bắt đầu.

{"type":"doc","content":[{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"I. Giới Thiệu PM2"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Khi bạn chạy ứng dụng Node.js bằng lệnh "},{"type":"text","marks":[{"type":"code"}],"text":"node app.js"},{"type":"text","text":", ứng dụng sẽ dừng nếu có lỗi hoặc khi máy chủ khởi động lại. Đây là vấn đề phổ biến khi triển khai ứng dụng Node.js trong môi trường thực tế."}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"PM2"},{"type":"text","text":" (Process Manager 2) là công cụ giúp giải quyết vấn đề đó. Nó cho phép bạn quản lý tiến trình (process) của ứng dụng Node.js một cách tự động, đảm bảo ứng dụng luôn hoạt động ổn định và có thể giám sát hiệu năng, log, và khởi động lại khi cần."}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Một số tính năng nổi bật của PM2:"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Tự động khởi động lại khi ứng dụng gặp lỗi"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Giám sát RAM, CPU, và log"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Hỗ trợ chế độ "},{"type":"text","marks":[{"type":"bold"}],"text":"cluster"},{"type":"text","text":" để tận dụng đa lõi CPU"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Tự khởi động cùng hệ thống"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Quản lý nhiều ứng dụng qua file cấu hình "},{"type":"text","marks":[{"type":"code"}],"text":"ecosystem.config.js"}]}]}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"II. Cài Đặt PM2"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"1. Cài đặt qua npm"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"PM2 được cài đặt toàn hệ thống thông qua npm:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"npm install -g pm2\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Kiểm tra phiên bản sau khi cài đặt:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"pm2 --version\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"2. Kiểm tra cài đặt"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Nếu bạn thấy PM2 hiển thị phiên bản (ví dụ "},{"type":"text","marks":[{"type":"code"}],"text":"5.4.2"},{"type":"text","text":"), nghĩa là đã cài đặt thành công."}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"III. Chạy Ứng Dụng Node.js Bằng PM2"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"1. Tạo ứng dụng Node.js mẫu"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"// app.js\nconst http = require('http');\n\nconst server = http.createServer((req, res) => {\n res.end('Hello from PM2-managed app!');\n});\n\nserver.listen(3000, () => {\n console.log('Server is running on port 3000');\n});\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"2. Chạy ứng dụng bằng PM2"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"pm2 start app.js\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"PM2 sẽ tự động khởi chạy ứng dụng và đưa ra thông tin tiến trình."}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"3. Kiểm tra danh sách ứng dụng"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"pm2 list\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Bạn sẽ thấy ứng dụng đang ở trạng thái “online” cùng với thông tin CPU, RAM và PID."}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"IV. Một Số Lệnh Cơ Bản Của PM2"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"LệnhChức năng"},{"type":"text","marks":[{"type":"code"}],"text":"pm2 list"},{"type":"text","text":"Hiển thị danh sách ứng dụng đang chạy`pm2 stop <idname>``pm2 restart <idname>``pm2 delete <idname>`"},{"type":"text","marks":[{"type":"code"}],"text":"pm2 logs"},{"type":"text","text":"Xem log ứng dụng"},{"type":"text","marks":[{"type":"code"}],"text":"pm2 monit"},{"type":"text","text":"Theo dõi hiệu năng theo thời gian thực"}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"V. Cấu Hình Tự Khởi Động Khi Server Reboot"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Để đảm bảo ứng dụng tự chạy lại khi server khởi động lại:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"pm2 startup\npm2 save\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Sau khi chạy lệnh "},{"type":"text","marks":[{"type":"code"}],"text":"pm2 startup"},{"type":"text","text":", PM2 sẽ hiển thị hướng dẫn cụ thể để bạn thêm dòng khởi động vào hệ thống."}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"VI. Deploy Node.js Với PM2 Trên VPS"}]},{"type":"image","attrs":{"src":"https://res.cloudinary.com/dwcdztgxn/images/c_scale,w_448,h_245,dpr_2/f_webp,q_auto/v1596725865/ntcde.com/gioi-thieu-pm2-trinh-quan-ly-cac-ung-dung-nodejs_fqq2zc/gioi-thieu-pm2-trinh-quan-ly-cac-ung-dung-nodejs_fqq2zc.png?_i=AA","alt":null,"title":null,"width":null,"height":null}},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"1. Chuẩn bị môi trường"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"VPS chạy "},{"type":"text","marks":[{"type":"bold"}],"text":"Ubuntu 22.04"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Đã cài "},{"type":"text","marks":[{"type":"bold"}],"text":"Node.js, npm và PM2"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Có quyền SSH hoặc root"}]}]}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Kết nối tới VPS:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"ssh ubuntu@<ip_vps>\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Cập nhật hệ thống:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo apt update && sudo apt upgrade -y\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"2. Lấy mã nguồn và cài đặt dependencies"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"git clone https://github.com/<user>/<repo>.git /var/www/myapp\ncd /var/www/myapp\nnpm install\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"3. Chạy ứng dụng bằng PM2"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"pm2 start app.js --name myapp\npm2 save\npm2 startup\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"4. Reverse Proxy với Nginx"}]},{"type":"heading","attrs":{"textAlign":null,"level":4},"content":[{"type":"text","text":"4.1. Cài Nginx"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo apt install nginx -y\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":4},"content":[{"type":"text","text":"4.2. Cấu hình chuyển tiếp"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo nano /etc/nginx/sites-available/myapp\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Nội dung file:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"server {\n listen 80;\n server_name example.com;\n\n location / {\n proxy_pass http://127.0.0.1:3000;\n proxy_set_header Host $host;\n proxy_cache_bypass $http_upgrade;\n }\n}\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":4},"content":[{"type":"text","text":"4.3. Kích hoạt cấu hình"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/\nsudo nginx -t\nsudo systemctl reload nginx\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"VII. Sơ Đồ Quy Trình Hoạt Động Của PM2"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Hình minh họa dưới đây mô tả cách PM2 quản lý ứng dụng Node.js:"},{"type":"hardBreak"},{"type":"text","text":"Người dùng gửi lệnh đến "},{"type":"text","marks":[{"type":"bold"}],"text":"PM2 CLI"},{"type":"text","text":" → PM2 giám sát "},{"type":"text","marks":[{"type":"bold"}],"text":"tiến trình Node.js"},{"type":"text","text":" → khi gặp lỗi, PM2 "},{"type":"text","marks":[{"type":"bold"}],"text":"tự khởi động lại"},{"type":"text","text":" ứng dụng → ghi log và theo dõi hiệu năng liên tục."}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"(Hình minh họa đã được tạo ở trên)"}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"VIII. Kết Luận"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"PM2 là công cụ thiết yếu trong quá trình triển khai ứng dụng Node.js. Với khả năng tự động quản lý tiến trình, giám sát log, và hỗ trợ deploy dễ dàng trên VPS, nó giúp việc vận hành ứng dụng trở nên ổn định và hiệu quả hơn."}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Sau khi làm quen với các tính năng cơ bản, bạn có thể khám phá thêm các chức năng nâng cao như "},{"type":"text","marks":[{"type":"bold"}],"text":"cluster mode"},{"type":"text","text":", "},{"type":"text","marks":[{"type":"bold"}],"text":"ecosystem file"},{"type":"text","text":", hoặc "},{"type":"text","marks":[{"type":"bold"}],"text":"CI/CD với PM2 deploy"},{"type":"text","text":" để đạt hiệu suất tối đa."}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"IX. Tài liệu tham khảo"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"link","attrs":{"href":"https://pm2.keymetrics.io/","target":"_blank","rel":"noopener noreferrer nofollow","class":null}}],"text":"PM2 - Home"}]}]}]},{"type":"paragraph","attrs":{"textAlign":null}}]}