H
10Corp Premium Hosting

Setting Up a Python Application in cPanel

Last Updated: 2025-01-01 3 min read

The Python Selector in cPanel allows you to deploy Python web applications on your hosting account without requiring root access. It manages Python versions, virtual environments, and application configuration through a user-friendly interface.

Prerequisites

  • A hosting plan that includes the Python Selector (check with your hosting provider).
  • Your Python application code ready to deploy.
  • A WSGI entry point file (commonly passenger_wsgi.py).

Creating a Python Application

  1. Log in to cPanel.
  2. Navigate to the Software section.
  3. Click on Setup Python App.
  4. Click Create Application.
  5. Configure the application:
SettingDescription
Python versionSelect the desired version (e.g., 3.9, 3.11)
Application rootDirectory containing your app (e.g., myflaskapp)
Application URLThe URL path for the app
Application startup fileWSGI entry point (e.g., passenger_wsgi.py)
Application Entry pointThe WSGI callable (e.g., application)
Passenger log fileOptional log file for debugging
  1. Click Create.

cPanel creates a virtual environment and sets up the application directory.

Setting Up the Virtual Environment

After creating the application, install your dependencies:

  1. Edit your application in the Python Selector.
  2. If you have a requirements.txt file, scroll to the Configuration files section and click Run Pip Install to install packages from requirements.txt.
  3. Alternatively, use the cPanel Terminal:
source /home/username/virtualenv/myflaskapp/3.11/bin/activate
cd ~/myflaskapp
pip install -r requirements.txt

Deploying a Flask Application

Example passenger_wsgi.py

import sys
import os

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

from app import app as application

Example Flask App (app.py)

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello from Flask!'

if __name__ == '__main__':
    app.run()

Directory Structure

myflaskapp/
├── passenger_wsgi.py     # WSGI entry point
├── app.py                # Flask application
├── requirements.txt      # Dependencies
├── templates/            # HTML templates
└── static/               # CSS, JS, images

Deploying a Django Application

Example passenger_wsgi.py

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

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

Make sure to update your Django settings.py:

  • Add your domain to ALLOWED_HOSTS
  • Configure STATIC_ROOT and run python manage.py collectstatic
  • Set DEBUG = False for production

Environment Variables

To set environment variables:

  1. Edit your application in the Python Selector.
  2. Scroll to the Environment variables section.
  3. Add key-value pairs (e.g., DJANGO_SETTINGS_MODULE=myproject.settings, SECRET_KEY=your-secret).
  4. Save and restart the application.

Managing Your Application

From the Python Selector dashboard:

  • Start/Stop/Restart the application.
  • Change Python versions (may require reinstalling dependencies).
  • Edit environment variables.
  • Add or remove packages via the web interface or terminal.

Troubleshooting

  • Application Error / 500 Error: Check the Passenger log file for detailed error messages. Common causes include missing modules, syntax errors, or incorrect WSGI configuration.
  • Module not found: Ensure you installed dependencies in the correct virtual environment. Activate the environment before running pip.
  • Static files not loading (Django): Run collectstatic and ensure your web server is configured to serve static files from STATIC_ROOT.
  • Changes not reflected: Restart the application from the Python Selector after deploying new code.
  • Permission errors: Ensure your application files have correct permissions (typically 644 for files, 755 for directories).

Important Notes

  • Python applications run through Phusion Passenger on cPanel hosting.
  • Shared hosting resource limits may affect application performance under heavy load.
  • For database-driven applications, create your MySQL or PostgreSQL database through cPanel and configure the connection in your application settings.
  • Consider a VPS or dedicated server for production Django/Flask applications with significant traffic.
Tags: cpanel hosting python flask django application

Still need help?

Our support team is available 24/7 to assist you.