A reliable deployment has three parts: the app runs as a service under its own user, nginx handles the public connections and HTTPS, and you know how to update it. If you are logged in as root, leave out sudo.
sudo apt install -y git
sudo adduser --system --group --home /opt/myapp myapp
sudo -H -u myapp git clone https://example.com/you/myapp.git /opt/myapp/app
cd /opt/myapp/app
sudo -H -u myapp npm ci --omit=dev
For Python, instead of the last line (needs sudo apt install -y python3-venv):
sudo -H -u myapp python3 -m venv /opt/myapp/venv
sudo -H -u myapp /opt/myapp/venv/bin/pip install -r requirements.txt
Install a current Node.js LTS (for example from NodeSource) or Python 3 from your distribution.
Create /etc/systemd/system/myapp.service:
[Unit]
Description=My app
After=network.target
[Service]
User=myapp
WorkingDirectory=/opt/myapp/app
EnvironmentFile=-/opt/myapp/app.env
ExecStart=/usr/bin/node server.js
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
For Python replace the ExecStart line with your server command, for example ExecStart=/opt/myapp/venv/bin/gunicorn -b 127.0.0.1:3000 app:app. Keep secrets in /opt/myapp/app.env (chmod 600, owned by myapp). Then:
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
sudo journalctl -u myapp -fMake the app listen only on 127.0.0.1 and put nginx in front of it with a free certificate: follow nginx reverse proxy with HTTPS. Open only ports 80, 443 and SSH in the firewall.
sudo -H -u myapp git -C /opt/myapp/app pull, then install dependencies as above.sudo systemctl restart myapp, then check journalctl -u myapp -e.Sizing: a small app runs on 1 to 2 GB of RAM; see Fast VPS from $7.99 a month.
How do I keep my app running after a crash or reboot?
Run it as a systemd service with Restart=always and enable it with systemctl enable.
Should the app run as root?
No. Create a separate user and run the service under it.
Where do I keep secrets?
In an environment file with restricted permissions, never in the code or a public repository.
How do I put HTTPS in front of the app?
Use nginx as a reverse proxy with a free Let's Encrypt certificate from Certbot.