การจัดการกระบวนการด้วย PM2
เรียนรู้การใช้ PM2 เพื่อจัดการ ตรวจสอบ และขยายแอปพลิเคชัน Node.js ในสภาพแวดล้อมการใช้งานจริง
การจัดการกระบวนการด้วย PM2 เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Node.js Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is PM2?
Welcome to process management with PM2! PM2 stands for Process Manager 2, and it's a powerful tool for Node.js applications.
In production, you need your application to run reliably, restart if it crashes, and utilize server resources efficiently. PM2 handles all of this.
- Keep Alive: Automatically restarts your app if it crashes.
- Monitoring: Provides insights into your app's performance.
- Scaling: Easily run multiple instances of your app for better performance.
Installing PM2 Globally
Getting started with PM2 is straightforward. You install it globally using npm, so it's available as a command-line tool on your system.
Open your terminal and run the following command:
npm install -g pm2
This command installs the PM2 CLI (Command Line Interface) globally, allowing you to use pm2 commands from any directory.
Starting Your First App
Once installed, you can start any Node.js application with a simple command. PM2 will keep it running in the background, even if your terminal closes!
First, here's a basic Node.js server:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from PM2!\n');
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});Launching with PM2
To start the server from the previous scene (let's say it's saved as app.js) with PM2, you'd run:
pm2 start app.js --name my-first-app
The --name flag gives your process a friendly identifier. PM2 will then list your running application, showing its status, uptime, and other details.
If your app crashes, PM2 will automatically restart it, ensuring high availability!
Monitoring Your Processes
PM2 offers excellent tools to monitor your running applications. You can quickly see a list of all managed processes and their statuses.
pm2 list: Shows a table of all your PM2-managed processes, including their ID, name, status, CPU usage, memory usage, and uptime.pm2 monit: Provides a dashboard-like view in your terminal, displaying real-time CPU, memory, and log data for all your applications. It's great for quick health checks.
Stopping & Restarting Apps
Managing the lifecycle of your applications is simple with PM2. You can stop, restart, or delete processes using their name or ID.
pm2 stop my-first-app: Stops the application named 'my-first-app'.pm2 restart my-first-app: Restarts the application. Useful after code changes.pm2 delete my-first-app: Stops the application and removes it from PM2's process list.pm2 stop all: Stops all running applications managed by PM2.
These commands give you full control over your deployed Node.js services.
Using a Configuration File
For more complex setups or managing multiple applications, PM2 allows you to use a configuration file (e.g., ecosystem.config.js). This file defines your applications, environment variables, and other settings.
Here's a simple app for demonstration:
const http = require('http');
const port = process.env.PORT || 3001;
http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Hello from config app on port ${port} (PID: ${process.pid})!\n`);
}).listen(port, () => {
console.log(`Config demo server running on port ${port}`);
});Ecosystem Config Example
Now, let's define an ecosystem.config.js to manage this app:
module.exports = {
apps : [{
name: "my-config-app",
script: "app-config-demo.js",
instances: 1,
exec_mode: "fork",
env: {
NODE_ENV: "development",
PORT: 3001
},
env_production: {
NODE_ENV: "production",
PORT: 80
}
}]
};
To start this app using the config file, you'd run: pm2 start ecosystem.config.js. This is great for version control and consistent deployments.
Scaling with Clustering Mode
Node.js is single-threaded, but modern servers have multiple CPU cores. PM2's clustering mode allows you to run multiple instances of your app, distributing the load across all available cores.
Here's a simple app that shows its process ID:
const http = require('http');
const process = require('process');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(`Hello from instance ${process.pid}!\n`);
});
const PORT = 3002;
server.listen(PORT, () => {
console.log(`Cluster server ${process.pid} running on port ${PORT}`);
});Running in Cluster Mode
To start the app from the previous scene (e.g., app-cluster.js) in cluster mode, you can use:
pm2 start app-cluster.js -i max
The -i max flag tells PM2 to launch as many instances as your CPU has cores. PM2 will then act as a load balancer, distributing incoming requests among these instances. This significantly improves performance and resilience.
PM2 Quick Check
You've learned how to manage, monitor, and scale your Node.js applications with PM2. Let's test your understanding!
Recap & Next Steps
Great job! You've successfully learned the fundamentals of process management with PM2.
PM2 is an essential tool for deploying and maintaining Node.js applications in production, offering features like automatic restarts, monitoring, and easy scaling through clustering.
Keep exploring PM2's documentation for more advanced features, such as log management, startup scripts, and custom metrics. Happy deploying!
คำถามที่พบบ่อย
บทเรียน “การจัดการกระบวนการด้วย PM2” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการกระบวนการด้วย PM2” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการกระบวนการด้วย PM2”
เรียนรู้การใช้ PM2 เพื่อจัดการ ตรวจสอบ และขยายแอปพลิเคชัน Node.js ในสภาพแวดล้อมการใช้งานจริง คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดการกระบวนการด้วย PM2” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดคอนเทนเนอร์ด้วย Docker
- การนำไปใช้งานบนคลาวด์ (AWS/Heroku)
- การจัดการกระบวนการด้วย PM2
- ไปป์ไลน์ CI/CD ด้วย GitHub Actions