Knowledgebase

How to deploy a Python, Django or Flask app in cPanel (Setup Python App)

EgWeb cPanel hosting can run Python web apps through the Setup Python App tool. This guide shows you how to deploy a Python, Django or Flask app in cPanel: create the app, install your requirements in its virtual environment, connect it with the WSGI file, and fix the most common errors.

Before you start

  • Your project should have a requirements.txt file listing its packages.
  • The app runs through Phusion Passenger using a WSGI entry point. Most Django and Flask projects work this way.
  • If you cannot see Setup Python App in cPanel, contact support to check your plan.

Step 1: Create the application

  1. Log in to cPanel.
  2. In the Software section, click Setup Python App, then Create Application.
  3. Python version: choose the version your project was developed with.
  4. Application root: a folder in your home directory, for example myapp. Keep it outside public_html.
  5. Application URL: the domain or subdomain where the app should answer.
  6. Application startup file: leave passenger_wsgi.py.
  7. Application Entry point: leave application.
  8. Click Create. cPanel creates the folder, a virtual environment and a sample passenger_wsgi.py.

Step 2: Upload your project

Upload your project files into the application root with File Manager, FTP or Git. Keep the generated passenger_wsgi.py and edit it in the next step.

Step 3: Install requirements

  1. On the app page, copy the command at the top that enters the virtual environment.
  2. Open Terminal in cPanel, paste the command, then install your packages:
source /home/USER/virtualenv/myapp/3.11/bin/activate && cd /home/USER/myapp
pip install -r requirements.txt

Use the exact command from your own app page. You can also add requirements.txt under Configuration files on the app page and click Run Pip Install.

Step 4: Connect your app in passenger_wsgi.py

Replace the content of passenger_wsgi.py so it imports your app as application.

Flask (if your Flask object is called app inside app.py):

from app import app as application

Django (if your project folder is called mysite):

from mysite.wsgi import application

Step 5: Django settings for production

  1. Set DEBUG = False and add your domain to ALLOWED_HOSTS.
  2. Keep SECRET_KEY and the database password in environment variables (add them on the app page).
  3. For MySQL, create the database in cPanel (see How to create a MySQL database and user) and use localhost as the host.
  4. Static files: set STATIC_ROOT to a folder inside the domain's document root, for example /home/USER/public_html/static, so the web server serves them directly. Or use the WhiteNoise package.
  5. Run the usual commands inside the virtual environment:
python manage.py migrate
python manage.py collectstatic --noinput

Step 6: Restart and test

Click Restart on the app page, then open your URL. Restart again after every code or settings change.

Tip: Turn on SSL for the domain first (see How to get a free SSL certificate (AutoSSL)), and in Django set CSRF_TRUSTED_ORIGINS to your https:// domain if forms fail with a CSRF error.

Common problems

  • "We're sorry, but something went wrong" or 500 error: usually an import error in passenger_wsgi.py or a missing package. Check the log (fill the Passenger log file field on the app page) and read the error logs.
  • mysqlclient fails to install: it needs to compile. Use PyMySQL instead and add import pymysql; pymysql.install_as_MySQLdb() at the top of your settings.
  • CSS and images missing in Django: collectstatic was not run or STATIC_ROOT is not inside the document root.
  • DisallowedHost error: add the domain (with and without www) to ALLOWED_HOSTS.

Frequently asked questions

Can I run FastAPI?

Passenger here uses WSGI. FastAPI is an ASGI framework, so it needs an adapter (for example a2wsgi) and you lose some async features. For heavy async apps, use a VPS.

Can I run Celery or background workers?

Long-running workers are not suitable for shared hosting. Use cron jobs for scheduled tasks, or move to a VPS.

Where do I see print() output?

In the Passenger log file if you set one on the app page.

Need help? Open a support ticket or message us on WhatsApp +20 127 113 3655.

  • python, django, flask, setup python app, cpanel, passenger, wsgi, deploy

Was this answer helpful?

0 Users Found This Useful