Deploy Django 4.0 on Shared Hosting - Python Fusion (2024)

In this post, I will present a step by step procedure to deploy a Django project to shared hosting. This is one of the options that you can avail to deploy your Django project. The advantage of shared hosting is that it is cheap. The disadvantage is that you might not be able to deploy some advanced projects because you can not install a software on your shared host. You can only install python modules via pip and some modules might not function correctly on a shared hosting. If your project is a basic website or a blog, it should work just fine. I have also used shared hosting to deploy APIs made in Django using the Django Rest Framework.

At the time of updating this article, the latest version of Django is Django 4.0.4 and by using the method that I am explaining in this post, it is possible to deploy a Django 4.0.4 project on shared hosting with MySQL database.

I have the Stellar Plus Plan of the NameCheap Shared Hosting which I will be using in this post.

Deploy Django 4.0 on Shared Hosting - Python Fusion (1)

I quickly created a Django project for giving this demo. The app has quotes in the database and it just displays them. You can download the project from github. I recommend you to download this project and practice deploying it before you deploy your actual project.

Parts of the Tutorial

  • Create a Python App
  • Setup the Database
  • Upload your project
  • Complete configuration from the Terminal
  • Transfer your Database (Optional)
  • FAQs
  • Useful Links

Note: You do not have to delete the migrations of your project to deploy it using Cpanel unless there is a problem with the migrations.

Create a Python App

Log in to CPanel and open Setup Python App.

Deploy Django 4.0 on Shared Hosting - Python Fusion (2)

Click on Create Application

  • Select Python Version 3.9.12
  • Application Root is the directory where you will place the code files of your Django project. Make sure that it is a different folder than your domain root.
  • Application URL is the domain or subdomain where your app will run
  • In the Application Startup File, type passenger_wsgi.py
  • In the Application Entry Point, type application

After setting all these, click on the Create button.

Deploy Django 4.0 on Shared Hosting - Python Fusion (4)

Setup the Database

Open MySQL Databases in Cpanel

Deploy Django 4.0 on Shared Hosting - Python Fusion (5)

Create a new Database and note the database name. We will need it later.

Deploy Django 4.0 on Shared Hosting - Python Fusion (6)

Create a new MySQL user and note the username and password. We will need it later.

Deploy Django 4.0 on Shared Hosting - Python Fusion (7)

Add the new user to the new Database

Deploy Django 4.0 on Shared Hosting - Python Fusion (8)

Grant all the permissions to the user and select Make Changes

Deploy Django 4.0 on Shared Hosting - Python Fusion (9)
Deploy Django 4.0 on Shared Hosting - Python Fusion (10)

Upload your project

Open the File Manager and go to the Application root you specified in the part 1

Deploy Django 4.0 on Shared Hosting - Python Fusion (11)

Zip your project. Upload it to this folder and extract the zip file. Your project files should be in the same folder as the passenger_wsgi.py file. Make sure that manage.py and passenger_wsgi.py are in the same folder.

Edit the passenger_wsgi.py file.

Deploy Django 4.0 on Shared Hosting - Python Fusion (12)

Delete everything from this file and add the following code:

from base.wsgi import application

Where base is your project folder. It is the same folder that contains your settings.py file. It will be different if you are not using the test project that I provided. You can locate your wsgi.py file and import application from it.

Now edit your settings.py

Deploy Django 4.0 on Shared Hosting - Python Fusion (13)

Add your domain to the ALLOWED_HOSTS list. If there is a www version of your domain, add that too. Do not use http:// to https://

ALLOWED_HOSTS = ['django.umer.link']
Deploy Django 4.0 on Shared Hosting - Python Fusion (14)

In the DATABASES dictionary, modify the default database.

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

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

Deploy Django 4.0 on Shared Hosting - Python Fusion (15)

Now go to the end of the file, modify STATIC_URL and add the STATIC_ROOT

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

Replace the username with your CPanel username. The domainroot will be public_html if you are deploying on the main domain of your hosting account. If you are deploying on a subdomain or an add on domain, it will be name of the addon domain or the subdomain.

Deploy Django 4.0 on Shared Hosting - Python Fusion (16)

Now edit the __init__.py file

Deploy Django 4.0 on Shared Hosting - Python Fusion (17)

Add the following code

import pymysqlpymysql.install_as_MySQLdb()

Complete configuration from the Terminal

Open the Terminal in your CPanel. If you can not find the terminal, go to Manage Shell and Enable SSH Access. You can also do these steps using SSH but using the terminal is easy.

Deploy Django 4.0 on Shared Hosting - Python Fusion (18)

Copy the command to enter the virtual environment from your python app.

Deploy Django 4.0 on Shared Hosting - Python Fusion (19)

Run the copied command in the terminal and press enter to enter the virtual environment,

Deploy Django 4.0 on Shared Hosting - Python Fusion (20)

Install Django 4.0.4 by running the following command.

pip install django==4.0.4

pymysql is required for using the MySQL database. Install it using pip. Here you will install any other modules required by your Django app.

pip install pymysql

If your migrations are not complete, then make migrations.

python manage.py makemigrations

Run migrate to create tables in the database.

python manage.py migrate

Run collectstatic to move the static files to the static root folder specified in settings.

python manage.py collectstatic

Run createsuperuser to ass a user to the Django admin panel

python manage.py createsuperuser

Finally, restart the python app.

Deploy Django 4.0 on Shared Hosting - Python Fusion (21)

Your Django app is deployed successfully. Open the url of your app to see if it is working fine. If you see the phusion passenger error page, you can find the error in the stderr.log file for debugging.

Deploy Django 4.0 on Shared Hosting - Python Fusion (22)

Transfer your Database (Optional)

This part of the tutorial will be helpful if you have the app running locally or somewhere else and you want to transfer the data from the database to the one in your shared hosting. Here I will assume that you have the Django app on your computer and you want to transfer the data from the local database to the database on your shared hosting.

Run the following command on your computer to export all the data from the database to a json file.

python manage.py dumpdata>data.json

Now upload this file to the app folder in your shared hosting.

Deploy Django 4.0 on Shared Hosting - Python Fusion (23)

Open the terminal in the CPanel and enter the virtual environment (steps are mentioned in the previous part of the tutorial).

First, you will clear the database by running the following command:

python manage.py flush

Then run the following command to load the data from the data.json file into the database.

python manage.py loaddata data.json

Now if you open the url, you will see that the data has been added to the database.

Deploy Django 4.0 on Shared Hosting - Python Fusion (24)

FAQs

Saving an imagefield gives a 404 page

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

Useful Links

  • Buy NameCheap Shared Hosting
  • Use PostgreSQL database with Django on shared hosting
  • Deal with media files and static files in Django on shared hosting

Post Views:13,101

Deploy Django 4.0 on Shared Hosting - Python Fusion (2024)
Top Articles
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 6648

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.