Skip to content

How to Deploy Python and Node.js Apps on VPS

Python suits API backends, automation and data processing. Node.js suits JavaScript servers and real-time applications. For production, run each service under a non-root account, supervise it with systemd and expose it through Nginx.

This walkthrough assumes Ubuntu 24.04, SSH access and a DNS name such as app.example.com. Replace examples with your own domain.

Terminal window
sudo apt update
sudo apt install -y python3 python3-venv python3-pip nginx nodejs npm
python3 --version
node --version

Check that your installed Node.js release is still supported; Ubuntu’s stock version may not be the latest LTS. Use the official Node.js distributions for a newer supported release when necessary.

Terminal window
sudo useradd --system --create-home --shell /usr/sbin/nologin appsvc || true
sudo mkdir -p /opt/py-api
sudo chown appsvc:appsvc /opt/py-api
sudo -u appsvc python3 -m venv /opt/py-api/.venv
sudo -u appsvc /opt/py-api/.venv/bin/pip install fastapi uvicorn
sudo tee /opt/py-api/main.py >/dev/null <<'EOF'
from fastapi import FastAPI
app = FastAPI()
@app.get("/health")
def health():
return {"status": "ok"}
EOF
sudo chown appsvc:appsvc /opt/py-api/main.py

Create /etc/systemd/system/py-api.service:

[Unit]
Description=Python FastAPI service
After=network-online.target
[Service]
User=appsvc
WorkingDirectory=/opt/py-api
ExecStart=/opt/py-api/.venv/bin/uvicorn main:app --host 127.0.0.1 --port 8001
Restart=on-failure
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target

Then run:

Terminal window
sudo systemctl daemon-reload
sudo systemctl enable --now py-api
curl http://127.0.0.1:8001/health

Create an isolated app directory and a small HTTP server:

Terminal window
sudo mkdir -p /opt/node-api
sudo tee /opt/node-api/server.mjs >/dev/null <<'EOF'
import http from 'node:http';
http.createServer((_req, res) => {
res.writeHead(200, {'content-type': 'application/json'});
res.end(JSON.stringify({status: 'ok'}));
}).listen(8002, '127.0.0.1');
EOF
sudo chown -R appsvc:appsvc /opt/node-api

Create /etc/systemd/system/node-api.service:

[Unit]
Description=Node API service
After=network-online.target
[Service]
User=appsvc
WorkingDirectory=/opt/node-api
ExecStart=/usr/bin/node /opt/node-api/server.mjs
Restart=on-failure
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target

If Node was installed outside /usr/bin, adjust ExecStart after checking command -v node. Start it with systemctl daemon-reload and systemctl enable --now node-api.

For the Python API, create a site pointing to port 8001 (use 8002 for the Node example):

server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Enable the site, run sudo nginx -t, reload Nginx, then issue a TLS certificate using Certbot after DNS is configured. Do not serve production APIs permanently over plain HTTP.

Terminal window
sudo systemctl status py-api
sudo journalctl -u py-api -n 30 --no-pager
curl -I https://app.example.com/health

Store credentials in root-readable environment files, restrict permissions, monitor memory and CPU, and back up application state. Avoid exposing development/debug modes to the internet.


Need a VPS for this project? Compare ProstoHosting VPS plans.