So you've built an amazing Node.js application. You've set up your AWS EC2 instance, cloned your project, and you're ready to go live. You SSH into your server, run npm run dev, and... it works! But then you close your terminal, and your website goes down. What happened?
When you close your SSH session, any process you started directly in it gets terminated. This is a common hurdle for developers moving to a server environment. The solution? A process manager. And one of the best for the Node.js ecosystem is PM2.
What is PM2 and Why Should You Use It?
PM2 is a production process manager for Node.js applications that comes with a built-in load balancer. Think of it as a supervisor for your app. It watches over your application and takes care of it.
Here are the key benefits:
It Keeps Your App Alive: If your app crashes for any reason, PM2 will automatically restart it.
It Runs in the Background: You can start your app and safely log out of your server. PM2 will keep it running.
Automatic Startup on Reboot: You can configure PM2 to automatically start your app when the server reboots.
Log Management: It gathers all your app's logs (console.log, errors, etc.) into one easy-to-access place.
Monitoring: It provides a simple way to monitor your app's CPU and memory usage in real-time.
Let's get started and make your application robust and production-ready on your EC2 instance!
Prerequisites
Before we begin, I'll assume you have:
An EC2 instance (like an Amazon Linux or Ubuntu server) running.
Node.js and npm installed on your EC2 instance.
Your Node.js project code is already on the server.
You have run npm install inside your project directory to install its dependencies.
Step-by-Step Guide to Using PM2
Step 1: Install PM2 Globally
First, we need to install PM2 itself. We install it globally so you can use the pm2 command from anywhere in your terminal.
Connect to your EC2 instance and run the following command:
npm install -g pm2This command tells npm (Node Package Manager) to install the pm2 package with the -g flag, which means "globally."
Step 2: Start Your Application with PM2
Now for the fun part! Navigate to your project's root directory. Instead of running npm run dev, you'll ask PM2 to run it for you.
pm2 start npm --name "myapp" -- run devThis command looks a bit complex, so let's break it down:
pm2 start npm: This tells PM2 to start the npm program.
--name "myapp": This is very important. It gives your process a simple, memorable name (myapp). You can change this to whatever you like (e.g., "api-server", "my-blog").
--: This double-dash is a separator. It tells PM2, "Okay, I'm done with my own options. Everything that comes after this is an argument for the npm program."
run dev: These are the arguments passed directly to npm.
So, in simple terms, you just told PM2: "Please run npm run dev for me, keep it running in the background, and let me refer to it by the name myapp."
You'll see a table confirming that your app is now online.
Step 3: Check Your Running Applications
How can you be sure it's running? Use the list command:
pm2 lsThis will show you the same table with a list of all applications currently managed by PM2, along with their status (online), id, and resource usage.
Step 4: View Your Application's Logs
Need to debug or see your console.log() outputs? PM2 makes it easy.
pm2 logs myappThis command will stream your application's logs in real-time. You'll see standard output and any errors right in your terminal. To exit the log view, press Ctrl + C.
Step 5: Make Your App Start on Server Reboot
This is the magic step that ensures your application is truly resilient. We need to tell PM2 to generate a script that will run on system startup.
Run the following command. It will detect your server's operating system (like systemd for modern Linux) and generate a configuration command for you.
pm2 startup systemd -u $(whoami) --hp /home/$(whoami)systemd: The init system used by modern Linux distros (like Amazon Linux 2, Ubuntu 16+).
-u $(whoami): Tells it to run the app as the current user.
--hp /home/$(whoami): Sets the home directory for that user.
After running this, PM2 will give you a new command that you must copy and paste back into the terminal. This second command grants the necessary permissions to the startup script.
Run the command that PM2 gives you! This only needs to be done once per server.
Step 6: Save Your Process List
You've told PM2 how to start on reboot, but you haven't told it what to start. The final step is to save your current list of running applications.
pm2 saveThis command saves the list of apps you currently have running (in our case, myapp) to a file. The startup script you generated in the previous step will read this file on reboot and automatically start all the saved applications.
Important: If you ever add a new app or remove an old one, you must run pm2 save again to update the list!
Everyday PM2 Commands: A Handy Cheatsheet
Your application is now running and configured to restart automatically. Here are some other commands you'll use frequently to manage it.
Restart the app: pm2 restart myapp
(Useful after you've pulled new code changes.)Stop the app: pm2 stop myapp
Delete the app from PM2's list: pm2 delete myapp
Show logs: pm2 logs myapp
Monitor resource usage: pm2 monit
(This shows a live dashboard of CPU and memory usage for all your apps. It's fantastic for a quick health check!)
Conclusion
That's it! You have successfully deployed your Node.js application on an EC2 instance using PM2. You can now close your SSH session with confidence, knowing that your application is:
Running 24/7 in the background.
Protected from crashes with automatic restarts.
Ready to come back online automatically if the server ever reboots.
PM2 is an essential tool for any Node.js developer working with servers. It takes the worry out of process management and lets you focus on what matters: building great applications. Happy coding