Setting Up and Deploying a Django Web Application with MongoDB on GCP

0
4554

This article is a guide to setting up and deploying a Django Web application with MongoDB in GCP (Google Cloud Platform). Django is a high level Python framework which enables rapid development of applications with a clean, pragmatic design. MongoDB is a powerful database often associated with Web applications.

Let us get to work straight away. Begin by going through the following steps in sequence.

Creating a VM in GCP
Create an account or use your existing Google account to access cloud.google.com, then create a project and select ‘VM Instance’ in the ‘Compute Engine’ of the ‘Compute’ section.

Select ‘Machine Type’ based on your application requirement – I have selected ‘Custom’ (two vCPUs and 4GB memory). In the ‘Boot disk’ section, select OS and persistent disk size (I have selected ‘Ubuntu 18.04 LTS’ and 10GB standard persistent disk to start with).

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

Enabling SSH access to your VM
In order to enable SSH access from your local machine, add your local RSA key to the VM in the section ‘SSH Keys’ of the metadata menu in ‘Compute Engine’. After adding your RSA key, you can access the VM from your local machine using the following command:

e.g.: ssh user_name@VM_IP

To copy files to your new VM, you can use the scp command, as shown below:

e.g.: scp /path/of/files user_name@VMIP:/path/of/remote/

Installing Python

To check if Python 3 is already present, run the command given below:

python3 --version

If Python 3 is not present, install it using the commands given below:

sudo apt-get install python3.6
sudo apt-get install python3-setuptools
sudo apt-get update

Installing pip3
Check if pip3 is already installed by running the command given below:

pip3 --version

If it is not present, then install it by using the following command:

sudo apt install python3-pip

Installing Django

To install Django, use the following commands:

sudo pip3 install django
sudo pip3 install django --upgrade

Installing and configuring MongoDB

Install MongoDB by using the following code:

sudo apt-get install mongodb
sudo service mongodb start

MongoDB security configuration is:

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

The command given above will start the mongodb service and then you can open the Mongo console by giving the following command:

mongo --port 27017

Create an admin user as follows:

use admin
db.createUser(
{
user: "admin",
pwd: "<<pwd here>>",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
);

Granting root privileges to the user admin

This is essential for managing other users. To do so, use the following code:

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

Shut down db to open in auth mode as follows:

db.adminCommand( { shutdown: 1 } )

Opening MongoDB in auth-enabled mode
To do so, use the code given below:

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 accessible 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:

db.createUser(
{
user: "appuser",
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,
db.createUser(

{
user: "appuser",
pwd: "<<pwd>>",
roles: [ { role: "readWrite", db: "<<db name>>" }]
}

);

The command given below restores 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>>

Creating Ubuntu user

adduser <<user_name here>>

The command given above will prompt you to enter required details like name, password, etc.
The command given below is to enable sudo access to the newly created user:

sudo usermod -aG sudo <<user_name here>>

The command given below switches the user to a newly created user session:

sudo su <<user_name here>>

You can use this Ubuntu user for storing your application files, etc, and also for managing application deployments.

Running your Django application
Copy Django code into the server by scp or by using Filezilla, etc. From your project root folder, run the command given below to start the application:

/usr/bin/python3 manage.py runserver 0.0.0.0:8000

IP 0.0.0.0 is for accessing application with the right IP of the server. You may specify * at ALLOWED_HOSTS in Django settings.
You can now access your Django application by directly typing the URL in your browser, i.e., http://VM_IP:8000.

The next article in this series will cover Django application production deployment with Supervisor, SSL and Nginx.

LEAVE A REPLY

Please enter your comment!
Please enter your name here