Deploying a Node.js or Python app from Git: pull, install, restart

Publishing over FTP works, until the day you forget a file. With Git, deployment becomes the same sequence every time: pull the code, install the dependencies, restart. On a PHP site, copying files is enough; on a Node.js or Python application the last two steps are missing, and that is where deployments fail. This article puts the sequence in order, for cPanel and for a VPS.

The sequence, and what each step does

Step Node.js Python
1. Pull the code git pull git pull
2. Install the dependencies npm ci (in cPanel, “Run NPM Install”) pip install -r requirements.txt (in cPanel, “Run Pip Install”)
3. Build, if needed npm run build In Django: python manage.py migrate and collectstatic
4. Restart The application’s button (cPanel), or pm2 restart name (VPS) The application’s button (cPanel), or sudo systemctl restart name (VPS)

In cPanel

1 Create the repository in the account with cPanel’s version control tool, or with git clone in Terminal, in a folder outside the public folder. A private repository needs the server to have access: see the warning below. The tool’s walk-through, and why .git never stays in view, is in publishing your site from a repository.
2 Use that folder as the application root in “Setup Node.js App” or “Setup Python App”. That way git pull updates exactly what the application runs.
3 Enter the application’s environment (the command is at the top of the screen), then pull and install: git pull
npm ci
4 Restart with the button on the application screen. Without the restart, the application keeps serving the old code.

On a VPS: a deployment script

On a VPS you write the sequence once, in a file, and run it with a single command. Example for a Node.js application with PM2 (adapt the folders and the name):#!/bin/bash
set -e
cd /home/appuser/app
git pull --ff-only
npm ci
npm run build
pm2 restart my-app
set -e makes the script stop at the first error, so it does not restart an application halfway through a failed install. For Python, swap the middle lines for .venv/bin/pip install -r requirements.txt and restart the service with sudo systemctl restart name.

Private repository: use a read-only key. Do not put your Git password on the server. Create an SSH key pair on the server (ssh-keygen -t ed25519) and register the public key as a read-only “deploy key” on the service that hosts the repository. If the key leaks, all it can do is read one repository. And never put .env, node_modules or the virtual environment in the repository.
To roll back, Git keeps the history: git log --oneline lists the versions and git checkout identifier goes back to one (then install and restart as above). If you deploy often, test on a subdomain first. Variables and secrets stay out of the code: environment variables and secrets.

Want a server where you can automate the whole deployment? Have a look at our VPS plans.

See the VPS servers

SEE ALSO

Git in cPanel: publishing your site from a repository

Keeping a Node.js app running on a VPS with PM2

Getting a Node.js application running

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $6.60/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?