How to install Django app in Namecheap cPanel

To install a Django app in Namecheap cPanel, you can follow these steps:

  1. First, make sure that you have a domain name registered with Namecheap and a hosting account set up with cPanel. If you don’t have these, you’ll need to purchase them before you can proceed.
  2. Next, log in to your cPanel account and navigate to the “Software” section. Here, you should see an option for “Setup Python App.” Click on this to proceed.
  3. On the “Setup Python App” page, you’ll see a form that allows you to configure your Django app. Fill in the form as follows:
  • In the “App Domain” field, enter the domain name that you want to use for your Django app.
  • In the “App Root” field, enter the root directory for your Django app. This is typically something like “/home/username/django_app”.
  • In the “App URL” field, enter the URL that you want to use to access your Django app. This should be in the form “http://yourdomain.com/django_app“.
  • In the “Python Version” field, select the version of Python that you want to use for your Django app. Django is compatible with Python 3.5 and above, so you can choose any version that is higher than 3.5.
  1. Once you have filled out the form, click the “Create” button to create your Django app. This will create a new virtual environment for your app, install Django, and set up all of the necessary files and directories.
  2. After your Django app has been created, you can access it by visiting the URL that you specified in the “App URL” field. You should see the Django welcome page, which means that your app has been installed and is up and running.

I hope this helps! If you have any questions or need further assistance, don’t hesitate to ask.

Get Command-Line Access

You can get command-line access to your hosting server in two ways: using SSH or via an online terminal. If you want to work via SSH, read your hosting provider’s documentation on how to do that. I will be using the online terminal because it is less complicated to use.

In your CPanel, open the Manage Shell option.

Turn on Enable SSH access

Now you will see the Terminal option in CPanel. Open it to check if it is working.

If a terminal like shown in the below screenshot opens, you have got the terminal access to your server. Keep this browser tab open. We will need it in the upcoming steps.

Setup a new Python app

In your CPanel, open Setup Python App

Click on the Create Application button.

  • Set the Python Version to 3.9.12
  • The Application Root is the folder in your hosting where the python application will be created. This will be the folder where you will start or upload your Django project.
  • In the Application URL, select the domain/subdomain and sub-directory where you want your Django application to be live. This will be the web address where you will see your Django app.
  • In the Application startup file, type
passenger_wsgi.py
  • In the Application entry point, type
application

You can also see the settings in the screenshot below. After you are done, click on Create.

Open the URL where you set up your python app. You should see a page like this. Do not worry about the error yet. It will disappear once we create our Django app.

Install Django

After your python app is set up, you will see a page as shown in the screenshot below. Click the command to enter the virtual environment to copy it to the clipboard.

Enter the command into the online terminal by Right Click / Paste and press enter. You will enter the python virtual environment. In my case, the command is

source /home/XXXX/virtualenv/django_test/3.9/bin/activate && cd /home/XXXX/django_test

It will not be the same for you. If there is no “&&cd …” part in your command, you will manually have to cd to the directory where you have set up your application.

Now you have to install Django. I have tested Django 4.0.4 on shared hosting and it works fine with the deployment method I am describing in this post. You can install Django 4.0.4 using the following command:

pip install django==4.0.4

If any other modules are required for your project, install them here using pip.

Confirm the Django installation by running the following command.

django-admin --version

Start a Django project

Create a new project

Go to the terminal in your Cpanel and enter the following command

django-admin startproject myapp ~/django_test

Make sure to replace “myapp” with your application name and “django_test” at the end with the directory name where you set up your python app. The directory name should be the same as the one used while setting up the python app.

Now open the file manager in CPanel (You can also use FTP) and go the folder where you set up your Django app. In my case, it is the “django_test” folder. You will see a folder with your app’s name and the manage.py file here.

Edit the passenger_wsgi.py file.

Delete everything in the file and add just one line.

from myapp.wsgi import application

Do not forget to replace “myapp” with your application name. Click on Save Changes to save the changes to the file.

Now open your application folder_ in my case, it is “myapp”.

Edit the settings.py file.

In the ALLOWED_HOSTS list, add the URL where our application will be running (which you provided while setting up the python app). In my case, it is “test.umer.link”. If your site has a www variant, make sure to add that here too if you want your site to be working in www as well.

Now go to Set up Python app in your CPanel and Restart your application.

Now open the URL where you have set up your application and you should see the Django welcome page.

Upload an existing project

Upload your project into the folder where you have set up the python app.

Edit the passenger_wsgi.py file

Delete everything and add the following code:

from app.wsgi import application

Make sure to replace “app” with your own Django project name. It is the name of the folder that contains the settings.py file.

Edit your project’s settings.py file and add your application URL to the ALLOWED_HOSTS list. If your domain also has a www variant, make sure to add that too to the list.

Set up the database

In this part, we will set up a MySQL database from the hosting with Django. It is also possible to use PostgreSQL database. I have a complete tutorial on that here. For now, let’s continue with the MySQL database because it is commonly available in most of the shared hosting packages.

Go to the MySQL Databases option in CPanel.

Create a new Database

Create a new User. Copy the password of this user somewhere because we will need it in the upcoming steps.

Add the user to the database

Allow all the privileges to this user.

Go to the terminal again. Enter the virtual environment and run the following command to install mysql with pip.

pip install pymysql

Edit your settings.py file. Replace the default database code with this.

 'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'databasename',
        'USER': 'databaseusername',
        'PASSWORD': 'databasepassword',
        'HOST': 'localhost',
        'PORT': '3306',
    }

Make sure to replace the databasename with the database name, databaseusername with the user name and databasepassword with the password for the user.

Now edit the __init__.py file in the app directory.

Add the following code to the file and save it.

import pymysql

pymysql.install_as_MySQLdb()

Apply migrations to the database.

python manage.py makemigrations
python manage.py migrate

Create a superuser now if you want to access the admin panel. This step is optional.

python manage.py createsuperuser

Set up static files

Edit your settings.py file and add the following two lines in the static files section. Make sure to replace username by your CPanel username and the domainroot with the path of the directory which is ser as the domain root.

STATIC_URL = '/static/'
STATIC_ROOT = '/home/username/domainroot/static'

Now open the terminal and run the following command.

python manage.py collectstatic

Go to the domain root folder and check if the static folder has been successfully copied.

Go to “Setup Python App” in CPanel and Restart the app.

If your project is using media files as well, I have written a complete guide about media files and static files in Django on shared hosting which you can see here. (The link is also available at the end of the post)

Test the app

Now go to http://yourappurl/admin and log in using the superuser details you created previously.

If you see the admin panel, congratulations! your Django app has been deployed on your shared hosting.

Deploy in 5 minutes

After you have followed this tutorial successfully and practiced deploying an existing application twice, you can deploy the Django app to your shared hosting in under 5 minutes using the pdf handbook that I have created. It just highlights the steps that you have to go through in the process of deploying your application.

Download PDF handbook

Frequently Asked Questions

FAQs

It keeps on showing the error page and I don’t know what went wrong

In your CPanel’s file manager, go to the project folder and see the error logs in the “stderr.log” file.

Can I use git to get my files?

Yes. You can use git to get your files. Open the terminal in CPanel (or get command-line access via ssh). Navigate to the folder where you want to set up your django app and use git to get the files in that directory.

My hosting does not support python

Get a shared hosting that supports python. I use and recommend Namecheap shared hosting.

In the wsgi file, I have to use the name of the django app or the django project?

You have to use the name of the project. The contents of the passenger_wsgi.py will be:

from projectname.wsgi import application

You can actually find the wsgi.py file inside the project folder. It is in the same folder as your settings.py.

I have made some changes in the files and they are not updated on the website

You need to restart the python app every time you make some changes in the files. There are a few points to note:

  • If you have made changes in models.py, restart the python app and do migrations.
  • If you have added any static files, restart the python app and then do collectstatic.
  • For any other file, just simply restart the python app.
Saving an ImageField gives a 404 page

See this stack overflow for the solution to this problem https://stackoverflow.com/questions/63328969/cannot-upload-media-files-on-cpanel-using-django

Related Articles

Amazon FBA How To Become a Successful Seller in 2023

This article from DOC EDUCATES is a free guide explaining how to sell on amazon for beginners, mostly explaining FBA (Fulfilled By Amazon) Business model. In this guide, we will cover all the steps needed to sell on Amazon FBA (Fulfilled By Amazon). We will show you how to set up your account, take pictures of your products, list them for sale and manage orders.

Amazon FBA How To Become a Successful Seller in 2023

This article from DOC EDUCATES is a free guide explaining how to sell on amazon for beginners, mostly explaining FBA (Fulfilled By Amazon) Business model. In this guide, we will cover all the steps needed to sell on Amazon FBA (Fulfilled By Amazon). We will show you how to set up your account, take pictures of your products, list them for sale and manage orders.

Responses

Your email address will not be published. Required fields are marked *