Published on Mar 23 2020 in Java Python3

Django 3, SQLite 3.8.5 and Pyton 3.7 in cPanel

As Cloudlinux in cPanel evolves constantly here is the current way of setting up Django 3. This is an update to the older article from here.

Ensure the lines exist above JVMcP section in ~/.bashrc. These will provide sqlite3 3.8.3+ for Django >=2.2:

export LD_LIBRARY_PATH='/opt/atomicorp/atomic/root/usr/lib64/'
export PATH=/opt/atomicorp/atomic/root/bin:$PATH

Add new Python app in cPanel - do not fill in Application startup file and Application Entry Point - it will be done automatically.

django_approot is a directory under your home directory where your app will reside (APPROOT)

Choose a domain to host the app at (you may also append a path to the domain) and Save. Review the settings:

passenger_wsgi.py is what will be called by Passenger module relative to the above directory (Application startup file)

application is app name defined in passenger_wsgi.py (Application Entry Point)

Python app setup

Visit http://yourdomain.com/ to get:

It works!
Python 3.7.3

Enter shell (cPanel’s Teminal or SSH client) and enter your virtualenv as advised in cPanel then continue with setting up Django:

source /home/username/virtualenv/django_approot/3.7/bin/activate && cd /home/username/django_approot

pip install --upgrade pip
pip install Django # you will need SQLite 3.8.3+ installed from source or system-wide for Django >=2.2, see at the top of the guide
#pip install Django==2.1.*  # for older sqlite
pip install Paste

django-admin.py startproject app_one .

Edit app_one/settings.py:

ALLOWED_HOSTS = ['yourdomain.com']

At the bottom add:

import os
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

In app root run:

mkdir -p {media,static}

./manage.py collectstatic
./manage.py migrate
./manage.py createsuperuser

Update passenger_wsgi.py with:

import os
import sys

#########Accessing the App's Default WSGI##############
import app_one.wsgi

application = app_one.wsgi.application

######## PASSENGER PATH INFO-FIX INITIALISATIONS#######
cwd = os.getcwd()
sys.path.append(cwd)
#sys.path.append(os.getcwd())
sys.path.append(cwd + '/app_one')  #You must add your project here
# Set this to your root
SCRIPT_NAME = os.getcwd()

########  MIDDLEWARE CLASS TO FIX PASSENGER'S URI ISSUES #############
class PassengerPathInfoFix(object):
    """
    Sets PATH_INFO from REQUEST_URI since Passenger doesn't provide it.
    """
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        from urllib.parse import unquote
        environ['SCRIPT_NAME'] = SCRIPT_NAME

        request_uri = unquote(environ['REQUEST_URI'])
        script_name = unquote(environ.get('SCRIPT_NAME', ''))
        offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
        environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
        return self.app(environ, start_response)

###########the redirecting Middleware
application = PassengerPathInfoFix(application)

Restart app with touch tmp/restart.txt and verify http://yourdomain.com/# now opens Django 3 website.