In this tutorial, I am going to walk you through how to deploy Python Django web app on a shared cPanel web hosting plan.
Login to your cPanel Account
Locate and Launch “Setup Python App”
Under SOFTWARE section you’ll find ‘Setup Python App’. To launch it just click once.
Create New Python Application
Now, locate the “Create Application” button and click it
Here you can select Python version, domain or sub domain name, and write your app directory name for your application, I have chosen Python 3.6.8, selected my sub domain name and given app root directory name, which can found from ‘File Manager” page.
Once, you are done with Python version, Application root, and Application URL, click create on the right top. “Create” can be any where on this page, on mine it’s on the right top.
View in the browser
Activate Python Virtual Environment
You just created your first Python Application in Cpanel. Now, let’s activate the python virtual environment.
Copy the underlined terminal command
Once you have copied the this command, go back to cPanel home page and look up for “TERMINAL” and launch it.
Paste the Terminal Command here
Let’s Install Django
To upgrade pip and install Django in this Python Environment I just ran following commands.
pip install --upgrade pip
pip install django==1.9.13
Remember you can install latest version of Django with ==1.9.13 flag, but this is what has worked on my cPanel without any error or issue.
When install and run the latest version of Django on my cPanel it does not find the latest version of SQLite on the server. And on the shared hosting plan I can not upgrade it.
Create Django Project
Let’s create Django project from cPanel Terminal. Type the following command
django-admin startproject project_name .
Don’t forget the period sign at the end
Locate App Root Directory
Edit/update /passenger_wsgi.py file with the following code
from project_name.wsgi import application
Save /passenger_wsgi.py file and close it
Edit/Update /project_name/settings.py file
Under settings.py update the ALLOWED_HOST field first
ALLOWED_HOSTS = ['best.tubemint.com', 'www.best.tubemint.com']
Under settings.py update static files section at very bottom of the page
STATIC_URL = '/static/' MEDIA_URL = '/media/' STATICFILES_DIRS = [BASE_DIR+"/assets",] STATIC_ROOT = '/home/eatawvtq/public_html/static' MEDIA_ROOT = '/home/eatawvtq/public_html/media'
Now, you can save and close this settings.py file.
Create Static Files’ Folders in the respective directories
- Create /assets/ folder in the project root directory.
- Create /static/ and /media/ folders in /public_html/ directory
Run the following command to sync the static files
python manage.py collectstatic