StarDomain

Running Python Applications on Shared Hosting

Running Python Applications on Shared Hosting

Python is a versatile programming language widely used for web applications, APIs, data processing, and automation. All {{COMPANY_NAME}} 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:

  1. Multiple Python versions — choose from Python 3.x versions
  2. Virtual environments — isolated environments for each application
  3. pip support — install Python packages from PyPI
  4. WSGI support — serve Python web applications via Passenger
  5. 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

  1. Log in to cPanel
  2. Navigate to the Software section
  3. Click Setup Python App

Step 2: Create a New Application

  1. Click Create Application
  2. 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)

  1. Click Create

Step 3: Set Up the Virtual Environment

The Python Selector automatically creates a virtual environment. To activate it via SSH:

bash
source /home/username/virtualenv/myapp/3.9/bin/activate

The exact path is shown on the application management page.

Step 4: Install Dependencies

  1. Upload your requirements.txt file to the application root
  2. From the application management page, click Run Pip Install
  3. Or install via SSH:
bash
source /home/username/virtualenv/myapp/3.9/bin/activate
pip install -r requirements.txt

Setting Up Python in DirectAdmin

Using the Python Selector

  1. Log in to DirectAdmin
  2. Go to Extra Features > Python Selector (or Advanced Features)
  3. Click Create Application
  4. Configure:

- Python version: Choose from available versions

- Application root: Your application directory

- Application URL: Domain or subdomain path

- Startup file: WSGI entry point

  1. Click Create
  2. 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

python
import sys
import os

# Add your application directory to the path
sys.path.insert(0, os.path.dirname(__file__))

from app import app as application

Where app.py contains:

python
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

python
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

python
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

bash
mkdir ~/myflaskapp
cd ~/myflaskapp

2. Create requirements.txt

Flask==3.0.0
gunicorn==21.2.0
python-dotenv==1.0.0

3. Create app.py

python
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

python
import sys, os
sys.path.insert(0, os.path.dirname(__file__))
from app import app as application

5. 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.0

2. Create the Django Project

Via SSH with the virtual environment activated:

bash
django-admin startproject myproject .

3. Configure settings.py

Update your Django settings for production:

python
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

bash
python manage.py collectstatic

5. Run Migrations

bash
python manage.py migrate

Environment Variables

Manage sensitive configuration:

Using the Control Panel

  1. Go to the Python application management page
  2. Add environment variables (e.g., SECRET_KEY, DATABASE_URL)
  3. 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=False

Load in your application:

python
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

python
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:

python
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.txt from 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 collectstatic and 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

  1. Use virtual environments: Always use the provided virtual environment
  2. Keep requirements.txt updated: Document all dependencies
  3. Set DEBUG to False: Never run in debug mode on production
  4. Protect sensitive data: Use environment variables for secrets
  5. Use production-grade settings: Configure ALLOWED_HOSTS, secure cookies, CSRF protection
  6. Monitor resource usage: Stay within your plan's memory and CPU limits
  7. Test locally first: Develop and test on your local machine before deploying
  8. Use logging: Implement proper logging for troubleshooting
  • 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 {{SUPPORT_EMAIL}} or visit {{SUPPORT_URL}}.