Guide to setup and deploy Django web application with MongoDB in GCP (Google Cloud Platform)

Somanath Balakrishnan
Nov 5 · 3 min read

I hope this article help you to setup and deploy Django web application with MongoDB in GCP (Google Cloud Platform)

Create a VM in GCP

Create an account or use your existing google account to access, create a project and select “VM Instance” in “Compute Engine” of Compute section.

Select “Machine Type” based on your application requirement — I have selected “custom (2 vCPUs, 4 GB memory)” . In the “Boot disk” section, select OS and presistant disk size (I have selected “Ubuntu 18.04 LTS” and 10GB standard persistant disk to startwith).

Once VM successfully created, you may note down the IP (your IP is ephemeral and for static IP, you have to raise a request there and there is a cost associated with the same)

Enabling SSH access to your VM

For enabling ssh access from your local, add your local RSA key to VM in section “SSH Keys” of Metadata menu in “Compute Engine”. After adding your RSA key, you can access the VM from your local,

eg: ssh user_name@VM_IP

for copying files to your new VM, you can use scp command,

eg: scp /path/of/files user_name@VMIP:/path/of/remote/

Installing Python and Django

Install python 3.6.7

python3 — version

if python 3 not present, please install by below command,

sudo apt-get install python3.6

sudo apt-get install python3-setuptools

sudo apt-get update

Install pip3

pip3 — version

if pip3 not installed, please install by below command,

sudo apt install python3-pip

Install django

sudo pip3 install django

sudo pip3 install django — upgrade

Install and configure MongoDB

Install mongodb

sudo apt-get install mongodb

sudo service mongodb start

MongoDB security configuration

mongod — port 27017 — dbpath /var/lib/mongodb

Above command will start mongod service and then, we can open mongo console by giving below command,

mongo — port 27017

Create an admin user

use admin



user: “admin”,

pwd: “<<pwd here>>”,

roles: [ { role: “userAdminAnyDatabase”, db: “admin” }, “readWriteAnyDatabase” ]



Granting root role for user admin — this is essential for managing other users

db.grantRolesToUser(‘admin’, [{ role: ‘root’, db: ‘admin’ }])

Shutdown db to open in auth mode

db.adminCommand( { shutdown: 1 } )

Opening mongodb in auth enabled mode

sudo mongod — auth — port 27017 — dbpath /var/lib/mongodb –bind_ip_all

# — auth will enable mongo in auth mode and –bind_ip_all makes mongo host accessable from outside.

sudo mongo — port 27017 -u “admin” — authenticationDatabase “admin”

# it will prompt for admin password

use admin

Creating application user — you are going to use this user from your web app,



user: “user_name”,

pwd: “<<pwd>>”,

roles: [ { role: “readWrite”, db: “<<db name>>” }]



use <<app db name>>

# creating application user — you are going to use this user from your web app,



user: “user_name”,

pwd: “<<pwd>>”,

roles: [ { role: “readWrite”, db: “<<db name>>” }]



# create ubuntu user

adduser <<user_name here>>

# above command will prompt you to enter required details like name, password etc

# below command is to enable sudo access to the newly created user

sudo usermod -aG sudo <<user_name here>>

# below command is to switch user to newly created user session,

sudo su <<user_name here>>

# below command is to restore data from your local DB to GCP.

mongorestore — username admin — password <<admin_pwd_here>> — authenticationDatabase admin -d <<DB_name_to_import_data>> <<folder_name_where_data_exported>>

Run your Django application

Copy django code into server by scp or using filezilla etc. From your project root folder, run below command to start application

/usr/bin/python3 runserver

IP is for accessing application with the right IP of the server. You may specify * at ALLOWED_HOSTS in Django settings

Now, you can access your Django application by giving direct URL in your browser, ie: http://VM_IP:8000

Thanks for reading this article, I will cover Django application production deployment with SSL and Nginx in next article.

Somanath Balakrishnan

Written by

Technical Consultant @ Digitalcore Technologies, Kochi, India

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade