How to Deploy a Django or Flask App on cPanel with Setup Python App
Django and Flask apps do not need a dedicated server to go live. On cPanel hosting with CloudLinux, the Setup Python App tool creates an isolated virtual environment, connects it to your domain through Phusion Passenger and lets you restart the app with one click. LiteSpeed servers read the same configuration and run the app with their own compatible handler.
This guide covers the full deployment: creating the app, installing requirements, writing passenger_wsgi.py, production settings, static files, database migrations and troubleshooting.
Before you start
- Check that the tool exists. Look for Setup Python App in the Software section of cPanel. If it is missing, ask your host whether your plan includes it.
- Prepare a
requirements.txtwith pinned versions, for example by runningpip freeze > requirements.txtin your local virtual environment. - Match the Python version. Django 5.2 LTS runs on Python 3.10 and newer, and Django 6.0 needs Python 3.12 or newer. Pick the version you developed and tested with.
- Get shell access. SSH or the cPanel Terminal makes migrations and debugging much easier. The cPanel user guide for beginners shows where to find them.
How Python apps run on cPanel
Passenger loads a file called passenger_wsgi.py from your application folder and looks for a WSGI callable named application. Every request to the app's URL is passed to that callable. You never start gunicorn or runserver yourself.
Each app gets its own virtual environment under /home/USER/virtualenv/, so packages for one project do not affect another. The app is started on the first request and may be stopped after a quiet period, which keeps memory free under your account's CloudLinux limits.
Step 1: Create the application
- Open Setup Python App and click Create Application.
- Python version: select the version your project needs.
- Application root: a folder in your home directory, such as
mysite. Keep it outsidepublic_htmlso your code and settings are never downloadable. - Application URL: choose the domain or subdomain. A subdomain such as
app.example.comkeeps the project separate from an existing website. - Application startup file:
passenger_wsgi.py. - Application Entry point:
application. - Passenger log file: for example
/home/USER/logs/mysite.log. - Click Create. cPanel creates the folder, a sample startup file and the virtual environment.
At the top of the application page you will see a command to enter the virtual environment. Copy it. It looks like this:
source /home/USER/virtualenv/mysite/3.12/bin/activate && cd /home/USER/mysite
Step 2: Upload your code
Upload the project into the application root with Git, SFTP or File Manager. For a Django project the layout usually looks like this:
/home/USER/mysite/
passenger_wsgi.py
manage.py
requirements.txt
mysite/
settings.py
urls.py
wsgi.py
blog/
Do not upload your local virtual environment folder. The server builds its own.
Step 3: Install requirements
In the application page, find Configuration files, add requirements.txt and click Run Pip Install. For larger projects, the shell is more reliable because you see every error:
source /home/USER/virtualenv/mysite/3.12/bin/activate && cd /home/USER/mysite
pip install --upgrade pip
pip install -r requirements.txt
Packages with C extensions, such as mysqlclient, compile during installation. If a build fails because development headers are missing, contact support with the full error.
Step 4: Write passenger_wsgi.py
Replace the sample file with one that points at your project.
For Django
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()
For Flask
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
from app import app as application
Here app is the module name (app.py) and the object created with Flask(__name__). Adjust both names to match your project.
Step 5: Production settings
Secrets should not live in your repository. Setup Python App has an Environment variables section where you can add values such as DJANGO_SECRET_KEY and the database password. Then read them in settings.py:
import os
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
DEBUG = False
ALLOWED_HOSTS = ["example.com", "www.example.com"]
CSRF_TRUSTED_ORIGINS = ["https://example.com", "https://www.example.com"]
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "USER_mysite",
"USER": "USER_mysite",
"PASSWORD": os.environ["DB_PASSWORD"],
"HOST": "localhost",
}
}
Create the database and its user first in cPanel under MySQL Databases. cPanel adds your account name as a prefix, so the names look like USER_mysite.
Variables set in the panel reach the web process. When you run manage.py over SSH, they are not set in your shell automatically, so export them for that session or load them from a .env file kept in the application root.
Step 6: Static files
With DEBUG = False, Django stops serving CSS, JavaScript and images. The simplest fix on shared hosting is WhiteNoise, which serves collected static files from inside the app:
pip install whitenoise
# settings.py
STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# the rest of your middleware
]
An alternative is to set STATIC_ROOT to a folder inside the domain's document root, such as /home/USER/public_html/static, so the web server delivers the files directly. Test a known file in the browser afterwards. Flask apps serve their static folder on their own, so no extra step is needed there.
Step 7: Migrations and the admin user
With the virtual environment active, run the usual commands:
python manage.py migrate
python manage.py collectstatic --noinput
python manage.py createsuperuser
Run migrate and collectstatic again after every deployment that changes models or assets. Testing these steps on a copy of the site first is good practice, as described in our article on staging environments.
Deploying updates later
Once the first deployment works, every later release follows the same short routine. Writing it down keeps updates predictable, even when someone else on the team does them.
- Take a database backup, or confirm that last night's backup exists.
- Upload or pull the new code into the application root.
- Activate the virtual environment and run
pip install -r requirements.txtif dependencies changed. - Run
python manage.py migrateandpython manage.py collectstatic --noinput. - Restart the application and open the main pages, the login and one form to confirm they work.
If something breaks, restore the previous code, restart, and investigate on a copy instead of the live site.
Restarting and reading logs
Python code stays loaded in memory. After you change code, settings or packages, click Restart on the application page, or touch the restart file from SSH:
mkdir -p /home/USER/mysite/tmp
touch /home/USER/mysite/tmp/restart.txt
If the site shows an error page, read the Passenger log file you set in Step 1. On LiteSpeed servers, errors written to stderr also appear in stderr.log inside the application root. A quick way to find syntax and import errors is to run the startup file directly:
python passenger_wsgi.py
| Symptom | Likely cause | What to do |
|---|---|---|
| ModuleNotFoundError | Package missing or wrong environment | Activate the environment and install from requirements.txt |
| DisallowedHost | Domain not in ALLOWED_HOSTS | Add the domain and restart |
| Pages load without styling | Static files not collected or not served | Run collectstatic, check WhiteNoise or STATIC_ROOT |
| CSRF verification failed on login | HTTPS origin not trusted | Set CSRF_TRUSTED_ORIGINS with https URLs |
| Code changes do not appear | App not restarted | Click Restart or touch tmp/restart.txt |
For background on the 503 error that Passenger shows when an app fails to start, see 502 and 503 errors explained.
Frequently asked questions
Can I run FastAPI or other ASGI apps?
Passenger speaks WSGI. You can wrap a simple ASGI app with the a2wsgi package (application = ASGIMiddleware(app)), but you lose the benefits of async I/O. Apps that depend on WebSockets or long-lived async tasks are better placed on a VPS.
Where do I run Celery or scheduled jobs?
A permanent Celery worker does not fit shared hosting limits. For periodic work, call a management command from a cron job, using the full path to the Python binary inside your virtual environment.
Can I use SQLite instead of MySQL?
Yes, for small sites with few writes. Keep the database file in the application root, never inside public_html, and include it in your backups.
Why do I see an "It works!" page instead of my site?
That text comes from the sample startup file cPanel creates with the app. Replace passenger_wsgi.py with the version from Step 4 and restart the application.
Hostinq.com shared hosting runs cPanel on CloudLinux with LiteSpeed Enterprise, nightly backups and support 09:00-03:00. You can compare plans on the shared hosting page, and if you want to confirm Setup Python App on a specific plan before ordering, ask our support team through the contact page.