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
- Log in to cPanel.
- Navigate to the Software section.
- Click on Setup Python App.
- Click Create Application.
- Configure the application:
| Setting | Description |
|---|---|
| Python version | Select the desired version (e.g., 3.9, 3.11) |
| Application root | Directory containing your app (e.g., myflaskapp) |
| Application URL | The URL path for the app |
| Application startup file | WSGI entry point (e.g., passenger_wsgi.py) |
| Application Entry point | The WSGI callable (e.g., application) |
| Passenger log file | Optional log file for debugging |
- 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:
- Edit your application in the Python Selector.
- If you have a
requirements.txtfile, scroll to the Configuration files section and click Run Pip Install to install packages fromrequirements.txt. - 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_ROOTand runpython manage.py collectstatic - Set
DEBUG = Falsefor production
Environment Variables
To set environment variables:
- Edit your application in the Python Selector.
- Scroll to the Environment variables section.
- Add key-value pairs (e.g.,
DJANGO_SETTINGS_MODULE=myproject.settings,SECRET_KEY=your-secret). - 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
collectstaticand ensure your web server is configured to serve static files fromSTATIC_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