Deploying a website online always become a cramp-breaking task, as we need to prepare web-server code for production environment, if we talk about a Python Django project we need to create a list of libraries that we have used in our Python Django project, need to edit configuration files and in some cases there is need for changing urls also. In the post we will see step by step guide for hosting Django project on Google cloud console i.e Google App Engine, whole process will take at max 30 minutes to set up. So, let’s start:

Django Project Overview : 

Here we continue deploying our Python Django with Google Firebase series Django Project in which we have create a Employee Daily Progress Reporting System which uses different-different Firebase Functionalities such as : Firebase Authentication for Login the user, Firebase Database for ReadWrite operations like storing reports data create by user, Firebase storage for uploading images, docx to be uploaded by user & at last but not the least Firebase cloud messaging : For sending automated push notification to Admin Android App once report created by user.
Steps below follows the video to help :

Setup Django Project for Hosting:

First step is make a list of libraries, installed while creating your Django Project. Here is bonus tip, every-time starting your Python Django Project create & activate virtual environment this will isolate installed libraries.

  •  pip freeze >req.txt

Above command create file named req.txt which includes the list of libraries used in our project. Now just paste this file where in Project directory and create a compressed tar file .

  • tar -cvzf cpanel.tar.gz cpanel
In next step we have to Setup Google Cloud Console and Vm Instance done in previous post.

Setup Google App Engine Vm instance:

Go to Google Cloud Console navigate to compute engine -> Vm instance -> create instance  :
Add relevant information make sure in firewall tab :
  • Allow HTTP traffic
  • Allow HTTPS traffic
that’s it click on create button and run vm instance and get SSH access.

Now install pip3 in vm instance typing following command in ssh terminal –

  • sudo apt-get install python3-pip

Setup SCP ssh key :

Now, we need to transfer our Django project from workstation to Vm instance for which we are having diffrent ways like using Github | Bitbucket, SCP & using Google cloud sdk. Using SCP is one of my favourite one but there issue of version control also for which you can intialize git.
  Generate a ssh key
  • ssh-keygen -t rsa -f ~/.ssh/[KEY_FILENAME] -C [USERNAME]
  • Example:  ssh-keygen -t rsa -f ~/.ssh/key -C Linux
  • cat ~/.ssh/

above commands will create and show a public ssh key that you have to add in Google cloud console.

Go on Google Cloud dashboard and navigate to compute engine -> metadata -> ssh keys -> edit

here add ssh key you got in previous step .

Using scp command for transferring  files :

  • scp -i ~/.ssh/my-ssh-key [LOCAL_FILE_PATH] [USERNAME]@[IP_ADDRESS]:~
above command transfer Django project from WorkstationHost machine to Vm instance.

Setup Django Project on Vm instance :

Now, Django project is on vm instance let’s extract the tar.gz file and live our django Web-App.

  • tar -xvzf cpanel.tar.gz
  • cd cpanel
as we have already installed pip3 in previous steps, navigate to req.txt file created for getting list of libraries used for creating Django project. Run the below command to install all libraries in one go

  • sudo pip install -r req.txt

So, here comes our final step for which we are waiting let’s run our Django project use below command and access your live website on vm instance public I.P. address.

For testing run Django on, as django server will stop if we close the terminal we need to run it as background process
  • sudo python3 runserver

There are too many ways for running django in background, i personally prefer Nohup command for running  project in background.

  • sudo nohup python3 runserver

there might be conditions you need to kill nohup process for server upgrade use kill command.

  • ps -ef | grep python3 runserver
  • killl process id.

Categorized in: