How to Deploy Django App on cPanel (The Ultimate Guide)

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.

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

  1. Log in to your cPanel account.
  2. Scroll down to the Software section and click on Setup Python App.
How to deploy Django App - Setup Python App
  1. 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).
How to deploy Django App - Creating the Python Web Application
  1. Click Create and note the Virtual Environment Path displayed.
How to deploy Django App - Virtual Environment Path

Step 2: Upload Your Django Project to cPanel

You can upload your Django project in two ways:

Option 1: Upload via File Manager

  1. Go to Files section in the cPanel and click File Manager.
  2. Navigate to the application root directory you set in Step 1.
  3. Click Upload and select your Django project’s .zip file.
How to deploy Django App - File Manager file upload
  1. 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)

How to deploy Django App - Move files to the chosen directory

Option 2: Clone from GitHub

  1. Open Terminal from cPanel or connect via SSH.
  2. Navigate to the application directory:
cd /home/yourusername/yourprojectname
  1. 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

  1. Go back to Setup Python App.
  2. Click the Edit button for your application.
  3. Find the Configuration files section and enter: requirements.txt then click Add button
How to deploy Django App - adding requirement dependencies file
  1. Click “Run Pip Install”, then select the requirements.txt file to install the dependencies.

Method 2: Using the Terminal

  1. 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
  1. 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

  1. Open Setup Python App and locate your application.
  2. Find the Application Startup File section: Enter the location of your wsgi.py file. In my case, it is studybud/wsgi.py, then save and restart the application.
How to deploy Django App - Application Startup File Modification
  1. Alternatively, you can keep the passenger_wsgi.py file and follow the rest of the process to modify it accordingly.
  2. 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'
  1. Restart the application in Setup Python App.
Suggested Reading:

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:

  1. Add the following to settings.py:
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
  1. Run:
python manage.py collectstatic
  1. 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)

Suggested Reading:

Picture of James Wilson

James Wilson

For over 10 years, James has been working in the tech industry. He's an expert in areas like software development, cybersecurity, and cloud computing. He understands the challenges and opportunities that new tech companies face, and he's known for coming up with creative solutions to help them succeed.

Leave a Reply

Enjoy 20% Off Your First Order!

Use promo code WELCOME at checkout to claim your exclusive discount.

Get 20% OFF on your first order

Oh hi there! It’s nice to meet you.

Please Kindly, sign up to receive awesome content in your inbox, every month.