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.txtfile 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
- Log in to cPanel.
- In the Software section, click Setup Python App, then Create Application.
- Python version: choose the version your project was developed with.
- Application root: a folder in your home directory, for example
myapp. Keep it outsidepublic_html. - Application URL: the domain or subdomain where the app should answer.
- Application startup file: leave
passenger_wsgi.py. - Application Entry point: leave
application. - 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
- On the app page, copy the command at the top that enters the virtual environment.
- 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
- Set
DEBUG = Falseand add your domain toALLOWED_HOSTS. - Keep
SECRET_KEYand the database password in environment variables (add them on the app page). - For MySQL, create the database in cPanel (see How to create a MySQL database and user) and use
localhostas the host. - Static files: set
STATIC_ROOTto 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. - 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.
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.pyor a missing package. Check the log (fill the Passenger log file field on the app page) and read the error logs. mysqlclientfails to install: it needs to compile. UsePyMySQLinstead and addimport pymysql; pymysql.install_as_MySQLdb()at the top of your settings.- CSS and images missing in Django:
collectstaticwas not run orSTATIC_ROOTis 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.