Deploying Django Application on cPanel: A Comprehensive Tutorial

  1. Introduction
  2. Features
  3. General procedure for deploying a Django application
  4. Deployment Methods
  5. Supported Platforms
  6. WSGI and ASGI Interfaces
  7. Conclusion
  8. How to Deploy a Django Application on cPanel: Step-by-Step Tutorial
  9. Concepts
    1. Django Deployment with WSGI
    2. WSGI vs ASGI
    3. WSGI Configuration
    4. The Application Object
    5. Configuring the Settings Module
    6. Django Environment
  10. References

Introduction

Django is a web framework that simplifies the development of web applications. It is a highly extensible framework that is ideal for building complex web applications. Django has a built-in web server, supports multiple databases, and provides a modular architecture.

Features

It also provides a number of features, including:

  1. A robust security model
  2. A flexible and extensible architecture
  3. A large and active community

General procedure for deploying a Django application

Deploying a Django application involves several key steps, from preparation to going live. This general procedure is designed to cover most deployment scenarios:

1. Preparation

  • Requirements File: Ensure you have a requirements.txt file listing all your project dependencies. Generate it using pip freeze > requirements.txt if necessary.
  • Settings Configuration: Split your settings into base (common settings), development (for local development), and production (for deployment) settings. Ensure sensitive information is not hard-coded but instead is pulled from environment variables or a secure source.
  • Static and Media Files: Configure your static files (CSS, JavaScript) and media files (uploads) settings properly for production.

2. Choose a Hosting Service

  • Decide on a hosting service that supports Python and Django. Popular options include Heroku, AWS (Elastic Beanstalk or EC2), DigitalOcean, and Google Cloud Platform.

3. Set Up Your Server

  • If not using a PaaS provider like Heroku, set up your server environment. This may involve installing Nginx or Apache for serving static files, and Gunicorn or uWSGI as the WSGI HTTP server to serve your Django app.

4. Database Configuration

  • Configure your production database. Django supports several databases out of the box, including PostgreSQL, MySQL, and SQLite. For production, PostgreSQL or MySQL is recommended.
  • Run python manage.py migrate to apply migrations to your production database.

5. Static and Media Files

  • Collect static files using python manage.py collectstatic. This gathers all your static files in the directory specified by STATIC_ROOT.
  • Ensure your production environment has a place to store and serve media files uploaded by users.

6. Security Settings

  • Enable DEBUG = False in your production settings.
  • Use Django’s SECURE_SSL_REDIRECT and CSRF_COOKIE_SECURE settings to enforce HTTPS and secure cookies.
  • Update your ALLOWED_HOSTS with the domain names your app will be served from.

7. Deploy Your Code

  • For PaaS (e.g., Heroku), follow the platform’s specific deployment process, often involving Git for pushing your code.
  • For IaaS (e.g., AWS EC2), you might use SSH to upload your code and dependencies, then start your application server (e.g., Gunicorn) and configure your web server (e.g., Nginx) to proxy to your app.

8. Configure DNS

  • Update your DNS settings to point your domain to your hosted application.

9. Monitor and Scale

  • Monitor your application’s performance and error logs.
  • Adjust resources and scale your app as needed based on traffic and usage patterns.

10. Continuous Integration/Continuous Deployment (CI/CD)

  • Optionally, set up CI/CD pipelines for automated testing and deployment using tools like GitHub Actions, GitLab CI/CD, or Jenkins.

This procedure outlines a broad overview; specific steps may vary based on your hosting environment and other technical choices. Always refer to the official Django documentation and your hosting provider’s guidelines for the most accurate and secure deployment practices.

Deployment Methods

There are several ways to deploy a Django application:

  1. Using a web hosting service: This is the simplest option, where the hosting provider takes care of all the details of deploying and maintaining your application.
  2. Using a PaaS (Platform as a Service): A PaaS provides a more managed environment for deploying your application, including features such as automatic scaling and load balancing.
  3. Deploying on your own servers: This gives you the most control over your application’s environment but requires the most maintenance and expertise.

Supported Platforms

Django can be deployed on several platforms, including:

  • Linux
  • macOS
  • Windows
  • AWS
  • Azure
  • Google Cloud Platform.

WSGI and ASGI Interfaces

Django supports two interfaces, WSGI and ASGI, for communicating with web servers:

  1. WSGI (Web Server Gateway Interface): It is a standard interface that allows web servers to communicate with Python applications. Django supports the WSGI interface by providing a WSGI application object. Django uses WSGI to communicate with the Gunicorn web server, which is the default web server that is installed with Django. It is a synchronous interface, which means that the web server waits for the Python application to finish processing a request before sending the response.
  2. ASGI (Asynchronous Server Gateway Interface): It is a newer standard interface that is designed to be more efficient than WSGI. It is an asynchronous interface, which means that the web server can continue to process requests while the Python application is still processing a previous request. Django supports the ASGI interface by providing an ASGI application object. Asynchronous applications are able to handle multiple requests at the same time, which can improve the performance of your application. If you are using an asynchronous Python framework, such as Twisted or Tornado, you should use the ASGI interface to deploy your Django application. Django supports ASGI through the asgiref library.

Conclusion

Django is a powerful and flexible web framework that is ideal for building complex web applications. It provides several deployment options, including using a web hosting service, PaaS, or your own servers. You can choose the deployment method that best suits your team’s needs and requirements. Django supports two interfaces, WSGI and ASGI, for communicating with web servers. Redeploying a Django app on cPanel involves removing the existing app and creating a new app with the updated settings.

How to Deploy a Django Application on cPanel: Step-by-Step Tutorial

  1. Go to your Django project folder
Icon of a blue folder labeled 'django_test' on a dark background.
An icon of a blue folder labeled ‘django_test’ signifies a dedicated space for Django project files.
  1. Within the folder you should find two folders, one is your environment and the other is your main project folder
A screenshot showing two blue folders named 'myenv' and 'myproject' inside a directory titled 'django_test'.
Two neatly organized folders, ‘myenv’ and ‘myproject’, reside within the ‘django_test’ directory, ready for development.
  1. Zip your principal project folder i.e django_test
  1. Now login to your cPanel account, and navigate to the cPanel File Manager
A control panel interface with icons for 'File Manager', 'Images', 'Directory Privacy', 'Disk Usage', 'Web Disk', 'Backup', 'Git™ Version Control', 'Backup Wizard', and 'File and Directory Restoration'.
Streamlined file organization within ‘django_test’: the ‘myenv’ folder for environment configuration and the ‘myproject’ folder for project assets.
  1. You can upload the zipped folder here by clicking on the “Upload” button. As you can see django_test.zip has been uploaded.
Screenshot of the cPanel File Manager interface displaying a list of directories and files, including 'applog', 'django_test', and 'public_html', with various file sizes, types, and permissions. The 'Upload' button in the top menu is highlighted with a red rectangle.
A highlighted ‘Upload’ button in the cPanel File Manager, ready to receive new files for the ‘django_test’ project.
  1. Right-click on the zip file and extract it.
A contextual menu in a file management interface showing options for a selected file named 'django_test.zip'. Options include 'Download', 'View', 'Move', 'Copy', 'Rename', 'Change Permissions', 'Delete', with 'Extract' highlighted, and 'Compress'.
Context menu in File Manager offering various options for ‘django_test.zip’, with ‘Extract’ highlighted for unpacking the contents.
  1. Click on the project folder on the left-hand side panel and you will see the folder(myproject) within the main principal project folder(django_test). You can later delete the myenv folder after uploading as we will set up the environment for our project in cPanel itself.
The cPanel File Manager interface showing a directory structure with a folder named 'django_test' selected. Inside 'django_test', a subfolder named 'myproject' is listed, with details noting it was last modified on February 21, 2023, at 11:07 PM, is a Unix directory, and has 0755 permissions.
File Manager view focused on the ‘django_test’ directory, showcasing a subdirectory named ‘myproject’, ready for development and deployment.
  1. Check whether all the project directories have been uploaded properly.
The cPanel File Manager showing the contents of the 'django_test/myproject' directory with files and subdirectories such as 'pycache', 'myapp', 'db.sqlite3', 'manage.py', 'passenger_wsgi.py', and 'requirements.txt', along with their sizes, modification dates, types, and permissions.
File Manager showing the structure of a Django project ‘myproject’ with various Python files and directories such as ‘migrations’, ‘admin.py’, and ‘views.py’, indicating a well-organized Django application setup.
  1. Navigate to cPanel home page and scroll down to the “Software” section. Click on “Setup Python App
    • If the “Setup Python App” feature is not visible within your cPanel interface, I recommend reaching out to the IT department for assistance in configuring your cPanel to enable this feature.
Control panel interface titled 'Software', displaying options such as 'PHP PEAR Packages', 'Optimize Website', 'MultiPHP INI Editor', 'Setup Python App' highlighted, 'Perl Modules', 'Application Manager', 'Setup Node.js App', 'MultiPHP Manager', 'Select PHP Version', and 'Setup Ruby App' with respective icons.
The ‘Software’ section of a web hosting control panel interface, highlighting various tools available for website optimization and application setup, including ‘Setup Python App’ highlighted for setting up a Python application on the server.
  1. Click on “Create Application” button. As you can see I have already created an application.
cPanel interface for Python Web Applications showing one application listed. The 'App URI' is 'pastel-test.cs.mtu.edu/', the 'App Root Directory' is '/home/pasteltest/django_test/myproject', and the 'Status' shows a green dot indicating the application is 'started (v3.10.4)'. On the right, there is a blue 'CREATE APPLICATION' button.
Screenshot of the cPanel web hosting control panel, specifically focused on the Python section under the Software category. This section is used to set up and manage Python applications on the web hosting account, indicating that the hosting environment supports Python and likely offers various tools and utilities to facilitate the deployment and management of Python-based web applications
  1. This is what you’ll see after you click on the button. Start filling in the required fields.
cPanel Python application setup interface, displaying fields to configure a new web application. Fields include 'Python version' with 2.7.18 selected, 'Application root' with a mandatory field highlighted in red, 'Application URL' pre-filled with 'pastel-test.cs.mtu.edu', and empty fields for 'Application startup file', 'Application Entry point', and 'Passenger log file' with a suggested path provided. Above the form, there is a 'CREATE APPLICATION' button.
cPanel’s Python application setup interface showing fields for selecting the Python version, specifying the application root, and defining the application URL, startup file, entry point, and passenger log file, with the ‘Application root’ as a mandatory field highlighted in red to be filled out.
  1. Follow these steps:
    1. Select the appropriate Python version. 
    2. Application root is basically your root-folder/project-folder. In this case django_test/myproject
    3. You can have multiple domains. Select the one you require for Application URL.
    4. You can leave the Application startup file and Application Entry point blank. It will be automatically generated.
    5. If you experience any problems like say the app crashes or encounter bugs or issues in deployment, we can refer to the log file. 
cPanel interface for managing a Python web application titled 'PASTEL-TEST.CS.MTU.EDU/'. It includes options to 'Stop App' and 'Restart', and a 'Destroy' button in the top right corner. The configuration form lists 'Python version' as 3.10.4 (recommended), 'Application root' as 'django_test/myproject', 'Application URL' as 'pastel-test.cs.mtu.edu', 'Application startup file' as 'passenger_wsgi.py', 'Application Entry point' as 'application', and 'Passenger log file' path as '/home/pasteltest/applog/passenger.log'. A command to enter the virtual environment is also displayed above the form.
The cPanel interface for a Python web application, displaying the application’s URI, root directory, status showing ‘started’ with version 3.10.4, and action buttons. Instructions for entering the virtual environment via a command line are also provided.
  1. As you can see the virtual environment has been automatically generated for us.
Bar in cPanel prompting to enter the virtual environment with the command provided: 'source /home/pasteltest/virtualenv/django_test/myproject/3.10/bin/activate && cd /home/pasteltest/django_test/myproject'.
A banner within the cPanel interface showing a command line instruction to enter a virtual environment for a Django project. The command provided is to source the virtual environment activation script followed by navigating to the project directory.

Before proceeding, we need to understand some concepts:

Django Deployment with WSGI

When deploying a Django application using the WSGI standard, the application server interacts with the code using an application callable, which is typically called ‘application’ and is provided as an object in a Python module that can be accessed by the server. Django utilizes WSGI as its primary deployment platform. WSGI is a widely accepted Python standard for web servers and applications.

WSGI vs ASGI

At present, Django provides two options for communicating between web servers and applications, namely WSGI (Web Server Gateway Interface) and ASGI (Asynchronous Server Gateway Interface). WSGI is a widely adopted Python standard for this purpose, but it only works with synchronous code. In contrast, ASGI is a new standard that enables the use of asynchronous Python features and will support the development of asynchronous Django features.

WSGI Configuration

When you use Django’s startproject management command, it creates a basic WSGI configuration for your project. You can then make any necessary adjustments to suit your project’s specific requirements. This configuration can be used by any WSGI-compliant application server to run your Django application. By default, Django sets up a minimal WSGI configuration that can be further customized by the user. The WSGI interface allows Django to seamlessly communicate with web servers, making it a preferred option for deployment.

The Application Object

The application object is the entry point for a Django application running under WSGI. When a Django project is created using the startproject command, it generates a file called ‘wsgi.py‘, which contains the application callable that can be used in both development and production WSGI deployments. The path to the application callable is obtained by WSGI servers from their configuration, and Django’s built-in server reads it from the WSGI_APPLICATION setting. By default, this setting is ‘<project_name>.wsgi.application’, which points to the application callable in the ‘wsgi.py‘ file.

For example, suppose we have a Django project named ‘myproject‘ with an app named ‘myapp‘. We can use the ‘wsgi.py‘ file generated by the startproject command to specify the application callable. In this file, we can define our application object like this:

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
application = get_wsgi_application()

Here, we import the ‘get_wsgi_application’ function from Django’s ‘core.wsgi’ module, and set the ‘DJANGO_SETTINGS_MODULE’ environment variable to point to our project’s settings module. We then define our application object as the result of calling ‘get_wsgi_application()’. The WSGI server can then use this ‘application’ object to interact with our Django application.

Configuring the Settings Module

When your application is loaded by the WSGI server, Django imports the settings module which defines your entire application. The ‘DJANGO_SETTINGS_MODULE’ environment variable is used by Django to locate the appropriate settings module. This variable must contain the dotted path to the settings module. You can use different values for development and production depending on how you organize your settings. If this variable is not set, the default wsgi.py sets it to ‘mysite.settings’, where ‘mysite’ is the name of your project. This is how ‘runserver’ discovers the default settings file by default.
For example, if you have a Django project named “myproject” with the settings file in a subdirectory called “config”, the ‘DJANGO_SETTINGS_MODULE’ environment variable can be set to ‘”myproject.config.settings“’ to load the correct settings file. Similarly, if you have separate settings for development and production, you can set the environment variable to “myproject.config.settings.development” or “myproject.config.settings.production” depending on the environment. This allows you to keep your settings organized and separate for different environments.

Django Environment Detection

This Python snippet below detects whether the Django web application is running in a production or development environment. It relies on the Django settings module to determine the environment based on the value of the DEBUG setting.

from django.conf import settings

def is_production():
    """
    Checks if the environment is production.
    Returns:
        True if the environment is production, False otherwise.
    """
    return settings.DEBUG is False

def is_development():
    """
    Checks if the environment is development.
    Returns:
        True if the environment is development, False otherwise.
    """
    return settings.DEBUG is True

if is_production():
    what_is_the_env = 'DEV'
    print(what_is_the_env)
else:
    what_is_the_env = 'PROD'
    print(what_is_the_env)

Functions

is_production(): This function checks if the environment is set to production.
Returns: True if the environment is production. False otherwise.

is_development(): This function checks if the environment is set to development.
Returns: True if the environment is development. False otherwise.

Example Usage
You can use these functions to conditionally execute code depending on the detected environment.

if is_production():
    what_is_the_env = 'DEV'
    print(what_is_the_env)
else:
    what_is_the_env = 'PROD'
    print(what_is_the_env)

In this example, it will print “PROD” if the Django application is running in a production environment (DEBUG is False) and “DEV” if it’s running in a development environment (DEBUG is True).

By utilizing these functions, you can ensure that your Django application behaves differently based on whether it’s in a production or development environment, which is crucial for tasks such as debugging, error handling, and configuration management.

  1. Now head over to cPanel. Open the ‘passenger_wsgi.py’ file by clicking on Edit. ‘passenger_wsgi.py’ file is created by cPanel and is your Application startup file.
The cPanel File Manager interface displaying the 'django_test/myproject' directory contents. A contextual menu is open next to the 'passenger_wsgi.py' file with options like Download, View, Edit, Move, Copy, Rename, Change Permissions, Delete, and Compress.
Screenshot of a file manager within cPanel, focused on the contents of the ‘myproject’ directory of a Django application. Visible files include the ‘passenger_wsgi.py’ script, which is highlighted for interaction, and a ‘requirements.txt’ file, suggesting a Python web application setup.
  1. This(Figure below the code snippet) is the code you will find in the passenger_wsgi.py file. We need to make some changes to the code. Comment out the ‘application’ function as shown below. Add the following line.
from myproject.wsgi import application
A text editor within cPanel editing a Python WSGI application file. The code imports OS and SYS modules, comments out an example application function, and imports an application object from a 'myproject.wsgi' module.
A screenshot of a Python code editor displaying a script. The code includes import statements for the ‘os’ and ‘sys’ modules, as well as commented out sections. The uncommented code imports the ‘application’ object from ‘myproject.wsgi’. This file is typically used for configuring the entry point for a WSGI-compatible web server to serve a Django application.
  1. myproject.wsgi’ is basically calling the ‘wsgi.py’ file within the directory. The directory also has our application’s ‘settings.py’ file.
The cPanel File Manager showing the directory contents of 'django_test/myproject/myproject'. Files displayed include 'init.py', 'asgi.py', 'settings.py', 'urls.py', and 'wsgi.py' with their respective sizes and last modified dates, all within a Python Django project structure.
A file manager interface displaying the directory structure of a Django web project named ‘myproject’ with subdirectories and files. The files include ‘asgi.py’, ‘settings.py’, ‘urls.py’, and ‘wsgi.py’ within the ‘myproject’ directory, which are typical components of a Django project’s configuration and URL routing. The directory structure indicates a well-organized Django application setup.
  1. If you Edit/View the ‘wsgi.py‘ file, you will see an instance of our application. This instance is what your are importing in passenger_wsgi.py
A text editor screen in cPanel showing the WSGI configuration file for a Django project. The file contains comments explaining the purpose of the file and Python code importing OS and Django WSGI modules, setting the default settings module, and defining the application callable for the WSGI server.
An open text editor window showing the WSGI configuration for a Django project named ‘myproject’. The Python code imports necessary modules and sets up the WSGI application callable, which is a standard procedure for configuring Django applications to communicate with web servers. The environment variable ‘DJANGO_SETTINGS_MODULE’ is set to ‘myproject.settings’, which points to the settings file within the Django project.
  1. After navigating to Setup Python App and click on the edit button as shown below.
cPanel interface for Python web applications, showing an application with the URI 'pastel-test.cs.mtu.edu/' and the root directory '/home/pasteltest/django_test/myproject'. The application status is indicated as 'started (v3.10.4)'. On the right, action icons for stopping, editing, and deleting the application are displayed.
A screenshot from the cPanel web hosting interface under the Python section, showing a web application overview. The application named ‘pastel-test.cs.mtu.edu’ is currently running with Python version 3.10.4, indicated as ‘started’. The ‘App Root Directory’ is listed as ‘/home/pasteltest/django_test/myproject’. There are action buttons available, including options to edit the application settings, restart the application, or delete it.”
  1. As you can see we have the following. Application URL and Application startup file have been automatically generated.
cPanel's Python application management interface showing an active application. Details displayed include the Python version 3.10.4, application root at '/django_test/myproject', and various fields like Application URL and Passenger log file path. There are options to 'Stop', 'Restart', 'Open' the application, and a 'Destroy' button to remove the application.
This is a screenshot from the cPanel web hosting interface, specifically the Python Web Applications section. The screen shows details for a specific web application. It lists ‘Python version’ as 3.10.4 (recommended), ‘Application root’ directory at ‘django_test/myproject’, and ‘Application URL’ pointing to ‘pastel-test.cs.mtu.edu’. The ‘Application startup file’ is noted as ‘passenger_wsgi.py’ and the ‘Application Entry point’ is set to ‘application’. There’s also a ‘Passenger log file’ path provided. Options to ‘Stop App’, ‘Restart’, and a destructive ‘Destroy’ button are visible, with a highlighted instruction above to enter the virtual environment using a provided command line.
  1. Now it is time to install Django on our server. Copy the following environment path.
Bar in cPanel indicating the command to enter the virtual environment for a Django project: 'source /home/pasteltest/virtualenv/django_test/myproject/3.10/bin/activate && cd /home/pasteltest/django_test/myproject'.
This image displays a snippet of text from a web hosting interface instructing the user on how to enter a virtual environment for a Django project. The command provided is source /home/pasteltest/virtualenv/django_test/myproject3.10/bin/activate && cd /home/pasteltest/django_test/myproject, which activates the Python virtual environment and changes the directory to the project folder.
  1. Go back to cPanel’s home page and you should see the Advanced section as you scroll down. Click on ‘Terminal’
Control panel interface under the 'Advanced' section featuring icons for 'Terminal', 'Cron Jobs', 'Track DNS', 'Indexes', 'Error Pages', 'Apache Handlers', and 'MIME Types'.
This image shows a section of a web hosting control panel labeled ‘Advanced’ featuring icons for various advanced hosting features such as Terminal, Cron Jobs, Track DNS, Indexes, Error Pages, Apache Handlers, and MIME Types. The Terminal icon is highlighted, indicating it is likely the focus of the task at hand or the next step to be taken in the web hosting management process.
  1. Copy and paste the path on the terminal and then press Enter. Once you are inside the virtual environment, then execute the command: ‘pip install Django’. You can install all the required libraries with specific versions in your virtual environment.
The cPanel Terminal interface with a command prompt open. The displayed command is 'source /home/pasteltest/virtualenv/django_test/myproject/3.10/bin/activate && cd /home/pasteltest/django_test/myproject', which activates the Python virtual environment and changes the directory to the Django project folder.
This image displays a command-line interface within the cPanel’s Terminal tool. A command is typed out, ready to be executed, which activates a Python virtual environment and navigates to a Django project directory. The prompt indicates the user ‘pasteltest’ logged into the server, positioned at the path where the Django project resides, signifying a development or deployment step for a web application.
  1. You can also install the required libraries by adding the projects ‘requirements.txt’ file below and clicking on ‘Run Pip Install’ or entering the path to the script file and clicking on ‘Run Script’. This will install all required libraries and tools on the server.
cPanel interface showing a section for 'Configuration files' with options to 'Run Pip Install'. Below, there is an 'Execute python script' section where users can enter the path to a script file and run it. There's also an 'Add' button to include additional scripts and a 'Run Script' button. At the bottom, there's an 'Environment variables' section with an 'Add Variable' button, and a message 'NO RESULT FOUND' indicating no current output or variables set.
This screenshot shows a section of a web application control panel, specifically for configuration files. There are options to run Pip install, execute a Python script with a field to enter the path to the script file, and add environment variables. There’s a button labeled ‘Run Script’ to execute the specified command and an area indicating that no result has been found yet, suggesting that no scripts have been run or no output is currently available.
  1. Now you have to restart the application before loading it on the browser. Navigate to Setup Python App and restart the application.
cPanel's Python Web Applications section displaying an application with details. It shows 'App URI' as 'pastel-test.cs.mtu.edu/', 'App Root Directory' as '/home/pasteltest/django_test/myproject', and status as 'started (v3.10.4)'. To the right, action buttons for 'Restart the application' are visible.
The image displays a section of the cPanel web hosting interface under the ‘Python’ section. It showcases an overview of a Python web application with details such as the ‘App URI’, which is the URL where the app is accessible (pastel-test.cs.mtu.edu/), and the ‘App Root Directory’, indicating the server path to the application files (/home/pasteltest/django_test/myproject). The application’s status is shown as started with the version number (v3.10.4). There are action buttons available for managing the application, including options to restart the application, edit details, and delete the application.
  1. Edit/View your ‘settings.py’ file and make sure you have added the application URL in the ALLOWED_HOSTS list. Also, according to the requirements check whether DEBUG has to be set to True or False
A cPanel text editor screen displaying a Django settings.py file. The code shows configuration settings including 'DEBUG = False' and 'ALLOWED_HOSTS' with an entry for 'pastel-test.cs.mtu.edu'. Comments in the code warn against keeping the secret key used in production and running with debug turned on in production.
The image shows a text editor with a portion of a Django project settings file open for editing. The ALLOWED_HOSTS setting is highlighted, with a single entry that specifies the domain ‘pastel-test.cs.mtu.edu’ as an allowed host, indicating that the Django application can serve requests from this domain. There are several commented-out security warnings reminding the user to keep the secret key secret and not to run with debug turned on in production. The debug mode is set to False, which is appropriate for a production environment. Other visible settings include the INSTALLED_APPS and MIDDLEWARE configurations, which list Django’s default components.
  1. Edit/View the following files by navigating to the project’s application folder.
cPanel File Manager interface showing the directory structure of a Django project within 'django_test/myproject/myapp'. It includes Python files such as 'init.py', 'admin.py', 'apps.py', 'models.py', 'tests.py', 'urls.py', and 'views.py', each with their file size and last modified date.
A screenshot of a file management interface, showing a directory structure for a Django project. In the main panel, the ‘myapp’ folder is expanded to reveal Python code files, such as ‘admin.py’, ‘apps.py’, ‘models.py’, ‘tests.py’, ‘urls.py’, and ‘views.py’. The directory path at the top indicates the current location within the project folder. The surrounding interface includes options for upload, download, delete, and other file management actions.
  1. In the ‘urls.py’ file you will find a list of all your paths/urls in the ‘urlpatterns’ list.
A cPanel text editor displaying a Django urls.py file with Python code. The code imports the 'path' function from 'django.urls' and 'views' from the current directory. It defines a list called 'urlpatterns' with a single path function mapping the root URL ('') to 'views.home' with the name 'home'.
A screenshot showing a Python code editor with a file being edited. The file contains import statements and a URL pattern list for a Django web application. The code imports the ‘path’ function from ‘django.urls’ and ‘views’ from the current directory. It defines a single URL pattern that maps the root URL (”) to a view function named ‘home’, with the name ‘home’ for reverse URL matching.
  1. In the ‘views.py’ file you will find all the views and their associated methods of your application.
A screenshot of a Python code editor showing the contents of a views.py file in a Django application. The file contains import statements for Django’s ‘render’ shortcut and ‘HttpResponse’ class. A function named ‘home’ is defined, which takes a ‘request’ parameter and returns an ‘HttpResponse’ object with the text ‘Hello, World!’. This function is intended to be a view that outputs a simple greeting when a user visits the home page of the web application.
  1. You can test the application by navigating to Setup Python App and clicking on the application URL as shown below.
The Python Web Applications section in cPanel, displaying an application with the App URI 'pastel-test.cs.mtu.edu/' and App Root Directory '/home/pasteltest/django_test/myproject'. The application's status is marked as 'started (v3.10.4)', and there are action buttons for managing the application.
  1. Result:
Web browser displaying a webpage with a plain text message 'Hello, World!' on a white background, located at the URL 'pastel-test.cs.mtu.edu'.


References

  1. “Deploying Django Web Application using Cpanel. | by Python Zimbabwe Open Source.” 2020. Medium. | https://medium.com/@pyzimos/deploying-django-web-application-using-cpanel-6687b8057439
  2. “Deploying/Hosting a Django Website on cPanel with Git Version Control.” 2021. Naitik Parmar. | https://parmarnaitik0909.medium.com/deploying-hosting-a-django-website-on-cpanel-with-git-version-control-6e8dce70a316
  3. “Deploying your Python/Django app on cPanel using a git repository – Webhostpython.” 2020.Webhostpython. | https://blog.webhostpython.com/2020/10/14/deploying-your-python-django-app-on-cpanel-using-a-git-repository/.
  4. “Django Deploy to cPanel: Passengerfile.json’ for reading: Permission denied (errno=13).” 2021.Stack Overflow. | https://stackoverflow.com/questions/69145378/django-deploy-to-cpanel-passengerfile-json-for-reading-permission-denied-err.
  5. Guide, step. n.d. “Charlie Eleanor Awbery.” Charlie Eleanor Awbery. Accessed April 10, 2023. | https://charlieawbery.com/deploying_django/4/.
  6. Gyford, Phil, and Phil Gyford’s. 2022. “Hosting a Django site on cPanel.” Phil Gyford’s website. | https://www.gyford.com/phil/writing/2022/05/24/hosting-django-cpanel/.
  7. “How do I deploy a Django website with cPanel?” 2020. Stack Overflow. | https://stackoverflow.com/questions/59801135/how-do-i-deploy-a-django-website-with-cpanel?rq=1.
  8. “How to deploy with WSGI.” n.d. Django documentation | https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/.
  9. “How to use the Python Selector in cPanel.” n.d. A2 Hosting | https://www.a2hosting.com/kb/cpanel/cpanel-software/using-the-python-selector/.
  10. “Setup Python WSGI apps on cPanel (Flask/Django).” 2020. DevDungeon. | https://www.devdungeon.com/content/setup-python-wsgi-apps-cpanel-flaskdjango#toc-8.
  11. Sims, Tonya. 2021. “How Python’s WSGI vs. ASGI is Like Baking a Cake | Vonage API Developer.” Vonage Developer. | https://developer.vonage.com/en/blog/how-wsgi-vs-asgi-is-like-baking-a-cake.
  12. Stephen, Nganga. 2022. “How to publish/deploy Django website in Cpanel ‣ ngangasn.com.” Nganga Stephen | Web Developer \ https://ngangasn.com/deploy-django-on-shared-hosting-ultimate-guide-to-hosting-django-on-cpanel/#setup-domain.
  13. Umer, Hafiz M. 2019. “Deploy Django app on Shared Hosting using CPanel.” Umer Softwares Blog. | https://blog.umersoftwares.com/2019/09/django-shared-hosting.html.

HideTech

Proudly powered by HIDE