Running a Node.js app with PM2 and Nginx
Keep the app running, restart it on boot and serve it on your domain.
# Node.js LTS from NodeSource
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
apt install -y nodejs
npm install -g pm2
# start your app (from its folder)
pm2 start npm --name myapp -- start
pm2 save
pm2 startup # follow the command it printsServe it on your domain with Nginx
In /etc/nginx/sites-available/example.com:
server {
server_name example.com www.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Enable it, reload Nginx, then add HTTPS with Certbot.