Cloud Ace Blog

Aug 12, 2019 | GCP Database

by Shivam Kohli


How to deploy a Django Application on Google Cloud using Apache2 and mod_wsgi



Django is an open-source web framework for python. It can be used for getting your python based application deployed easily. Any application in production requires a web server that is powerful and flexible enough to scale with demand.

In this tutorial, we will learn to deploy a Django application on the google cloud with the use of apcahe2 and mod_wsgi. We will also learn how to set up a virtual environment, setup the apache server that handles the client request and configures the mod_wsgi Apache file to communicate with the Django WSGI file. This guide is users that are using Ubuntu.

So let’s get started.


Installing the required packages on your ubuntu server

You need to install the following packages on your server.

If your application is using python2

sudo apt-get update
sudo apt-get install python-pip apache2 libapache2-mod-wsgi

If your application is using python3

sudo apt-get update
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3

Downloading the module for the virtual environment

If you are using python2

sudo pip install virtualenv
virtualenv <envname>
cd <projectname>
source <envname>/bin/activate

If you are using python3

sudo pip3 install virtualenv
cd <projectname>
virtualenv <envname> -p python3
source <envname>/bin/activate

Your terminal prompt would change indicating the name of your environment before. It would look somewhat like this (<envname>)user@host:~/<projectname>$

We need to install Django that is the one main requirements. To install Django use the following command

pip install django <<version>>

You can clone a project directly in your working directory or you can start a new project on the server itself. To start a new Django project please follow this tutorial which explains how you can get started with Django

After cloning your project you have to set the Ip of your server in the file of the Django project.

In your file, please add the following:

ALLOWED_HOSTS = [“<server_domain_ip>”]

To configure the static files in your application, we will need to add the path of the static folder in the file. You need to add the following in your file.

At the bottom of your file, there will be a STATIC_URL after that line add the STATIC_ROOT as shown below

STATIC_URL = ‘/static/’

STATIC_ROOT = os.path.join(BASE_DIR, ‘static/’)

After successfully completing the above-mentioned steps, we will now set up the database. It is assumed that the SQLite database is being used currently. You can also use PostgreSQL or SQL or any other database according to your requirement.

In your project, you need to migrate the database and create a superuser. For the same follow the following command

python makemigrations
python migrate
python createsuperuser
python collectstatic


Configuring Apache

We will configure the Apache as a front end. Client connections that it receives will be translated into the WSGI format that the Django application expects using the mod_wsgi module. Port 80 is the default port that is kept open. The Django project will be deployed on this port. To configure the WSGI, we need to edit the default port settings.

On your terminal,

sudo nano /etc/apache2/sites-available/000-default.conf

Configure the file as shown below


After configuring the above file we will have to give permission.

chmod 664 ~/yourprojectname/db.sqlite3
sudo chown :www-data ~/yourprojectname/db.sqlite3
sudo chown :www-data ~/yourprojectname

The final step would be to allow the traffic to the Apache process

sudo ufw allow ‘Apache Full’

Restart the server and it works

sudo systemctl restart apache2

Check your Django Application on



Congratulations you deployed your Django application on Google Cloud.

We also publish articles on Medium. Read this article on Medium and follow us to see when we publish new articles.

Contact Us