How to Deploy Django Application and connect a Database in cPanel
Django is one of the most popular web frameworks for building scalable and secure applications. While deploying Django on cloud servers is common, many developers and website owners prefer using cPanel for hosting due to its user-friendly interface and affordability.
If you’re looking for a detailed, step-by-step guide on how to deploy a Django application on cPanel, you’re in the right place.
- How to Deploy Django Application and connect a Database in cPanel
- Why Deploy Django on cPanel?
- Prerequisites
- Step 1: Create a Python Application in cPanel
- Step 2: Upload Your Django Project to cPanel
- Step 3: Install Dependencies
- Step 4: Configure Application Startup File
- Step 5: Configure Allowed Hosts
- Step 6: Common Errors and Fixes
- Conclusion
- Frequently Asked Questions (FAQs)
Deploying a Django application on cPanel might seem challenging, but with the right steps, you can get your project up and running smoothly. This guide will walk you through the process in a friendly, easy-to-follow manner.
In this comprehensive guide, we’ll walk you through every step, from setting up your Django project to configuring your database, handling static files, and securing your application.
Whether you’re a beginner or an experienced developer, you’ll find all the necessary details here to successfully deploy your Django app on cPanel.
Why Deploy Django on cPanel?
cPanel is one of the most popular web hosting control panels, widely used for managing websites, databases, and emails. Many shared hosting providers offer cPanel, making it an affordable and accessible option for hosting Django applications.
While cPanel is traditionally optimized for PHP applications, you can still run Django by using its Setup Python App feature.
Prerequisites
Before you begin, ensure you have:
- A Django project ready for deployment
- A cPanel hosting account
- SSH Access (Optional)
- A registered domain or subdomain pointed to your cPanel server
- Python 3.x support (provided by your hosting provider)
- Basic familiarity with the command line
Watch the Video Tutorial
For an easier way to deploy a Django application on cPanel, watch the YouTube video below. If you prefer a detailed, step-by-step written guide, continue reading.
Step 1: Create a Python Application in cPanel
- Log in to your cPanel account.
- Scroll down to the Software section and click on Setup Python App.

- Click Create Application and select:
- Python version (Choose the latest available 3.x version).
- Application root:
/home/yourusername/yourprojectname
(or any preferred directory). For instance, if your application’s root folder is named Django-project and you are using your primary domain, the application root directory will be public_html/Django-project. In this guide, we are using a subdomain, so our application root becomes py.hostplus.top. - Application URL: Select the domain or subdomain you want to use.
- Application startup file: Leave it blank for now (we’ll configure this later).
- Application Entry point: Leave it blank for now (we’ll configure this later).

- Click Create and note the Virtual Environment Path displayed.

Step 2: Upload Your Django Project to cPanel
You can upload your Django project in two ways:
Option 1: Upload via File Manager
- Go to Files section in the cPanel and click File Manager.
- Navigate to the application root directory you set in Step 1.
- Click Upload and select your Django project’s
.zip
file.

- Extract the
.zip
file after uploading. Right-click the uploaded ZIP file and select ‘Extract‘ to unzip it.
Ensure that all files are located in the py.hostplus.top folder (or any preferred directory). If they are not, navigate to the extracted folder, select all files, and move them to the py.hostplus.top folder or your chosen directory.
Suggested Reading: How to Upload Your Website (in 3 Simple Steps)

Option 2: Clone from GitHub
- Open Terminal from cPanel or connect via SSH.
- Navigate to the application directory:
cd /home/yourusername/yourprojectname
- Clone your project from GitHub:
git clone https://github.com/yourusername/your-repository.git
Step 3: Install Dependencies
Your Django project has dependencies listed in requirements.txt
. You can install them in two ways:
Method 1: Using Setup Python App Page
- Go back to Setup Python App.
- Click the Edit button for your application.
- Find the Configuration files section and enter:
requirements.txt
then click Add button

- Click “Run Pip Install”, then select the requirements.txt file to install the dependencies.
Method 2: Using the Terminal
- Activate the virtual environment: copy and paste the Virtual Environment Path from Step 1 or manually activate the virtual environment.
Virtual Environment Path from Step 1
source /home/cpusrplus/virtualenv/py.hostplus.top/3.10/bin/activate && cd /home/cpusrplus/py.hostplus.top
Manually activate the virtual environment
source /home/yourusername/virtualenv/yourprojectname/3.x/bin/activate
- Install dependencies: In the Terminal, navigate in your project directory and run the command to install the dependencies;
pip install -r requirements.txt
Step 4: Configure Application Startup File
- Open Setup Python App and locate your application.
- Find the Application Startup File section: Enter the location of your
wsgi.py
file. In my case, it isstudybud/wsgi.py
, then save and restart the application.

- Alternatively, you can keep the
passenger_wsgi.py
file and follow the rest of the process to modify it accordingly. - Inside your project folder, edit the
passenger_wsgi.py
file and add the following code snippet:
import os
import sys
# Adjust this path
sys.path.insert(0, "/home/yourusername/yourprojectname")
os.environ['DJANGO_SETTINGS_MODULE'] = 'yourprojectname.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Remember to update the following with your Django project directory:
sys.path.insert(0, "/home/yourusername/yourprojectname")
For example, in my case, since I’m using a subdomain, it would be:
sys.path.insert(0, "/home/cpusrplus/py.hostplus.top/studybud")
Additionally, update your Django settings module:
os.environ['DJANGO_SETTINGS_MODULE'] = 'yourprojectname.settings'
For example:
os.environ['DJANGO_SETTINGS_MODULE'] = 'studybud.settings'
- Restart the application in Setup Python App.
Suggested Reading:
- How to Set Up a MySQL Database & User in cPanel (2 Easy Methods)
- How to Set Up a PostgreSQL Database and User in cPanel
Step 5: Configure Allowed Hosts
Edit the settings.py
file inside your Django project:
ALLOWED_HOSTS = ['yourdomainname.com', 'www.yourdomainname.com']
If your site is on a subdomain:
ALLOWED_HOSTS = ['sub.yourdomainname.com']
Save the file and restart your application.
Step 6: Common Errors and Fixes
Error 1: Module Not Found
Solution:
- Make sure dependencies are installed correctly using:
pip install -r requirements.txt
- Restart the application in Setup Python App.
Error 2: Internal Server Error (500 Error)
Solution:
- Check your
passenger_wsgi.py
file for any incorrect paths. - Review
stderr.log
in the Django application project folder. - Verify that
ALLOWED_HOSTS
includes your domain.
Error 3: Static Files Not Loading
Solution:
- Add the following to
settings.py
:
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
- Run:
python manage.py collectstatic
- Ensure the static files directory is accessible.
Error 4: Database Connection Issues
Solution:
- If using SQLite, check that the database file is in the correct location.
- If using MySQL/PostgreSQL, ensure the credentials in
settings.py
match your cPanel database. - Verify that the database host is set to
localhost
.
Conclusion
Deploying a Django application on cPanel is straightforward when you follow the correct steps. From setting up your Python environment to configuring the startup file, this guide provides everything you need to launch your app successfully.
Still facing issues? Drop a comment or reach out to Aveshost support for additional assistance!
Happy coding! 🚀
Frequently Asked Questions (FAQs)
Yes, you can deploy a Django application on cPanel without root access by using the Setup Python App feature in cPanel. This allows you to create a virtual environment and run your app without needing administrative privileges.
It is recommended to use the latest Python 3.x version available in cPanel. Check your hosting provider’s available Python versions in the Setup Python App section.
You can upload your project files via FTP/File Manager by compressing them into a ZIP file or, preferably, using Git through the terminal within the virtual environment.
You can install dependencies either by copying and pasting the contents of requirements.txt
into the “Setup Python App” page and clicking “Run Pip Install,” or by using the terminal within the virtual environment and running pip install -r requirements.txt
.
passenger_wsgi.py
file, and how do I configure it?The passenger_wsgi.py
file is responsible for starting your Django application on cPanel. You need to modify it to point to your Django project’s wsgi.py
file and set the DJANGO_SETTINGS_MODULE
correctly.
A 500 error can occur due to various reasons, including:
1. Incorrect passenger_wsgi.py
configuration.
2. Missing or incorrect ALLOWED_HOSTS
settings.
3. Uninstalled dependencies.
4. Errors in the settings.py
file.
Check the stderr.log in your application directory to diagnose the issue.
1. Add this to settings.py
:STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
2. Run the command:python manage.py collectstatic
3. Ensure your web server serves static files correctly.
Go to Setup Python App in cPanel and click Restart next to your application.
Yes! Configure your settings.py to connect to the MySQL database provided by cPanel. Example:DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_db_name',
'USER': 'your_db_user',
'PASSWORD': 'your_db_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
1. Set your domain name in the Setup Python App under the Application URL.
2. Add your domain to ALLOWED_HOSTS
in settings.py
:ALLOWED_HOSTS = ['yourdomainname.com', 'www.yourdomainname.com']
This error usually indicates that the passenger_wsgi.py
file is not pointing to the correct wsgi.py
file or that the DJANGO_SETTINGS_MODULE
is incorrect.
Double-check your file paths and settings.
ALLOWED_HOSTS = ['*']
safe for a production environment?No, it is not safe. Using ALLOWED_HOSTS = ['*']
allows any host to access your application, which poses a significant security risk. You should always specify your actual domain names in a production environment.
Suggested Reading: