Python is a versatile, high-level programming language renowned for its readability, vast library ecosystem, and powerful web development capabilities. Django is the premier Python web framework, designed to help developers build secure, scalable, and maintainable web applications rapidly with built-in features such as the Django ORM, an automatic Admin Interface, robust authentication, and CSRF protection.

DirectAdmin hosting provides native support for Python and Django applications powered by the CloudLinux Setup Python App (Passenger / WSGI) manager. This allows you to create isolated Python virtual environments, select your desired Python version (Python 3.9, 3.10, 3.11, or 3.12), install dependencies with Pip, and run production-grade WSGI web applications effortlessly.

In this comprehensive guide, we will walk you step-by-step through configuring, deploying, and managing a Python / Django web application in DirectAdmin.


Prerequisites


Step-by-Step Guide: How to Deploy Python and Django in DirectAdmin

Step 1: Prepare Your Django Project Locally

Before uploading your Django project, ensure production settings and dependencies are properly configured:

  1. Generate requirements.txt: Open your terminal in your local project directory and export your packages:
    pip freeze > requirements.txt
  2. Configure settings.py: Open your project's settings.py and configure the following:
    • ALLOWED_HOSTS: Add your domain name:
      ALLOWED_HOSTS = ['domain.com', 'www.domain.com', '127.0.0.1']
    • DEBUG: Set to False for production:
      DEBUG = False
    • STATIC & MEDIA Settings: Configure static and media asset paths so that the web server can serve them directly:
      STATIC_URL = '/static/'
      STATIC_ROOT = os.path.join(BASE_DIR, 'public_html/static')
      
      MEDIA_URL = '/media/'
      MEDIA_ROOT = os.path.join(BASE_DIR, 'public_html/media')
  3. Compress project files: Compress your Django project files (including manage.py, requirements.txt, and your application modules) into a .zip archive (e.g. django-app.zip).
    Note: Exclude local virtual environments (venv/), .git/, and __pycache__/ folders.

Step 2: Access "Setup Python App" in DirectAdmin

  1. Log in to your DirectAdmin control panel.
  2. In the top search bar, search for Setup Python App (or navigate to Extra Features > Setup Python App in the left sidebar).

DirectAdmin Dashboard - Setup Python App and Tools

  1. Click the + CREATE APPLICATION button in the top right corner.

DirectAdmin Setup Python App - Web Applications List

Step 3: Configure Application Settings

Fill out the application creation form with the following settings:

Setting Recommended Value Description
Python version 3.10, 3.11, or 3.12 Select the Python version matching your project requirements.
Application root django-app The folder name where your Python project files will reside.
Application URL domain.com Select your target domain name from the dropdown.
Application startup file passenger_wsgi.py The WSGI entry script used by the web server.
Application Entry point application The callable WSGI object name.
  1. Click CREATE. DirectAdmin will create an isolated virtual environment and a default passenger_wsgi.py file.

DirectAdmin Setup Python App - Create Application Form

Step 4: Upload and Extract Your Project Files

  1. In DirectAdmin, navigate to System Info & Files > File Manager.
  2. Navigate into your application root folder (e.g. django-app or domains > domain.com > django-app).
  3. Click Upload in the top navigation bar and upload your django-app.zip file.
  4. Right-click the uploaded ZIP file and click Extract.

DirectAdmin File Manager - Upload and Extract Python Project

Step 5: Configure passenger_wsgi.py

DirectAdmin creates a starter passenger_wsgi.py. We must update it to point to our Django project's WSGI application:

  1. In File Manager inside your Application Root directory (django-app), select passenger_wsgi.py and click Edit.
  2. Replace its contents with the following WSGI configuration:
    import os
    import sys
    
    # Add project root directory to Python path
    project_dir = os.path.dirname(os.path.abspath(__file__))
    sys.path.insert(0, project_dir)
    
    # Set Django settings module (replace 'myproject' with your Django project name)
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
    
    # Import Django WSGI Handler
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
  3. Click Save.

Step 6: Install Python Dependencies via Pip

  1. Return to Setup Python App in DirectAdmin and click the Edit icon on your application.
  2. Scroll down to the Configuration files section.
  3. In the text box, enter requirements.txt and click Add.
  4. Click Run Pip Install. DirectAdmin will automatically install all specified Python libraries (including Django) into your application’s isolated virtual environment!

Step 7: Run Migrations & Collect Static Files

To run management commands like database migrations and static collection, open the DirectAdmin Terminal (or connect via SSH):

  1. Copy the virtual environment activation command shown at the top of your Setup Python App page:
    source /home/username/virtualenv/django-app/3.11/bin/activate && cd /home/username/django-app
  2. Run your database migrations:
    python manage.py migrate
  3. Collect static files for the Admin dashboard:
    python manage.py collectstatic --noinput
  4. Create your superuser administrator account:
    python manage.py createsuperuser

Step 8: Restart and Verify Your Application

  1. In Setup Python App, click the Restart button at the top.
  2. Open your web browser and visit https://domain.com and https://domain.com/admin.
  3. Your Python / Django web application is now live and fully operational!

Deploying Plain Python / Flask Web Applications

If you are deploying a lightweight Flask application or simple Python script instead of Django, your passenger_wsgi.py file should look like this:

import sys
import os

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

# Import Flask app instance as 'application'
from app import app as application

Frequently Asked Questions & Troubleshooting

Q: Why does my site show "Passenger Error" or a 500 error?
A: This error typically occurs due to an unhandled Python exception, syntax error, or missing dependency. To diagnose:

  1. Check your Passenger error log configured in Step 3 (e.g., /home/username/logs/passenger.log).
  2. Ensure all required libraries are installed via requirements.txt.
  3. Verify that the application callable in passenger_wsgi.py matches your WSGI object name.

Q: How do I fix the "DisallowedHost at /" error in Django?
A: Open your settings.py file and add your exact domain and subdomains to ALLOWED_HOSTS = ['domain.com', 'www.domain.com'], then restart the application.

Q: Why are static files (CSS/JS) not loading in Django Admin?
A: Ensure you configured STATIC_ROOT = os.path.join(BASE_DIR, 'public_html/static') and executed python manage.py collectstatic in the terminal so that static assets are placed in the public web root.

Q: How do I restart the Python application after making code changes?
A: In DirectAdmin, go to Setup Python App and click Restart. Alternatively, you can create or touch the tmp/restart.txt file in your application root folder.


Need Further Assistance?

If you need any help configuring your Python environment or deploying your Django application in DirectAdmin, our technical support team is available 24/7. Feel free to submit a support ticket through your client area for expert assistance.

War diese Antwort hilfreich? 0 Benutzer fanden dies hilfreich (0 Stimmen)