Deploying a Django App on cPanel with Git Version Control

Naitik Parmar
5 min readFeb 7, 2021

--

So, you’ve built a fully functional website using Django Framework as backend and have it on GitHub. Now you must be wondering where can you deploy your master piece. Well, there are many good options to deploy a Django website, such as AWS, Google Cloud, Heroku etc. but most of them are very expensive (after their free trial expires :P). These services might be more beneficial if the website is resource demanding or if the website might scale after time. But for some relatively less resource-hungry websites, cPanel can be a good choice.

In this article, I’ll show all the steps required to deploy a Git version-controlled Django website to a cPanel. Also, I’ll suggest some options to serve your static files while in production.

NOTE- Before purchasing a cPanel, please make sure that it offers support for Python Applications and has a ‘Setup Python Application’ feature in the ‘Software’ section.

Modifying your Django App

First, lets make some changes in settings.py-

DEBUG = FalseALLOWED_HOSTS = ['127.0.0.1', 'www.yourdomain.com']

Now, lets make sure all our static files are collected at one place so that they will be easily served during production. Add this line of code at the bottom of settings.py-

# Make sure that BASE_DIR is defined somewhere at the top
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

This line basically tells Django where to store all the static files of all the applications in your Django project.

Now, run this command on your command line-

python manage.py collectstatic

This will collect all the static files of your project to the location ‘STATIC_ROOT’.

Updating your requirements.txt

Create a file named ‘requirements.txt’ in the root directory and write names of all the python dependencies used by you in your project. Like this-

django
gunicorn
pillow
django-cleanup
beautifulsoup4
whitenoise
django-environ
requests

You can also specify the specific version of the dependency.

That's it! Now, we can move to the cPanel. Commit all these changes to GitHub.

cPanel- Creating Git Version Control instance

  1. Login to your cPanel(Go to- yourdomain.com/cPanel)Click on Git Version Control-

2. Click on the ‘Create’ button. After that, you’ll see something like this-

3. After filling this, hit the create button.

NOTE- Repository must be ‘public’ on GitHub. If your repository is ‘private’, you need to follow a different procedure to clone it on the cPanel. You can refer this- https://docs.cpanel.net/knowledge-base/web-services/guide-to-git-set-up-access-to-private-repositories/

cPanel- Creating Python Application

Now its time to create a Python Application inside cPanel.

  1. In the Software tab, click on ‘Setup Python App’-

2. After that, click on ‘Create’ button, and fill the form as follows(Select your appropriate python version) -

NOTE -Any Django related errors that you may encounter after deployment will be saved in the Passenger log file.

3. Lets install all the python libraries mentioned in the file ‘requirements.txt’. Go to ‘Setup Python Application’, find the application that you just created and click on the edit icon. Scroll to the bottom and you’ll see this form. Fill it as follows-

After this, save the changes and restart your application.

You can now edit newly created passenger_wsgi.py file to point to your project’s actual wsgi file. To do so-

  1. Open ‘File Manager’ from cPanel’s home. Then open ‘repositories’ folder and open your particular repository. Here, you’ll see a file named ‘passenger_wsgi.py’. Right click on it and click ‘edit’-

2. In the text editor, remove the pre-existing code and write this-

import os
import sys
from your_project_name.wsgi import application

3. Click on ‘Save changes’ and you’re done!

Now, you can try to visit your website on your domain! Your website should be visible on the browser, but, without any styling (since we haven’t configured our static files on the cPanel yet). If that’s the case, you can move to the next step. Otherwise, you may encounter an error like this when you visit your domain-

Don’t panic, it just asks you to revise your File permissions on the cPanel. Go to cPanel’s home, open the ‘File Manager’. Go to the ‘repositories’ folder, right click on your repository’s folder and click on ‘Change permissions’-

Then, match your permissions with these-

After making the changes, click ‘Change Permissions’. Probably, the error will be gone and you could move on to the next step.

cPanel- Configuring static files

  1. Go to cPanel’s home, open the ‘File Manager’. Go to ‘repositories’ folder and open your repository.
  2. You’ll see a folder named ‘static’ in the root directory of your repository. Right click on this folder and select ‘Copy’.
  3. Now, if you’re linking this website to your primary domain, Enter ‘/public_html/’ in the input box, like this-

But, if you’re linking a secondary domain to this website, type the location of that domain in the input box.

That's it ! Bravo ! You’ve successfully deployed your Django site to a cPanel. Now you can use Git Version Control to pull post-deployment changes on the cPanel so that your website remains up-to date. P.S, you can also use environment variables to secure your Django Secret Key and other important credentials during production.

Make sure to run the command ‘python manage.py collectstatic’ on your local machine before pushing any changes in the future and transfer new static files after pulling the code on cPanel (just like we did in the last step).

--

--

Naitik Parmar
Naitik Parmar

Responses (4)