This guide walks you through setting up and managing a Node.js signaling server using PM2 on an Ubuntu server. You’ll learn how to install PM2, run your server in the background, enable automatic startup on boot, monitor logs, restart or stop processes, and configure Apache as a reverse proxy for secure domain access. Perfect for WebRTC, chat applications, or any real-time Node.js service.
This guide will show you how to:
- Install PM2 to manage your Node.js server
- Keep your signaling server running in the background
- Enable it to start automatically on boot
- Check logs and restart when needed
- Configure Apache Reverse Proxy for domain access
✅ Step 1: Install PM2 Globally
If PM2 isn’t already installed on your system, install it globally using npm:
sudo npm install -g pm2
PM2 is a process manager for Node.js that helps keep your apps alive forever, reload them without downtime, and monitor logs.
✅ Step 2: Navigate to Your Project Directory
Go to the folder where your signaling server file (server.js) is located:
cd /var/www/room.amankhalsa.in/signaling-server
Make sure server.js exists:
ls server.js
✅ Step 3: Start Your Server with PM2
Run your server using PM2:
pm2 start server.js --name signal-room
Here:
server.js → Your Node.js signaling server file
--name signal-room → A custom name for your process (easier to manage later)
Now PM2 is running your signaling server in the background.
✅ Step 4: Save PM2 Process List
To make sure PM2 remembers your running apps:
pm2 save
✅ Step 5: Enable PM2 to Run on Boot
Generate a startup script:
pm2 startup
You’ll see a command like:
sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u ubuntu --hp /home/ubuntu
Copy and paste it exactly as shown. Then run:
pm2 save
This ensures your signaling server starts automatically whenever your VPS reboots.
✅ Step 6: Check Status, Logs, and Manage Your Server
List running processes:
pm2 list
View logs for your process:
pm2 logs signal-room
Restart your server:
pm2 restart signal-room
Stop your server:
pm2 stop signal-room
✅ Step 7: Apache Reverse Proxy Configuration
If you want to access your signaling server via a domain/subdomain (e.g., https://room.amankhalsa.in/signal), configure Apache as a reverse proxy.
In your Apache site config:
ProxyPass /signal http://localhost:3001/
ProxyPassReverse /signal http://localhost:3001/
Reload Apache:
sudo systemctl reload apache2
✅ Step 8: Connecting from Laravel
In your Laravel frontend, connect to the signaling server using:
const socket = io('https://room.amankhalsa.in/signal', {
path: '/signal/socket.io',
auth: {
token: '{{ $token }}'
}
});Restart the process:
pm2 restart signal-room
This restarts the process named signal-room without stopping PM2 or affecting other processes.
Show the list of processes:
pm2 list
This ensures secure, authenticated WebSocket communication.
Final Notes
By using PM2, your Node.js signaling server will:
Run continuously in the background
Restart automatically after crashes or reboots
Provide easy monitoring with pm2 logs and pm2 list
Combined with Apache Reverse Proxy, you get a clean, secure URL for your signaling server without exposing internal ports.
If you want, I can also make a follow-up blog post with a complete server.js signaling server code example so readers can directly test this setup. That would make the tutorial more complete.