Google Cloud Platform offers a huge number of services and if your project needs integration with their services it might be a great idea to deploy your app on their platform to be closer to their API.
In this article you will see how how to deploy a Django project on GCP Virtual Machine.
If you are looking for tutorial for another cloud provider checkout out one of these:
- Deploy Django on Hetzner Cloud
- Deploy Django to Digital Ocean Droplet
- Deploy Django to AWS Lightsail
- Deploy Django to AWS EC2
- Deploy Django on Linode
- Deploy Django on Azure
Table of contents
- An application to deploy
- Appliku Account
- Google Cloud Platform Account
You need to have your app pushed to a git repository. You can use GitHub, GitLab or any other custom git repository.
For easiest deployment your application should have:
requirements.txtfile in the root of the repository with all dependecies needed to run the app
Procfilewith commands to run your app. Look at the example here: https://gitlab.com/speedpycom/speedpycom-backend/-/blob/main/Procfile
- respect to environment variables if needed. e.g.
DATABASE_URLfor database credentials.
- DEBUG mode turned off for security reasons.
If you don't already have an Appliku Account create it by going here: https://app.appliku.com/
Click on "Add a server"
Pick a Custom Server:
Select this key, we will need it at server creation step.
Google Cloud Platform Account
If you don't already have a GCP account sign up here
After you have signed up and created a project go to VM Instances: https://console.cloud.google.com/compute/instances
Click on "Create Instance"
Give instance a name or leave a default one.
Select a region. It can be closer to you (useful during development) or closer to your target audience.
Select a machine time. For the purpose of this tutorial we will pick the smallest one 1 shared core and 1GB RAM.
Scroll down to the "Boot Disk" section and click "Change"
Select Ubuntu, Ubuntu 20.04 LTS for x86/64 architecture, set disk size to 30 GB.
In the "Firewall" section check both Allow HTTP and HTTPS traffic.
In "Advanced Options" -> "Security" -> "VM Access" -> "Add manually generated SSH Keys" click "Add Item"
In the SSH Key field you need to paste the key offered to you in Appliku Dashboard, and add space + "gcpuser"
Scroll down and click "Create"
You will be taken to the list of all your instances where you will see a spinner indicating that your instance is being created.
Until it becomes green:
Now we need to allow access via SSH.
Click on "Setup up firewall rules"
Click on "Create firewall rule"
Here are the fields you need to set or change:
- Name: "ssh"
- Targets: "All Instances in the network"
- Source IPv4 ranges: 0.0.0.0/0
- Protocol and ports: Specified protocols and ports, TCP, 22
Scroll down and click "Create"
You will see that your rule has been created.
Let's go back to our VM instances.
Open the left side bar and go to Computer Engine -> VM Instances
Click on your VM instance.
A server details page will open. Scroll down and in the table "Network interfaces" find "External IP address". Copy it and switch back to Appliku Dashboard
Paste the IP address into the "IP" field, username should be "gcpuser" (remember what we added at the end of our SSH Key?), click "Check credentials and add server"
Your server has been added to your Appliku Team and setup will begin shortly.
It may take up to 10 minutes while Appliku sets up all packages and configures the server.
Wait until the field "Server Setup" says "Finished" and Resources gauges become alive.
If it says Error then you should go to the Setup Logs tab and see for errors.
Adding an application
Click on the "Applications" menu link
You will get to the Git Provider selection.
Creating application from GitHub
Select the "Setup Credentials" for GitHub.
You need to get a personal token from GitHub.
Go to this link to create a token: https://github.com/settings/tokens
Click on Generate new token -> Generate new token(Classic) Fill in the note, select expiration date, check the "repo" checkbox and "user:email"
And click "Generate token".
Copy the token from the next page.
Go back to Appliku dashboard and paste the GitHub Token, and click Test Credentials and Save
Status must change to "Active".
Click on the logo in top left corner to get back to your team and go to Applications again and create an app, select GitHub.
Fill the form with the app name, select the repository with your application, branch and select the server, click on "Create Application".
You will see your app has been created, but not yet deployed.
Creating application from GitLab
Alternatively if you use GitLab you should add credentials for GitLab and create an app from GitLab.
Go to Applications menu link, Click "Add Application" and Setup credentials for GitLab.
You need to create a Personal Access Token here: https://gitlab.com/-/profile/personal_access_tokens
Give the key a name, set or clear expiration date, select "api" checkbox and click "Create Personal Access token"
Copy the token and paste it in Appliku dashboard.
After you click "Test Credentials and Save" the Status should turn green and say "Active".
Click on the logo to go back to your team, go to Applications, Add Application and select GitLab
Give your application a name, pick repository and the branch, select the server and click "Create Application"
Setup the app
If your application needs a database, which Django apps need most of the time, you can click on the "Manage" in databases block and "Create Database"
Select a database type you need and your server to deploy the DB to, click "Create database"
Your database will start deploying, wait for the deployment status to become "deployed".
Go back to your app
You can see your database is in the list.
Environment variables and build settings
Click on the "Settings" link next to the application name.
Go to the "Environment Variables" tab
You can see that the
DATABASE_URL is already added automatically from the database you created.
If you have a
.env file you can use "Import from .env" button to paste variables.
Or click on "Add" button and add variables one by one.
Usually for Django app we need to add
Appliku provides a default host name for your application which is YOUR_APP_NAME+ '.applikuapp.com'
After you finished adding your variables click on "Save and Deploy"
You will see that the deployment started.
Go back to the application overview.
The "Deploy Now" button is grey because deployment is in progress and in the "Latest deployment" Block you can see the currently running deployment.
Click on "Deployment #1"
You can see the deployment log. Scroll down to see the most recent output.
After it finishes and reports the "exit_code: 0", which is success go back to your application overview
Click on "View Application"
It will open your application in a new window!