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.
The application form appears:
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.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.
Enter
to activate the virtual environment.To install Django, enter the following command:
To check the installed version of Django, use this command:
Step 3 — Set up a new Django project
To start a new Django project, enter the following command:
To set up directories for the project’s static files, run the following commands:
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:
Find the STATIC_URL line, then add these lines directly below it:
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.
Use the collectstatic command to transfer static files to the directory specified in your settings.
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.