Beginners Guide: Deploy Django To Google Cloud | OKR Management Project — Part 5

yongjoon
6 min readOct 15, 2023

--

Huge thanks for visiting my blog 🙌 welcome~! This is part 5 series of my project posts. I created a simple OKR management website. In this post, I will explain my journey of deploying the Django Rest Framework application to a Google Cloud server.

I deployed the Django on Google Compute Engine using Nginx and Git. I didn’t use advanced technologies like Google App Engine or Docker because I wanted to deploy it simply. I can’t say this is the best way to do it, but I assure you it is easier to understand.

Why Google Cloud?

I just want to try Google Cloud this time because I have used AWS before. AWS was great. I can find many references and books. However, using AWS was expensive, even though my project was tiny. I spent almost 30~40$ / month. I hope Google Cloud charges me less 😅

You can use the free tier in Google Cloud when I’m writing this blog. You can gain a 90-day $300 free trial. Check out the official docs here.

What Cloud Product Did I Use?

I chose Google Compute Engine. It is the most simple server in Google Cloud. I saw many tutorials that deploy Django using Google App Engine using Docker. But I just don’t want to care what the App Engine or Docker is this time. Why don’t we deploy it simply to a server?

Tips For Using Google Cloud

I won’t show you how to create resources and manage them. Instead, I give you how to find tutorials in Google Cloud. Let’s say you want to create a Compute Engine but don’t know how to. Google Cloud offers excellent interactive tutorials for that. You can find tutorials like the one below.

Click The Search Bar
Search: Compute Engine Quickstart
Enjoy Interactive Tutorials

I created a Compute Engine and a MySQL instance following interactive tutorials. It was better than watching YouTube videos.

Prepare Domain Name GoDaddy X Google Cloud

I bought a domain in GoDaddy. I needed to customize the Name Servers of the GoDaddy domain to Google’s Name Servers. Why? To replace the IP address of my domain with Google Cloud’s VM instance IP, Google Cloud needs to manage my domain.

Let’s see the Google Name Servers. You need to create a Zone in Cloud DNS beforehand. Then you can find Name Servers. Check out below.

You Can See The Created Zone
Click Row With NS Type
You Can Find Google Name Servers Here

Copy and Paste these Name Servers to GoDaddy like the below.

Visit GoDaddy And Click The Manage Button Of Your Domain
Click Manage DNS
Click The Nameservers Tab And The Change Nameservers Button

I created app.okr-manager.com for the web app domain and api.okr-manager.com for my Django domain in Google Cloud DNS Zone.

How To Access VM Instances From Local Machine?

I use gcloud CLI. Please follow the instructions here to install Google CLI. I needed to execute an additional command after following the steps in the link. Read here to understand what the Application Default Credentials are. I understand that gcloud manages credentials for us.

Execute For Credentials

Now, I can access my compute engine instance.

Use gcloud To Connect Compute Engine Instance

Did you forget the gcloud command? Don’t worry. You can find the access methods in the VM instances dashboard.

How To See Connection Method
Copy gcloud Command

When Can I Deploy The Server?

Please be patient. We’re almost there. I need to install some stuff on the Compute Engine instance.

  1. Install Git
  2. Install Nginx
  3. Configure SSL
  4. Configure Nginx Conf For Reverse Proxying: 443 -> localhost:8000
  5. Install Mysql Dev Header Package

You can easily find the instructions above, so let me just cover how I did No 3. I used the certbot for creating an SSL certificate and configured it to the Nginx conf file automatically. Here is the reference.

sudo apt update
sudo apt upgrade -y
sudo apt install certbot python3-certbot-nginx
Change example.com to your domain.

After the above, certbot automatically changes the Nginx configuration file, enabling an SSL connection.

What Is Nginx, and Why Do I Need It?

Nginx is a reverse proxy. It receives all the requests from the internet and proxies them to the Django Server. Nginx handles SSL handshake and proxying the request as HTTP to Django Server because it doesn’t need to use HTTPS between Django.

Nginx protects my server from direct exposure to the internet and adds an extra layer of security. I gave the SSL connection role to the Nginx.

The Position Of Nginx

So How To Deploy Django?

It’s time to deploy Django 😃 First, I needed to pull my code from Github. You can use the HTTPS method for pulling, but I used the SSH method. To use SSH, you must create an SSH key in the server and enroll it in the GitHub Setting. See here.

I created a venv and installed dependencies from the requirements.txt file. I faced some errors while doing this, and It turns out I must install the Mysql Dev Header package for using MySQL dependency.

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config

Don’t forget that you must have a Real DB connection configuration on your Django App. I added the below line in the settings.py (* is mask. I don’t want to open my DB information 😃)

DJANGO_ENV = os.environ.get('DJANGO_ENV', 'beta')

if DJANGO_ENV == 'real':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'okr-management-database',
'USER': '*',
'PASSWORD': '*',
'HOST': '*',
'PORT': '3306'
}
}
]

I’m ready~! I run Django with the below command.

Run Django Application!

I found many people use the Guicorn for deploying Django. It is a web server that manages multiple worker processes to execute Python code. Since I created a simple web application, I didn’t use it this time, but maybe next time 👍. For those who are interested in Gunicorn X Django, check out here.

Summary

System Architecture Overview
  • Google Cloud’s Interactive Tutorials are fantastic.
  • Lots of work to set up the server.
  • I will try Gunicorn the next time.

--

--

yongjoon

Hi, I'm yongjoon, a backend developer passionate about creating content and solving tech challenges. Empowering devs to learn and grow!