Deployment

Deployment of django application

blog image

In this tutorial we will learn various way you can deploy the django application such as deployment in cpanel,vps ,pythonanywhere and other many free sever.

1.Deploy The django app in cpanel

Before deploy any project , at first you need a project so firstly you need to setup the django project. During setup of django project following point need to be takecare.

Step 1:

i. make .env and .gitignore file and make the corsheaders settings and debug=False

ALLOWED_HOSTS = ["*"]
CORS_ALLOW_ALL_ORIGINS = True
DEBUG=False

ii.make requirements.txt file by running the following command

pip freeze > requirements.txt
iii. it better to make settings of static file so run command as follow
python manage.py collectstatic
iv.push you project in github repo, better to set the private visibility for the repo

Step-2

i.   Now time to setup the database for our project so for that goto the cpanel , login in into it and then search for database and go to MYSQL database.

ii.create a new database for your project with the appropriate name 

iii. create the user for the database, for that go to mysql user and then add new user with username,password and then create it.

iv.Then finally , add recently created user to the corresponding database and provide the privilages according to the requirements.

Step-3

i.Now time to setup the python project , for that go the dashboard of the capanel, search for the python setup , go through it and click on create new python application and then choose the python version and the path for our project . for example amritpanta.com.np/ecommerce. Then click in create button, it will automatically create a passenger_wsgi.py file on mention directory 

ii, It will create python project for us with virtual env as well. so copy virtualenv 

iii. Open terminal and paste the virtualenv for project , Now you can clone python project here

git clone <your python project repo link>

iv.After successfully cloning the project then you can setup the project here .Go to the file manager and the project directory that you have mention early when creating the python project. In that directory you 

can see the you project's all files,folder along with passenger_wsgi.py file ,public folder e.t.c

v.Now open passenger_wsgi.py file , write code as below and saved it

import os
import sys
from <inner_project_folder>.wsgi import application

 vi. open .env file (if not create it) and then setup all env that you have used in your project like database setting,email configuration and many more

 vii. Come back to the terminal and hits following commands

pip install -r requirements.txt
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py collectstatic
python manage.py runserver

viii.Then final go the python setup and the click on restart button , then your python application is successfully hosted on cpanel .

ix. some time you need to setu .htaccess file on your root directory as 

# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home2/namankha/blog.amritpanta.com.np"
PassengerBaseURI "/"
PassengerPython "/home2/namankha/virtualenv/blog.amritpanta.com.np/3.8/bin/python"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN
<IfModule Litespeed>
</IfModule>
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END

 

Note:

if you are using the github for cloning the porject instead of terminal then follow the steps

i. go to the cpanel search for the git control version and go through it , click on create button on it.

ii.setup the SSH Access for the project , for it go to the terminal and paste

ssh-keygen -t rsa -b 2048 -C "yourserverusername@yourserverdomain.com"

it will generate the ssh key for the domain and go to the git project repo settings and to the deploy key, paste the ssh key on 3.2 then clone git project (remember ssh link ), Repository Path and Repository Name , then click on create button to createe repo on cpanel 3.4 then go to the file manager then on repositories inside it you will see the repo of your git

iii. While making the python project , the repo directoty should be given as  expample : repositories/ ecommerce instead of  amritpanta.com.np/ecommerce

iv. After making all the setup related to migration ,superuser go to the project folder which is incase repositories/ecommerce this time , click on it and change the permission. to run project successfully


 

 

https://www.cloudwithdjango.com/adding-a-session-timeout-in-django/
https://www.cloudwithdjango.com/django-web-application-security-checklist/
https://infosec.mozilla.org/guidelines/web_security#content-security-policy
https://dev.to/arnopretorius/the-best-third-party-tools-to-utilize-for-django-deployment-59pc
https://www.ssltrust.com.au/ssl-tools/website-security-check
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security
https://developers.cloudflare.com/ssl/get-started
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security

 

 


About author

author image

Amrit Panta

Python developer, content writer



3 Comments

Amanda Martines 5 days ago

Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod Pinterest in do umami readymade swag. Selfies iPhone Kickstarter, drinking vinegar jean.

Reply

Baltej Singh 5 days ago

Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.

Reply

Marie Johnson 5 days ago

Kickstarter seitan retro. Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.

Reply

Leave a Reply

Scroll to Top