Live Software Developer Ltd Logo
HomeServicesWeb HostingDomainsEmail HostingArticlesContact UsAbout Us

How to Deploy a Django Application to cPanel

How to Deploy a Django Application to cPanel

Created On:
Wed Sep 11 2024 (3:22:33 PM)
Updated On:
Wed Sep 11 2024 (3:22:33 PM)
Deploying a Django application on cPanel requires several steps, from preparing your project files to configuring your cPanel environment. Here's a detailed, step-by-step guide to help you through the process.

Deploying Your Django Project to cPanel: Step-by-Step Guide

Deploying a Django application on cPanel requires several steps, from preparing your project files to configuring your cPanel environment. Here's a detailed, step-by-step guide to help you through the process.

1. Creating the requirements.txt File

First, ensure that all the required packages for your project are captured in a requirements.txt file. To generate this file, run the following command in your terminal:

pip freeze > requirements.txt

This command saves a list of all the installed Python packages into the requirements.txt file, which will be used for installing dependencies on cPanel.

2. Preparing and Exporting Static Files

Next, you'll need to prepare your project for deployment. This includes collecting your static files.

Run the following command to gather all your static files into the directory specified in your settings.py file (typically STATIC_ROOT):

python manage.py collectstatic

Or, if you're using python3, run:

python3 manage.py collectstatic

This will move all your static files into the appropriate folder (such as staticfiles), which you can later serve in production.

Before proceeding, set the DEBUG option in your settings.py file to False, which is essential for production.

Zipping Your Project

Once your static files are collected, zip your project files. Make sure to include the requirements.txt, your Django app files, and static files, but exclude unnecessary large media files to keep the zip size manageable.

3. Uploading the Project to cPanel

  1. Log in to your cPanel account.
  2. Navigate to the File Manager.
  3. In File Manager, create a new folder (e.g., project).
  4. Upload your zipped Django project into this folder.
  5. Once uploaded, right-click the zip file and select Extract to unzip it.

Your project files should now be in the project folder.

4. Setting Up Your Python Application in cPanel

  1. In cPanel, go to the Setup Python App section (this may be under "Software").

  2. Click Create Application and fill out the required fields:

    • Python Version: Select Python 3.10 or later.
    • Project Root: Enter the folder name where your project is located (e.g., project).
    • Domain: Choose the domain or subdomain where your project will be hosted.
    • Application Entry Point: Leave this as the default passenger_wsgi.py file.
    • Log File: Specify a path for your log file (e.g., /project/applog.log).
  3. Click Create to set up your application.

5. Installing Project Dependencies

  1. Before installing your project dependencies, stop the application to avoid conflicts during installation.
  2. Scroll down in the Python application setup page, find the configuration files section, and add the requirements.txt file.
  3. Click Run pip install and select the requirements.txt file to install all the dependencies from the file.

6. Configuring the Database and Running Migrations

If you're using a production database (such as MySQL or PostgreSQL), follow these steps:

  1. Create a new database in cPanel (you can use MySQL or PostgreSQL). Add a user and grant full permissions to this user.
  2. Update your DATABASES configuration in settings.py to reflect the new database details.

Next, run the migrations to set up the database tables. In the Setup Python App section, find the input box labeled Run Python Script and type:

manage.py migrate

This will apply all the migrations. If there are any issues, the output will appear below the input box.

7. Updating the Passenger WSGI File

The passenger_wsgi.py file is the main entry point for your application. You need to modify this file to point to your Django project's wsgi.py.

  1. In File Manager, navigate to your project folder.
  2. Right-click on the passenger_wsgi.py file and select Edit.
  3. Replace its contents with:
from myproject.wsgi import application

Make sure to replace myproject with the actual name of your Django project.

Save the file.

8. Finalizing the Deployment

  1. Since DEBUG is set to False in production, you need to ensure your static files are properly served. Move the static files to the public directory of your domain or subdomain (e.g., /public_html/staticfiles).
  2. In Setup Python App, restart the application by selecting it and clicking Start.

9. Final Checks

Your Django app should now be up and running! Here are some final checks and tips:

  • Make sure that your database and static files are properly configured.
  • If everything is working correctly, you'll see your application live at your domain.
  • Check the log file (applog.log) for any issues during startup or while running the application.

Congratulations! You've successfully deployed your Django app to cPanel.