Set up Django on a Linux VM with Google Cloud Platform
Tags: cloud, googlecloud, django, linux
Published: 2023-02-18 14:05:42

As a senior in my BSCS program I've been working on my Final Year Project since the last several months. Early on, I decided to develop my project as a client-server web-application, and since the majority of the backend code was in Python, it was a pretty easy choice for me to choose Django as the web framework. Having the backend all be in one language streamlines a lot of things, but that isn't the point of this post.

So coming to the actual point: Up until now I've been hosting django on my local machine, and while that's worked fine until now, I'll eventually have to move on to using a _real server_. 

To this end, I decided to experiment by setting up a basic Django VM using Google Cloud Platform, and this post is going to be a documentation of that process (for both my own future reference and for anyone else who might be interested in doing something similar).

## 1. Creating a Google Cloud Platform Account
For now, I went with the free trial handed out by GCP. While you do need to setup a credit/debit card, you won't be charged anything initially and will be given 300$ in credits for the first 90 days.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqcrvpubnue5l6axp0mg.png)

## 2. Enabling the Google Compute API
Next up we want to enable the Google Engine API. On the left-side panel hover over the Compute Engine section, and then select **VM instances**. 
You'll be taken to a page that will ask you to enable the Compute API.
**Enable the computation!**
This might take a minute or two, but you'll be notified by the little bell icon on the top right once it's ready.

## 3. Creating the VM Instance
Now we're reading to create our VM. Once again, click on Compute Engine on your left, select VM instances, and you'll be taken to a page where you can configure your machine. Everything here is pretty straight forward, but there's a few things I want to focus on.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6e69h31smggyv8oo9g2v.png)

Here we want to be sure of two things:

1. The boot disk is selected as a Linux image (I'll be using Ubuntu).
2. The 'Allow HTTPS traffic' box is checked.

While we could always add in HTTPS traffic rules later, checking this box here simplifies the process from the get-go. 

## 4. Configuring SSH Keys
Quick refresher: SSH is used to connect securely to a shell running on a remote machine. There's two Keys, a public one a private one. Since I'm working on Windows, I'll be using PuTTy to generate my keys.

PuTTy download link: https://www.puttygen.com/

Open up PuTTYgen after installation, choose an encryption algorithm (I'm going with RSA), decide on a Key Comment, and click generate. You should get something like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0uaue6zlx80ncl9a70s.png)
Save the private key somewhere on your local PC, and copy the public key generated in the box on top onto your clipboard. 

Now open up the Google Cloud Console again. This time, select Metadata from the left hand-side panel. Go to the SSH keys tab, and add your public key here. All the VMs under the current project will inherit these keys.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmwtxaeh0b58so6u6cr9.png)

Alright we're done with the setup! Now we just have to connect to our VM remotely. Now open up PuTTy (not PuTTygen). From here, enter the External IP of your VM instance (if you're not sure what the IP is you can look it up from the Google Cloud Console). 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dnto1y1r40eay13pfbt.png)



From the left hand-side panel in PuTTY, expand SSH, expand Auth, select credentials, and select your previously generated private key. Now click open. When asked to 'login as', choose what you wrote down as a Key Comment earlier.

We've successfully connected to our VM using SSH.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6lhuhpdg9gfidc9cro3.png)

## 5. Modifying Firewall rules
Remember what I said about firewall rules earlier when creating the VM instance? Well we're mostly setup already and only have to add one extra rule for Django. 

From Cloud Console, go to the VPC section and then select 'Firewall' from the left hand-side. Now click on the Create Firewall Rule button. Here are the settings I used:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4yfcn7qbmgtdy8audqk.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuc6vjtm2bywcckayupm.png)
In particular, I set the IP range to 0.0.0.0/0 (to allow all public IPs to connect to the VM), and I specified port 8000 as open (since that is what Django uses by default).

## 6. Setting up Django
Now all that's left to do is download and install django on our VM. On the terminal, run the following commands to install (if you're wondering about python, it came pre-installed with the Ubuntu image):

`sudo apt install python-django
sudo apt install python-django-common`

Now we create a new django project called 'mysite' using the following command:

`sudo django-admin startproject mysite`

Just one more thing left to do before we run our server. We have to add the IP of our VM to the ALLOWED_HOST list in our django settings. So go into the mysite folder (inside the mysite root directory), open 'settings.py' using nano (command: sudo nano settings.py), and add the external IP of the VM (in single quotes) to the ALLOWED_HOSTS list.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nyf011deg6078jpp83m.png)

Now go to the root of the 'mysite' directory, and start the server using the following command:

`sudo python manage.py runserver 0.0.0.0:8000`

Now let's confirm if our server is working. From your local machine's browser, enter the external IP and port of your VM instance to connect to the django server.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ytcwz77slbzioemhds3.png)

All done!