[Node.JS Course] Running Services with PM2 and Nginx
![[Node.JS Course] Running Services with PM2 and Nginx](/uploads/editor/Node-JS-강좌-19강-PM2와-Nginx를-이용한-서비스-운영하기-20260711-aa7c66.webp)
Hello.
We've now implemented all core features of our real-time chat service.
The program currently runs correctly but only in the development environment.
To operate a live service, the server must run continuously and automatically recover from errors.
In this lesson, we'll build a stable operating environment using PM2 and Nginx.
Difference Between Development and Production Environments
In development, Node.js servers are typically started directly from the terminal.
However, closing the terminal stops the server as well.
For live services that need to run 24/7, a dedicated process management tool is required.
This is where PM2 comes in.
What Is PM2?
PM2 is a process manager designed for stable Node.js application operation.
It offers features such as:
Running servers in the background
Automatic restarts
Log management
CPU and memory monitoring
Automatic startup after server reboot
It's one of the most commonly used tools for operating Node.js services.
Why Use PM2?
With PM2, if a server unexpectedly stops, it automatically restarts.
It also allows managing multiple Node.js services simultaneously.
For example:
Chat server
API server
Admin server
You can easily monitor the status of each service.
Log Management
As services operate, various errors may occur.
PM2 automatically saves:
Execution logs
Error logs
Restart history
Reviewing these logs helps quickly identify and fix issues.
What Is Nginx?
Nginx is a fast and reliable web server.
It acts as a reverse proxy in front of Node.js, handling tasks such as:
HTTPS processing
Serving static files
Request distribution
Security enhancement
Most Node.js services are operated with Nginx.
What Is a Reverse Proxy?
A reverse proxy receives user requests first, then forwards them to the actual Node.js server.
Users connect to Nginx,
but the actual request is handled by the internal Node.js server.
This structure provides benefits for both security and performance.
Socket.IO and Nginx Configuration
Real-time chat uses not only standard HTTP requests but also WebSocket connections.
Therefore, Nginx requires specific configuration to ensure WebSockets work properly.
With this setup, Socket.IO can be used reliably.
Serving Static Files
Files like images, CSS, and JavaScript rarely change.
Having Nginx handle these files reduces the load on Node.js.
This improves response speed and server performance.
Running Multiple Services
On a single Ubuntu server, multiple Node.js services can run simultaneously.
For example:
Chat service
Blog
Admin page
API server
Each can be executed on different ports,
and Nginx routes requests based on domain names.
Checking Service Status
PM2 allows easy monitoring of currently running servers:
Running status
CPU usage
Memory usage
Uptime
This feature is very convenient for managing live services.
Setting Up Automatic Startup
After an Ubuntu server reboot, the Node.js server should automatically restart as well.
PM2 provides this functionality.
Once configured, services will automatically recover after a server restart.
This is highly recommended for production environments.
Future Scalability
The environment we've set up today can be expanded with additional features:
Operating multiple servers
Load balancing
Redis integration
Docker deployment
Kubernetes operation
This allows scaling to larger service architectures.
Summary of This Lesson
In this lesson, we learned how to manage Node.js processes with PM2 and configure Nginx as a reverse proxy.
We also covered essential production features like log management, automatic restarts, WebSocket configuration, and static file handling.
Now our chat service is ready for stable operation in a real-world environment.
Next Lesson Preview
In the next lesson, we'll learn about deploying on Ubuntu servers with HTTPS implementation and practical operational tips.
We'll deploy our real-time chat service to a live Ubuntu server, apply HTTPS, and complete it as a fully functional service accessible to all users.
FAQ
What is PM2 used for?
PM2 manages Node.js processes, providing automatic restarts, background execution, and monitoring.
Why use Nginx with Node.js?
Nginx acts as a reverse proxy to handle HTTPS, static files, and improve security and performance.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Node.JS 강좌] 19강 PM2와 Nginx를 이용한 서비스 운영하기