marko devcic

Software Engineer
  • github:
    deva666
  • email:
    madevcic {at} gmail.com
Deploying Django apps on Google App Engine

Deploying Django apps on Google App Engine

Posted on 02/28/2021

After trying out a lot of different hosting providers, I finally settled on Google App Engine to host this site. Deploying apps is fast and easy with Google Cloud SDK, and they have a free tier for the Standard Environment. F Class instances have a generous 28h per day free quota. Which means if you have a low to medium traffic site that you'll probbably run it month to month free of charge. Also, you get a free SSL certificate.

This site is built with Python and Django, and uses SQLite as a database. Let's see what are the steps to publish a Django app on GAE. I'm assuming that you have a working Django App, Google Cloud SDK installed and already have a project created in Google Cloud Console. If you generated your project with django-admin startproject command, then you probably have a root folder with manage.py file, a subfolder with settings.py (I'll call this subfolder my_site) and another subfolder, your app folder where your views and models are (I'll call that one my_app).

1. app.yaml

First step is to add a app.yaml file with following content to the root of your project.

# [START django_app]
runtime: python39
handlers:
# Tel GAE where are Django static assets located
- url: /static
  static_dir: static/
# Route everything else to the main app
- url: /.*
  script: auto
# [END django_app]

This tells GAE that we'll be running a Python app. We're also configuring a handler to server static assets of our app. By the way, your settings.py should already have STATIC_URL and STATIC_ROOT properties.

STATIC_URL = '/static/'
STATIC_ROOT = 'static'

2. main file

We need to tell GAE how to run our Django app. Add a main.py file to the root folder and add the following.

from my_site.wsgi import application
app = application

3. requirements.txt

Next, we need to install all our projects dependencies during deployment. GAE will do this for us if it finds requirements.txt in the root folder. If you are using venv (and you should) just run pip freeze > requirements.txt.

4. .gcloudignore

This step is optional, if you want to exclude some files or folders from being deployed with your app, add a .gcloudignore file to the root folder. There's no need to upload Git, virtual env or your IDE config files.

.gcloudignore
.git
.gitignore

# Python pycache:
__pycache__/
# Ignored by the build system
/setup.cfg
env/
venv/
.vscode

5. deploy

With all this in place, we can deploy our app. Executing gcloud app deploy from the terminal where your app.yaml is will do the trick.

Optional things to consider

  • Database setup

    Depending on your database, you might need to setup up your DB connection.
    I am using SQLite in read only mode, so I can deploy it with rest of my code/files. Since it is read only I don't have to worry if GAE decides to scale up the number of instances.

  • Static files serving

    Moving your Static files from your instance will lessen the load on it and reduce the overall instance use hours. You get 5 GB of free storage on Google Cloud Storage and with Django Storages it is easy to set up.