Running Python Applications on Shared Hosting
Python is a versatile programming language widely used for web applications, APIs, data processing, and automation. All Domain India shared hosting plans include Python support through CloudLinux, enabling you to run Python web applications (Django, Flask, and more) without a VPS. This guide covers setting up, deploying, and managing Python applications on your shared hosting account.
Python Support on Shared Hosting
Our shared hosting includes Python capabilities through CloudLinux's Python Selector:
- Multiple Python versions — choose from Python 3.x versions
- Virtual environments — isolated environments for each application
- pip support — install Python packages from PyPI
- WSGI support — serve Python web applications via Passenger
- No VPS required — full Python support on shared hosting plans
Tip: You do NOT need a VPS to run Python applications. Our shared hosting includes full Python support through CloudLinux.
Setting Up a Python Application in cPanel
Step 1: Access the Python Selector
- Log in to cPanel
- Navigate to the Software section
- Click Setup Python App
Step 2: Create a New Application
- Click Create Application
- Configure your application:
- Python version: Select your desired version (e.g., 3.9, 3.10, 3.11)
- Application root: Directory containing your application (e.g., myapp)
- Application URL: The URL path for your application
- Application startup file: Your WSGI entry point (e.g., passenger_wsgi.py)
- Application Entry point: The WSGI callable (e.g., application)
- Click Create
Step 3: Set Up the Virtual Environment
The Python Selector automatically creates a virtual environment. To activate it via SSH:
source /home/username/virtualenv/myapp/3.9/bin/activateThe exact path is shown on the application management page.
Step 4: Install Dependencies
- Upload your
requirements.txtfile to the application root - From the application management page, click Run Pip Install
- Or install via SSH:
source /home/username/virtualenv/myapp/3.9/bin/activate
pip install -r requirements.txtSetting Up Python in DirectAdmin
Using the Python Selector
- Log in to DirectAdmin
- Go to Extra Features > Python Selector (or Advanced Features)
- Click Create Application
- Configure:
- Python version: Choose from available versions
- Application root: Your application directory
- Application URL: Domain or subdomain path
- Startup file: WSGI entry point
- Click Create
- Install dependencies via pip install
Creating the WSGI Entry Point
Shared hosting uses Passenger to serve Python applications. You need a passenger_wsgi.py file:
For a Flask Application
import sys
import os
# Add your application directory to the path
sys.path.insert(0, os.path.dirname(__file__))
from app import app as applicationWhere app.py contains:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello from Python on shared hosting!'
if __name__ == '__main__':
app.run()For a Django Application
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()For a Generic WSGI Application
def application(environ, start_response):
status = '200 OK'
headers = [('Content-Type', 'text/html')]
start_response(status, headers)
return [b'Hello from Python WSGI!']Setting Up a Flask Application
Complete setup for a Flask application:
1. Create the Application Directory
mkdir ~/myflaskapp
cd ~/myflaskapp2. Create requirements.txt
Flask==3.0.0
gunicorn==21.2.0
python-dotenv==1.0.03. Create app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=False)4. Create passenger_wsgi.py
import sys, os
sys.path.insert(0, os.path.dirname(__file__))
from app import app as application5. Create Templates
Create a templates/ directory with your HTML files.
6. Install and Start
Use the Python Selector to install dependencies and start the application.
Setting Up a Django Application
Complete setup for Django:
1. Create requirements.txt
Django==5.0
mysqlclient==2.2.0
python-dotenv==1.0.02. Create the Django Project
Via SSH with the virtual environment activated:
django-admin startproject myproject .3. Configure settings.py
Update your Django settings for production:
DEBUG = False
ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_database_name',
'USER': 'your_database_user',
'PASSWORD': 'your_database_password',
'HOST': 'localhost',
}
}
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')4. Collect Static Files
python manage.py collectstatic5. Run Migrations
python manage.py migrateEnvironment Variables
Manage sensitive configuration:
Using the Control Panel
- Go to the Python application management page
- Add environment variables (e.g.,
SECRET_KEY,DATABASE_URL) - Restart the application
Using a .env File
Create a .env file (add to .gitignore):
SECRET_KEY=your-secret-key-here
DATABASE_URL=mysql://user:pass@localhost/dbname
DEBUG=FalseLoad in your application:
from dotenv import load_dotenv
load_dotenv()
import os
secret_key = os.getenv('SECRET_KEY')Database Connectivity
Python applications can connect to databases available on your hosting:
MySQL/MariaDB
import mysql.connector
conn = mysql.connector.connect(
host='localhost',
user='your_db_user',
password='your_db_password',
database='your_database'
)SQLite (For Small Applications)
SQLite works out of the box with no additional setup:
import sqlite3
conn = sqlite3.connect('database.db')Troubleshooting Python Applications
Application Shows 503 Error
- Check the application logs for errors
- Verify the virtual environment is set up correctly
- Ensure all dependencies are installed
- Restart the application from the control panel
Import Errors
- Verify the package is installed in your virtual environment (not system Python)
- Run
pip install -r requirements.txtfrom the correct virtual environment - Check that the Python version matches your code requirements
passenger_wsgi.py Errors
- Ensure the file is in the application root directory
- Verify the import path matches your application structure
- Check that the WSGI callable is named
application
Static Files Not Loading
- Configure static file serving in your framework settings
- For Django, run
collectstaticand configure the static URL - For Flask, ensure the
static/directory is in the correct location
Memory Errors
- Shared hosting has memory limits per account
- Optimize your application to use less memory
- Avoid loading large datasets into memory
- Consider a VPS if your application consistently requires more resources
Best Practices
- Use virtual environments: Always use the provided virtual environment
- Keep requirements.txt updated: Document all dependencies
- Set DEBUG to False: Never run in debug mode on production
- Protect sensitive data: Use environment variables for secrets
- Use production-grade settings: Configure ALLOWED_HOSTS, secure cookies, CSRF protection
- Monitor resource usage: Stay within your plan's memory and CPU limits
- Test locally first: Develop and test on your local machine before deploying
- Use logging: Implement proper logging for troubleshooting
Related Articles
- Running Node.js Applications on Shared Hosting
- How to Access Your Account via SSH
- Managing MySQL Databases
Need help with your Python application? Contact our support team at [email protected] or visit https://domainindia.com/support.