Loading Kaushal Patel
All articles

How to Deploy a Django (Python) App on Namecheap — Step by Step (with Git Commands)

Deploy Django on Namecheap — step-by-step guide with Git and SSH commands

Namecheap’s shared hosting isn’t only for WordPress and PHP — its cPanel includes Setup Python App, which runs Python web apps through Passenger. That makes it an affordable home for a Django project, as long as you set it up the right way.

In this guide we’ll take a Django project from your laptop to a live, HTTPS-secured site on Namecheap, using Git and SSH to deploy — and finish with a one-command script for every future update.

Diagram: build on your laptop, store on GitHub, run on Namecheap shared hosting with Apache, Passenger, a virtualenv and a database
How the pieces fit: you push to GitHub, the server pulls, and Passenger runs Django.

What you’ll need

Step 1 — Create the Django project locally

Create a folder, a virtual environment and the project:

mkdir mysite && cd mysite
python3 -m venv .venv
source .venv/bin/activate          # Windows: .venv\Scripts\activate

pip install "django~=5.2" whitenoise python-dotenv
django-admin startproject mysite .

That gives you manage.py and a mysite/ settings package in the current folder. WhiteNoise will serve your CSS and JavaScript in production, and python-dotenv loads secrets from a .env file.

Step 2 — Make the settings production-ready

Open mysite/settings.py and replace the hard-coded secret key, debug flag and allowed hosts so they come from the environment:

import os
from pathlib import Path

from dotenv import load_dotenv

BASE_DIR = Path(__file__).resolve().parent.parent
load_dotenv(BASE_DIR / ".env")  # reads the .env file if it exists

SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "dev-only-insecure-key")
DEBUG = os.environ.get("DJANGO_DEBUG", "False") == "True"
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", "localhost,127.0.0.1").split(",")
CSRF_TRUSTED_ORIGINS = [f"https://{h}" for h in ALLOWED_HOSTS if h not in ("localhost", "127.0.0.1")]

Add WhiteNoise right after Django’s security middleware, and tell Django where to collect static files:

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",  # ← add this line
    # …the rest stay as they are
]

STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"

STORAGES = {
    "default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
    "staticfiles": {"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage"},
}

For local development, create a .env file next to manage.py:

DJANGO_DEBUG=True
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1

Then check it runs with python manage.py runserver and open http://127.0.0.1:8000.

Step 3 — Add the file Passenger needs

Passenger starts your app from a file called passenger_wsgi.py in the project root. Create it — and commit it, so every server gets the same one:

import os
import sys

sys.path.insert(0, os.path.dirname(__file__))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()
Folder tree of the Django project on the server, including passenger_wsgi.py, tmp/restart.txt and stderr.log
What the application folder looks like on the server.

Step 4 — Commit and push to GitHub

First, keep the virtual environment, database, collected files and secrets out of Git. Create a .gitignore:

.venv/
__pycache__/
*.pyc
db.sqlite3
staticfiles/
.env

Save your dependencies, commit, and push to a new private GitHub repository (create it on GitHub first, without a README):

pip freeze > requirements.txt

git init
git add .
git commit -m "Initial Django project"

git remote add origin https://github.com/your-username/mysite.git
git branch -M main
git push -u origin main

Step 5 — Create the Python app in cPanel

Log in to cPanel, open Setup Python App and click Create Application. Fill in:

FieldValue
Python versionThe version you chose, e.g. 3.11
Application rootmysite (a folder in your home directory)
Application URLYour domain
Application startup filepassenger_wsgi.py
Application Entry pointapplication
Illustration of the five fields in cPanel’s Setup Python App create form
The five fields to fill in (an illustration — wording in your cPanel may differ slightly).

Click Create. cPanel builds a virtual environment for the app and shows a command at the top of the page to enter it — something like source /home/USER/virtualenv/mysite/3.11/bin/activate && cd /home/USER/mysite. Copy it; you’ll use it a lot.

Step 6 — Get your code onto the server with Git

Connect over SSH (find your server name and username in Namecheap’s hosting panel or cPanel):

ssh -p 21098 cpanelusername@your-domain.com

Your repository is private, so give the server its own read-only deploy key:

ssh-keygen -t ed25519 -C "namecheap-deploy" -f ~/.ssh/github_deploy -N ""
cat ~/.ssh/github_deploy.pub

Copy the printed key into GitHub: your repository → Settings → Deploy keys → Add deploy key (leave write access off). Then tell SSH to use it for GitHub by adding this to ~/.ssh/config on the server:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github_deploy
  IdentitiesOnly yes

The app folder cPanel created already contains a few files, so instead of cloning into it, turn it into a repository and check out your code:

cd ~/mysite
git init
git remote add origin git@github.com:your-username/mysite.git
git fetch origin
git checkout -f -t origin/main

The -f replaces cPanel’s sample passenger_wsgi.py with yours, and -t links your local main to GitHub so later updates are just git pull.

Step 7 — Install packages and add your secrets

Enter the app’s virtual environment (the command cPanel showed you) and install your requirements:

source /home/USER/virtualenv/mysite/3.11/bin/activate && cd /home/USER/mysite
pip install --upgrade pip
pip install -r requirements.txt

Generate a secret key:

python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())"

Then create the production .env file on the server (with nano .env), using your own domain and the key you just generated:

DJANGO_SECRET_KEY=paste-the-generated-key-here
DJANGO_DEBUG=False
DJANGO_ALLOWED_HOSTS=your-domain.com,www.your-domain.com

Lock it down so only your account can read it: chmod 600 .env. Because settings.py loads this file itself, the same secrets work for the live site and for the commands you run over SSH.

Step 8 (optional) — Use a MySQL database

SQLite works out of the box and is fine for small sites. For MySQL, open MySQL Databases in cPanel, create a database and a user, and add the user to the database with All Privileges. The usual mysqlclient driver often can’t be compiled on shared hosting, so use the pure-Python PyMySQL instead:

pip install pymysql && pip freeze > requirements.txt

In mysite/__init__.py:

import pymysql

pymysql.install_as_MySQLdb()

And in settings.py, reading the credentials from .env:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": os.environ["DB_NAME"],
        "USER": os.environ["DB_USER"],
        "PASSWORD": os.environ["DB_PASSWORD"],
        "HOST": "localhost",
        "OPTIONS": {"charset": "utf8mb4"},
    }
}

Add DB_NAME, DB_USER and DB_PASSWORD to the server’s .env. Django 5.2 needs MariaDB 10.5+ or MySQL 8.0.11+ — phpMyAdmin in cPanel shows which version your server runs.

Step 9 — Migrate, collect static files and go live

Still inside the virtual environment on the server:

python manage.py migrate
python manage.py createsuperuser
python manage.py collectstatic --noinput
python manage.py check --deploy

touch tmp/restart.txt

check --deploy lists security settings worth reviewing, and touching tmp/restart.txt tells Passenger to restart your app (the Restart button in Setup Python App does the same). Visit your domain — Django is live, and /admin/ is ready for the superuser you just created.

Step 10 — Turn on HTTPS

In cPanel, open SSL/TLS Status (or the Namecheap SSL tool on your plan) and make sure your domain has a certificate installed. Once https:// works, add these lines to the end of settings.py so Django always uses secure connections and cookies in production:

if not DEBUG:
    SECURE_SSL_REDIRECT = True
    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True

Step 11 — Ship updates with one command

From now on, the workflow is: commit and push on your laptop, then pull and restart on the server.

Terminal: git push on the laptop, then git pull, pip install, migrate, collectstatic and touch tmp/restart.txt on the server
Every update: push from your laptop, then pull, migrate, collect and restart on the server.

Save the server half as deploy.sh in your project (replace USER and the Python version with yours):

#!/usr/bin/env bash
set -e

source /home/USER/virtualenv/mysite/3.11/bin/activate
cd /home/USER/mysite

git pull
pip install -r requirements.txt
python manage.py migrate --noinput
python manage.py collectstatic --noinput
touch tmp/restart.txt

echo "Deployed ✓"

Make it executable once with chmod +x deploy.sh, commit it, and every future deploy is just:

ssh -p 21098 cpanelusername@your-domain.com "~/mysite/deploy.sh"

Troubleshooting

Your launch checklist

That’s a complete, repeatable Django deployment on Namecheap — Git for your code, a virtualenv for your packages and Passenger to run it all. Prefer a fully managed platform for a JavaScript project instead? Read my guide to deploying a Next.js app on Vercel. And if you get stuck, send me a message.

Kaushal Patel

Entrepreneur · Business Owner · IT Consultant — writing about startups, AI and building products.

Keep reading

Related articles