Hey there! Ready to get your Node.js app out into the wild using just your server’s public IP? Let’s walk through this step-by-step—no domain name required. Think of this as the "launch now, fancy domain later" approach. 😊
What You’ll Need
- A Node.js app that’s ready to roll (already on your Ubuntu server).
- SSH access to your server (you’re probably already logged in!).
- Your server’s public IP address (check your hosting provider’s dashboard if you’re unsure).
1. Prep Your Node.js App
First, navigate to your app’s directory on the server. You know the drill:
cd /path/to/your/app
2. Install PM2: Your App’s Bodyguard
PM2 keeps your app alive and kicking, even if it crashes. Install it globally with:
npm install pm2 -g
3. Fire Up Your App with PM2
Start your app and let PM2 manage it. Replace app.js
with your actual entry file (like server.js
or index.js
):
pm2 start app.js
Now your app runs in the background—no more panicking if you close the terminal!
4. Make PM2 Autostart (Because Reboots Happen)
Want your app to restart automatically if the server reboots? Run:
pm2 startup
Follow the instructions it gives you—this part is crucial! It usually involves copying/pasting a command it generates.
5. Install Nginx (Your Traffic Director)
Nginx will act as a middleman, forwarding requests to your Node.js app. Install it:
sudo apt update && sudo apt install nginx -y
6. Configure Nginx to Route Traffic
Time to tell Nginx where to send incoming requests. Let’s create a config file:
sudo nano /etc/nginx/sites-available/your-app-name
Paste this configuration, replacing YOUR_PUBLIC_IP
with your actual IP (and 3000
with your app’s port if it’s different):
server {
listen 80;
server_name YOUR_PUBLIC_IP; # Example: 123.123.123.123
location / {
proxy_pass http://localhost:3000; # Your Node.js app's port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
What’s happening here?
- Nginx listens on port 80 (default HTTP port).
- Requests to your IP get forwarded to your Node.js app running locally on port 3000.
- The
proxy_set_header
lines ensure websockets and other features work smoothly.
7. Test Your Nginx Config (Don’t Skip This!)
Typos happen. Validate your config with:
sudo nginx -t
If you see nginx: configuration is OK
, you’re golden! If not, check for typos in the config file.
8. Activate the Config & Reload Nginx
Enable your new config by linking it:
sudo ln -s /etc/nginx/sites-available/your-app-name /etc/nginx/sites-enabled/
Then reload Nginx to apply changes:
sudo systemctl reload nginx
9. Open the Firewall Gate (If You Have One)
Using UFW? Unblock port 80 so traffic can flow:
sudo ufw allow 80/tcp
(Skip this if you’re not using a firewall, but most cloud servers have one enabled by default.)
10. Celebrate! Test Your Live App
Open your browser and visit http://YOUR_PUBLIC_IP
. If all went well, your app should be live! 🎉
Troubleshooting Tips
-
Not working? Double-check:
- Your Node.js app is actually running (
pm2 list
). - The port in your Nginx config matches your app’s port.
- The firewall allows port 80 (
sudo ufw status
).
- Your Node.js app is actually running (
- Need to restart Nginx later? Use:
sudo systemctl restart nginx
You did it! Your app is now accessible to anyone with your server’s IP. When you’re ready to level up, consider adding a domain name and HTTPS (Let’s Encrypt is free!). But for now, bask in the glory of a live app. 🚀
Top comments (7)
Cool!
⚡⚡
Awesome! Thanks!
🔗
Great 🚦
Is any setup required on the router?
no just configure your firewall correctly