How to Install/Run a Python app via cPanel - Flask & Django

⚙️ GENERAL STEPS (Applicable to Both Flask and Django)

Here’s the complete guide for installing and running a Python app via cPanel, including Flask and Django versions.

1. Log in to cPanel

This simple guide will teach you on how to Login to your cPanel – Click Here to Learn How.

2. Open “Setup Python App”

  • In cPanel, search for Setup Python App
  • Click it to open the Python Selector (mod_passenger)

3. Create a New Python Application

Click Create Application, then fill in:

Field Description
Python Version Choose the latest version (e.g., 3.9, 3.10, or 3.11)
Application Root Folder name, e.g. myflaskapp or mydjangoapp
Application URL The domain or subdomain to run the app (e.g. app.yourdomain.com)
Application Startup File app.py (for Flask) or passenger_wsgi.py (for Django)
Application Entry Point application

Click Create when done.

4. Upload Your Application Files

Use File Manager or FTP to upload your app into the directory set as your Application Root.

5. Install Dependencies

  1. In Setup Python App, note the virtual environment path shown.
  2. Open Terminal in cPanel or use SSH (if available):
    source /home/username/virtualenv/myapp/3.10/bin/activate
    pip install -r /home/username/myapp/requirements.txt
    
  3. If you don’t have a requirements.txt, you can install manually:
    pip install flask
    

    or

    pip install django
    

🧩 FLASK APPLICATION SETUP

1. Create the app.py File

Inside your app folder (e.g. /home/username/myflaskapp/), create app.py:

from flask import Flask

application = Flask(__name__)

@application.route('/')
def index():
    return "Hello from Flask app hosted on cPanel!"

if __name__ == "__main__":
    application.run()

2. Set App Settings in cPanel

In Setup Python App:

  • Startup File: app.py
  • Application Entry Point: application

Click Save and then Restart Application.

Now visit your domain/subdomain — your Flask app should load!

🌍 DJANGO APPLICATION SETUP

1. Create a Django Project (if not yet done)

In cPanel’s Terminal, run:

source /home/username/virtualenv/mydjangoapp/3.10/bin/activate
cd /home/username/mydjangoapp
django-admin startproject myproject .

This creates:

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py

2. Update settings.py

Open /home/username/mydjangoapp/myproject/settings.py and edit:

ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com']
DEBUG = False

3. Configure passenger_wsgi.py

In your app root, create or edit a file called passenger_wsgi.py:

import sys, os

# Add project directory to system path
sys.path.insert(0, os.path.dirname(__file__))

# Set environment variable for Django
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

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

4. Apply Migrations

In cPanel Terminal, activate your environment and run:

cd /home/username/mydjangoapp
python manage.py migrate
python manage.py collectstatic --noinput

5. Set Up Static Files (Optional but Recommended)

In settings.py, add:

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Then run:

python manage.py collectstatic

6. Configure App in cPanel

In Setup Python App:

  • Startup File: passenger_wsgi.py
  • Application Entry Point: application

Click Save, then Restart Application.

Now your Django app is live!
Visit your configured domain/subdomain to see it running.

In Summary

  1. Log in to cPanel.
  2. Open “Setup Python App.”
  3. Click “Create Application.”
  4. Select Python version, application root folder, domain/subdomain, startup file, and entry point.
  5. Click “Create.”
  6. Upload your project files into the application root folder.
  7. Open Terminal in cPanel.
  8. Activate the virtual environment:
    source /home/username/virtualenv/myapp/3.10/bin/activate
    
  9. Install dependencies:
    pip install -r /home/username/myapp/requirements.txt
    
  10. Go back to “Setup Python App.”
  11. Click “Restart Application.”
  12. Visit your domain to confirm it loads.

🧠 Tips

  • Error Logs: Check /home/username/myapp/passenger.log if something fails.
  • Database Setup: Use MySQL Databases in cPanel, then configure credentials in settings.py (Django) or your Flask config.
  • Virtual Environment Management: Always activate it before installing packages.

Need help? Our friendly support team is always here for you! Reach out below.

Happy hosting! 🌟

Leave a Reply

Your email address will not be published. Required fields are marked *