How To Deploy Django App on Linux Shared Hosting Using cPanel

In this post we will learn step by step how to deploy django on linux shared hosting using cpanel. For dajngo application deployment it’s probably not the best thing to run Django application on cPanel because of the

  • You Can’t Install Custom Library which you need to compile.
  • It is not very reliable and does not perform very well on cPanel.

But if you want to low costing facility in this case Linux shared hosting has more advantages.

What we need to host Django App

  • Linux Hosting
  • Access to CPanel
  • Support for python apps
  • Command-Line access to the server

Most of hosting are Linux shared hosting and all cpanle interface almost same. Check you hosting, is support for python apps ? To check in cpanel search for Setup Python App and search for Terminal to ensure that you have command line access.

Let’s start deploying step by step

Step: 1 Set Up Python App

First Open Setup Python App from your Cpanel , then click on Create Application button

  • Set the Python Version to 3.7.3
  • 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

When you are done click on create button. Look at the screenshot you will see the settings.

Step 2: Install Django

your setup python application almost done now you will see a page as like as the screenshot below. You have to copy the command to enter on virtual environment

Now open your online terminal from cpanel and enter the command by right click and paste them then hit enter. Your python virtual environment will be activate. In my case command is

source /home2/riajulme/virtualenv/srms/3.7/bin/activate && cd /home2/riajulme/srms

Now you have to install django Note install the stable version of django dont’ install latest version. Install django by running following command

pip install django==2.2

after installing django successfully install others module if any module need for your project.

Step 3: Create Django Project

On your terminal in cpanel type the command for creating new django project in my case the command is

django-admin startproject mysite ~/srms

where mysite is project name and srms at the end with the directory name where you set up your python app. . Make sure that you have replace mysite into your project name and replace srms into your directory name. Now got to application folder in filemanager where you will see manage.py file. Edit the passenger_wsgi.py file. Deleter everything from the file and replace with this line

from mysite.wsgi import application

where mysite is your application name replace mystie into your application name and click save changes button. Now open app folder and edit 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 “srms.riajul.me”. 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 got to Setup Python App and restart your application

Now if open project url you will see a welcome page from django.

Step 4 Create A Database

If your app isn’t so big you can Sqlite database which is builtin in django. But for big app you need to setup mysql db. It is also possible to use PostgreSQL database but most of the shared hosting provide only MySql Database so now i will setup MySql DB.

To Create Database go to MySql Database in your cPanel

Click to Create Database

Create Database User

Add Add the user to the database Add

Now go to terminal again and install pymysql

pip install pymysql

then edit the settings.py and replace the default database code with this code below

'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'riajulme_srms',
        'USER': 'riajulme_srms',
        'PASSWORD': 'T.@2.qeEZFL1',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            # # utf8_general_ci
            'charset': 'utf8mb4',
           'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
           'use_unicode': True,
        }
    }

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

Step 5: Setup Static Files

Edit your settings.py file and add the following lines at the end.

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Edit your urls.py and add the following lines

from django.conf import settings
from django.urls import path, include, re_path
from django.conf.urls.static import static
from django.views.static import serve

urlpatterns += [
    re_path(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}), 
    re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}), 
]

OK !! Now Our Django App deployment is almost done.!! Now go to “Setup Python App” in CPanel and Restart the app.

Now test the app

Your Django deployment is almost done now go to http://yourappurl/ and check is everything OK

After reading this tutorial next time you not need to read this for explanation.

Now you can deploy you app in 5 minute by following below

Setup Python App

Application startup file: passenger_wsgi.py

Application entry point: application

Create A Database And Add A User To It

Edit passenger_wsgi.py from app.wsgi import application

Edit settings.py

Add your URL to ALLOWED_HOSTS

Replace the default database with

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

Add the following lines top of urls.py

from django.contrib import admin
admin.autodiscover()

Edit __init__.py add the follwing code

import pymysql
pymysql.install_as_MySQLdb()

Leave a Reply

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