EN · $ USD
Developer and Application Servers

How to Deploy a Python (Django/Flask) App in cPanel

7 min read Updated: Sunucu Atlas technical team

In short

To deploy a Python app in cPanel, you create a virtual environment in the Setup Python App tool by choosing the Python version, app folder and URL. Then you expose your Flask or Django WSGI object as application in passenger_wsgi.py, install pip dependencies from requirements.txt, collect Django static files into the web root with collectstatic, and restart the app.

In this guide
  1. Key Concepts
  2. 1. Create the Python App
  3. 2. Upload Your Code and Install Dependencies
  4. 3. Configure passenger_wsgi.py
  5. 4. Serve Static Files
  6. 5. Restarting and Debugging
  7. Update Workflow
  8. Limits of Shared Hosting
  9. Next Step

To deploy a Python app in cPanel, you use the Setup Python App tool: it creates a virtual environment (virtualenv) with the Python version you choose, connects your app to the web through the Passenger application server, and reads passenger_wsgi.py as the entry point. Your job is to expose your Flask or Django app correctly in that file, install dependencies with pip, and set up how static files are served. This guide gives you working configurations for both frameworks, step by step.

Key Concepts

  • WSGI (Web Server Gateway Interface): the standard Python web apps use to talk to a web server. Flask and Django are WSGI-compatible.
  • Passenger: the application server that runs Python and Node.js apps in cPanel. It starts your app when needed and forwards requests to it.
  • passenger_wsgi.py: the entry file Passenger looks for. It must contain a WSGI object named application.
  • Virtual environment: an app-specific Python interpreter and package folder. It lets you install the versions you want without touching system packages.

For this tool to appear, your plan must include the Python application manager. Despite the name, Sunucu Atlas Laravel hosting plans aren't just for PHP; they come with the Node.js and Python application manager, Terminal/SSH access and Git.

1. Create the Python App

  1. In cPanel, open the Setup Python App tool under Software.
  2. Click the Create Application button.
  3. Python version: choose the version your project supports. Each Django release supports specific Python versions; pick the one closest to what you use locally.
  4. Application root: the folder where your code will live, for example pyapp. It must be outside public_html.
  5. Application URL: the domain or subdomain the app will be served from.
  6. Application startup file: leave it as passenger_wsgi.py.
  7. Application Entry point: leave it as application.
  8. Click Create.

The tool generates a sample passenger_wsgi.py file in the application root and shows a command at the top of the page for entering the virtual environment. If you see a test output like "It works!" when you open the URL, the basic connection is ready.

2. Upload Your Code and Install Dependencies

Copy your project files to the pyapp folder with File Manager, FTP or Git. Don't upload your local virtual environment folder (venv, .venv). You need a requirements.txt file listing your dependencies; if you don't have one, create it locally with pip freeze > requirements.txt.

Install from the interface

On the app's edit screen, type requirements.txt in the Configuration files section, click Add, then click the Run Pip Install button.

Install from the terminal

Copy the command shown on screen; it looks like this:

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

The version number in the path depends on the Python version you selected. Working in the terminal is more practical because you see install errors in full; for connecting, see the cPanel SSH access and Terminal guide.

MySQL tip: the mysqlclient package needs to compile during install and can fail on shared hosting because header files are missing. In that case, install the pure-Python PyMySQL package and add these two lines at the top of Django's settings.py:

import pymysql
pymysql.install_as_MySQLdb()

3. Configure passenger_wsgi.py

For Flask

If your Flask app is defined in app.py as app = Flask(__name__), the entry file is this simple:

import os
import sys

sys.path.insert(0, os.path.dirname(__file__))

from app import app as application

Passenger looks for an object named application; that's why you need to rename your Flask object with as application if it has a different name.

For Django

If your project folder (the one containing settings.py and wsgi.py) is named proje:

import os
import sys

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

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Before going live, be sure to update these settings in settings.py:

DEBUG = False
ALLOWED_HOSTS = ["alanadiniz.com", "www.alanadiniz.com"]
CSRF_TRUSTED_ORIGINS = ["https://alanadiniz.com", "https://www.alanadiniz.com"]

Instead of hardcoding values like the database password and SECRET_KEY, define them under Environment variables on the edit screen and read them with os.environ.get("SECRET_KEY"). To create the database in cPanel, see the create a MySQL database and user in cPanel guide. Then, inside the virtual environment, create the tables:

python manage.py migrate
python manage.py createsuperuser

4. Serve Static Files

When DEBUG = False, Django doesn't serve CSS, JavaScript and images itself. They need to be served directly by the web server. In cPanel, the most practical approach is to collect static files into a folder under the domain's web root; the web server delivers existing files without ever touching the app.

# settings.py
STATIC_URL = "/static/"
STATIC_ROOT = "/home/kullaniciadi/public_html/static"

MEDIA_URL = "/media/"
MEDIA_ROOT = "/home/kullaniciadi/public_html/media"
python manage.py collectstatic --noinput

If your app is served on a subdomain, use that subdomain's document root instead of public_html. Alternatively, you can serve static files from the app itself with the whitenoise package; it's easy to set up, but every static request goes through the Python process, so collecting files into the web root is more efficient for busy sites. In Flask, files in the static folder are served through the app; if you expect high traffic, you can copy them under the web root the same way.

5. Restarting and Debugging

Restart the app every time the code, settings or an environment variable changes. Use the Restart button on the Setup Python App screen, or run this in the terminal:

cd ~/pyapp
mkdir -p tmp && touch tmp/restart.txt
SymptomLikely causeFix
"Web application could not be started"Import error in passenger_wsgi.pyRun python passenger_wsgi.py inside the virtual environment to see the error
ModuleNotFoundErrorThe package isn't installed in the virtual environmentActivate the right environment and run pip install -r requirements.txt
Django "Bad Request (400)"The domain isn't in ALLOWED_HOSTSAdd the domain and its www version
Page loads but has no CSScollectstatic wasn't run or STATIC_ROOT is wrongCheck the path and run the command again
CSRF error on formsThe HTTPS origin isn't trustedAdd the CSRF_TRUSTED_ORIGINS setting

App errors are usually written to stderr.log in the application root or to the Passenger log file; check the last lines with tail -n 50 stderr.log. For Django's pre-launch checklist, the official deployment checklist is a good resource.

Update Workflow

Once the app is live, following this order for every new release avoids surprises:

  1. Activate the virtual environment and switch to the app folder (the command on the Setup Python App screen).
  2. Pull the new code with git pull, or upload the files.
  3. If requirements.txt changed, run pip install -r requirements.txt.
  4. In Django, run python manage.py migrate if there are new migrations, and python manage.py collectstatic --noinput if static files changed.
  5. Restart the app with touch tmp/restart.txt and check the site.

Backing up the database from cPanel before updates that change the schema makes it easy to recover from a bad migration. It's also a good habit to put these steps in a script and run them with one command, so no step is forgotten and everyone on the team follows the same order.

Limits of Shared Hosting

Setup Python App is ideal for web apps that work on a request-response basis. Celery workers, always-on Telegram bots, heavy machine learning models or projects that need Docker run better on a server with root access. You can use cron for scheduled tasks; when calling Django management commands from cron, use the full path to the Python binary in the virtual environment. For example, to run a cleanup command every night at 03:00, you can add this line under Cron Jobs in cPanel:

0 3 * * * cd /home/kullaniciadi/pyapp && /home/kullaniciadi/virtualenv/pyapp/3.11/bin/python manage.py clearsessions >> /dev/null 2>&1

Take the virtual environment path from the command on your own app screen. For more on the cron interface, see the how to set up a cron job in cPanel guide. If your project starts outgrowing these limits, planning a move from shared hosting to a server with root access is far less painful than fixing problems after the fact.

Next Step

If you want to manage your Flask API or Django project alongside your domain, email and database in a single cPanel account, check out our Laravel hosting plans with the Python application manager. SA-DEV-1 is enough for a small project, while the higher plans offer more addon domains, RAM and CPU for projects that need multiple apps and more databases. Servers are in Istanbul; if you get stuck during setup, you can open a support ticket with the Sunucu Atlas team.

Frequently asked questions

Which file does a Python app need in cPanel?

The application root must contain a passenger_wsgi.py file that exposes a WSGI object named application. In Flask you assign your app object to that name; in Django you assign the result of get_wsgi_application().

Why aren't Django static files loading in cPanel?

With DEBUG off, Django doesn't serve static files itself. Set STATIC_ROOT to a folder under the domain's web root and run collectstatic, or use the whitenoise package.

mysqlclient fails to install. What should I do?

Shared hosting may lack the header files needed to compile it. Install the pure-Python PyMySQL package and add pymysql.install_as_MySQLdb() to settings.py to work around the problem.

Why doesn't my app update after I change the code?

A Python app runs loaded in memory. Click Restart on the Setup Python App screen or touch the tmp/restart.txt file in the application root.

Can Celery or an always-on bot run in cPanel?

On shared hosting, persistent background processes can be terminated because of resource limits. A server with root access is a better fit for these workloads; cron is enough for simple periodic jobs.

For this guide

Recommended plans

Our Laravel hosting plans include the Python application manager, Terminal/SSH and Git in cPanel, so you can deploy your Flask or Django project in a virtual environment in just a few steps.

SA-DEV-1

cPanel
Recommended

40 GB NVMe disk

$0.69/mo

+VAT

  • Processor2 cores
  • Memory2 GB
  • LocationIstanbul
  • Terminal / SSH Access (cPanel)
  • Composer and Git
  • Node.js and Python App Manager
  • PHP version selection (up to 8.5)

SA-DEV-2

cPanel

60 GB NVMe disk

$0.98/mo

+VAT

  • Processor3 cores
  • Memory3 GB
  • LocationIstanbul
  • Terminal / SSH Access (cPanel)
  • Composer and Git
  • Node.js and Python App Manager
  • PHP version selection (up to 8.5)

SA-DEV-3

cPanel

80 GB NVMe disk

$1.37/mo

+VAT

  • Processor4 cores
  • Memory4 GB
  • LocationIstanbul
  • Terminal / SSH Access (cPanel)
  • Composer and Git
  • Node.js and Python App Manager
  • PHP version selection (up to 8.5)

Laravel Hosting — tüm paketler

Need help with setup?

Send questions about your service in a support ticket, and our technical team will walk you through it step by step.