Select Page
Welcome to our Support Center
< All Topics

How to Deploy Django Web Application on Shared Hosting – cPanel

How to Deploy Django Web Application on Shared Hosting – cPanel

 

Introduction

Shared hosting at HostingHome is an affordable option for hosting your website. Django, a Python-based framework, allows you to develop robust websites with ease. This guide will walk you through the steps to install and configure Django on a Linux shared hosting account with cPanel.

Step 1 — Set Up a Python Application in cPanel

Begin by setting up a Python application within cPanel, which will serve as the environment for your Django project.

Follow these steps to get started:

Log in to cPanel and open Setup Python App from the Software Section.

Next, click CREATE APPLICATION

The application form appears:

In the Python version dropdown choose Python 3.10.9 the latest version available.Enter djangoapp in the Application Root field.From the Application URL dropdown, select the domain or subdomain where your application will be hosted.In the Application Startup File field, input passenger_wsgi.py.Finally, set the Application Entry Point to application.Optionally, you can specify a log file for your application in the Passenger Log File field.Once you’ve configured everything, click CREATE in the top right corner of the page.
cPanel will now create the application and set up the Python Environment for you.

Step 2 — Configure the Django project

Once you’ve created the Python application in cPanel, you can proceed with the following tasks via the command line:

  • Install Django.
  • Set up a new Django project.
  • Configure Passenger to integrate with the Django project.

Copy the command to enter the virtual environment from your Python app

Open the Terminal in your cPanel.

Execute the copied command in the terminal and press Enter to activate the virtual environment.

To install Django, enter the following command:

bash
$ pip install django==5.1

To check the installed version of Django, use this command:

bash

$ django-admin –version

Step 3 — Set up a new Django project

To start a new Django project, enter the following command:

bash
$ django-admin startproject demo

To set up directories for the project’s static files, run the following commands:

bash
$ mkdir -p demo/templates/static_pages
$ mkdir demo/static_files
$ mkdir demo/static_media

Find the ALLOWED_HOSTS line and update it by replacing example.com with your domain name:

 ALLOWED_HOSTS = [‘example.com’] 

Locate the TEMPLATES block and update it as follows:

python
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',
        ],
    },
},
]
  

Find the STATIC_URL line, then add these lines directly below it:

python
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static_media')
  
Use the file manager to open the `demo/urls.py` file, remove all existing content, and then paste the following text into the file:
python
from django.contrib import admin
from django.urls import path, include
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)
  
Use the file manager to open the `demo/passenger_wsgi.py` file, remove all existing content, and then paste the following text into the file:
python
import os
import sys

import django.core.handlers.wsgi
from django.core.wsgi import get_wsgi_application

# Set up paths and environment variables
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = 'demo.settings'

# Set script name for the PATH_INFO fix below
SCRIPT_NAME = os.getcwd()

class PassengerPathInfoFix(object):
    """
        Sets PATH_INFO from REQUEST_URI because 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)

# Set the application
application = get_wsgi_application()
application = PassengerPathInfoFix(application)
  

Use a file manager text editor to create a basic index.html file in the demo/templates/static_pages directory. The file can be a simple text file with just Hello world in it.

Execute the migrate command to set up the database tables.

bash
$ python manage.py migrate

Use the collectstatic command to transfer static files to the directory specified in your settings.

bash
$ python manage.py collectstatic
Create a superuser account for the Django admin interface by running the createsuperuser command.At the Username prompt, enter the administrator username and press Enter.At the Email address prompt, enter the administrator’s email address and press Enter.At the Password prompt, enter the administrator’s password and press Enter.
bash
$ python manage.py createsuperuser

Finally, restart your Python application to apply all the changes.

Your Django app has been successfully deployed. Open the app’s URL to verify that it’s working correctly.