Skip to content

Installing and configuring Flask on a Linux shared hosting account

Installing and configuring Django on Linux shared hosting

Section titled “Installing and configuring Django on Linux shared hosting”

This guide explains how to install and configure Django on a Linux shared hosting account using cPanel. After following these steps, you will have a Django site that:

  • Loads a static homepage.
  • Loads the Django administration interface.
  • Uses a SQLite database.

Step 1: Create a Python application in cPanel

Section titled “Step 1: Create a Python application in cPanel”
  1. Log in to cPanel.

  2. In the Software section, click Setup Python App.

  3. Click CREATE APPLICATION.

  4. In the application form:

    • Select Python 3.8.1 in the Python version list.
    • Set Application root to myapp.
    • Select your domain in Application URL. Leave the rest blank.
    • Leave Application startup file and Application Entry point blank. cPanel will automatically create passenger_wsgi.py.
    • Optionally specify a Passenger log file.
  5. Click CREATE. cPanel will create the application and set up the Python environment.

  6. Note the command next to Enter to the virtual environment. You will need it in the next step.

  1. Log in via SSH.
  2. Activate the virtual environment (replace with your environment path):
source /home/username/virtualenv/myapp/3.8/bin/activate && cd /home/username/myapp

Important: Your prompt should show (myapp:3.8) indicating the active virtual environment. Reactivate the environment if you log out or deactivate it.

  1. Install Django:
cd ~
pip install django==2.1.8
  1. Verify Django installation:
django-admin --version
  1. Create the Django project:
django-admin startproject myapp ~/myapp
  1. Create directories for static files:
mkdir -p ~/myapp/templates/static_pages
mkdir ~/myapp/static_files
mkdir ~/myapp/static_media
  1. Edit ~/myapp/myapp/settings.py:
  • Set allowed hosts (replace with your domain):
ALLOWED_HOSTS = ['example.com']
  • Update the TEMPLATES block:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
  • Add static and media settings:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "static_media")
  1. Edit ~/myapp/myapp/urls.py to include:
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'), name='home'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  1. Edit ~/myapp/passenger_wsgi.py:
import os
import sys
import django.core.handlers.wsgi
from django.core.wsgi import get_wsgi_application
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
SCRIPT_NAME = os.getcwd()
class PassengerPathInfoFix(object):
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)
application = get_wsgi_application()
application = PassengerPathInfoFix(application)
  1. Create a basic index.html in ~/myapp/templates/static_pages containing simple text, e.g., “Hello world”.
  2. Run migrations and create a superuser:
python ~/myapp/manage.py migrate
python ~/myapp/manage.py createsuperuser

Follow prompts to set admin username, email, and password.

  1. Collect static files:
python ~/myapp/manage.py collectstatic

Type yes if prompted to overwrite files.

  1. Log in to cPanel.
  2. In Software → Setup Python App, locate your myapp application and click Restart.
  • Visit http://www.example.com to see your index.html page.
  • Visit http://www.example.com/admin to access the Django admin interface using your superuser credentials.

Tip: If the site does not appear, run:

python ~/myapp/passenger_wsgi.py

There should be no output. Check for syntax errors if there are any.