Lesson 11 of 20 · Learn · 20 min
2.4 Put an app on your server
1. Install Node.js and pm2
sudo apt install nodejs npm -y sudo npm install -g pm2
2. The app
mkdir ~/hello cd ~/hello nano app.js
Type this, then save (Ctrl + O, Enter) and exit (Ctrl + X):
const http = require("http");
http.createServer((req, res) => {
res.end("Hello from my app on my own server");
}).listen(3000);Test it on the server: node app.js, then in a second SSH window, curl http://localhost:3000. You'll see the line. Press Ctrl + C in the first window to stop it.
3. Keep it running with pm2
pm2 start app.js --name hello pm2 save pm2 startup
pm2 startup prints one command starting with sudo. Copy it, run it, then run pm2 save again. Now the app starts by itself after a restart.
| Command | What it does |
|---|---|
pm2 list | Shows your apps and whether they're online |
pm2 logs hello | Shows what the app is saying (Ctrl + C to leave) |
pm2 restart hello | Restarts it after a change |
4. Put nginx in front (once your domain points at the server, item 2.6)
sudo nano /etc/nginx/sites-available/hello
server {
listen 80;
server_name app.kwesibuilds.com;
location / {
proxy_pass http://localhost:3000;
}
}Use your own domain. Then turn it on, check it, and reload nginx:
sudo ln -s /etc/nginx/sites-available/hello /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
nginx -t must say syntax is ok and test is successful before you reload. If it doesn't, open the file again and look for a missing ; or }.
Why port 3000 stays closed: the firewall only opens 22, 80 and 443. Visitors reach the app through nginx, never directly. Don't open extra ports unless you know why.
Sign in to save your progress and earn the certificate.

