Powered By GitBook
get-started-bitnami-django
Create and deploy a basic Django Web application on Google Cloud with Bitnami Django.
Contributed by the Google Cloud community. Not official Google documentation.
This tutorial demonstrates how to create and deploy a basic "hello world" Django web app on Google Cloud in just a few minutes using Bitnami Django.

Objectives

    Install Bitnami Django on a Compute Engine instance.
    Create a "hello world" Django application.
    Serve the application with Apache.
    Configure a database (optional).

Before you begin

Before starting this tutorial, ensure that you have set up a Google Cloud project. You can use an existing project or create a new project.

Cost

The default configuration allows you to run a low-traffic web app powered by Django using an f1-micro instance with a standard 10 GB persistent disk. You can customize the configuration when deploying this solution or change it later, although the default configuration is fine for the purposes of this tutorial.
Estimated cost for the above default configuration is $4.28 per month, based on 30-day, 24 hours per day usage in the Central US region. Sustained use discount is included.
Use the pricing calculator to generate a cost estimate based on your projected usage. New Google Cloud customers may be eligible for a free trial.

Deploy Bitnami Django on a Compute Engine instance

Deploy Bitnami Django on a Compute Engine instance:
    1.
    From the Google Cloud menu, select the Cloud Launcher.
    2.
    Search for "django certified by bitnami" and select the resulting Django Certified by Bitnami template.
    3.
    Review the information and cost. Click Launch on Compute Engine to proceed.
    4.
    Review the default zone, machine type, boot disk size and other parameters and modify as needed. Ensure that the Allow HTTP traffic and
    Allow HTTPS traffic boxes are checked in the firewall configuration. Click Deploy to proceed with the deployment.
The Cloud Launcher deploys Bitnami Django on a new Compute Engine instance. You can monitor the progress of the deployment from the Deployment Manager. After deployment is complete, note the public IP address of the instance and the password for the MySQL and PostgreSQL databases.

Create a "hello world" Django application

Login to the deployed instance and create a simple Django application:
    1.
    From the Deployment Manager, click the SSH button to log in to the instance over SSH.
    2.
    Switch to the bitnami user account:
    1
    sudo su - bitnami
    Copied!
    3.
    Create a folder for your Django application:
    1
    sudo mkdir /opt/bitnami/projects
    2
    sudo chown $USER /opt/bitnami/projects
    Copied!
    4.
    Create a new project:
    1
    cd /opt/bitnami/projects/
    2
    django-admin.py startproject myproject
    Copied!
    5.
    Create a new application skeleton within the project:
    1
    cd /opt/bitnami/projects/myproject
    2
    python3 manage.py startapp helloworld
    Copied!
    6.
    Edit the /opt/bitnami/projects/myproject/helloworld/views.py file and add this content:
    1
    from django.http import HttpResponse
    2
    ​
    3
    def index(request):
    4
    return HttpResponse("Hello world!")
    Copied!
    7.
    Create the /opt/bitnami/projects/myproject/helloworld/urls.py file and add these lines to it:
    1
    from django.conf.urls import url
    2
    from . import views
    3
    ​
    4
    urlpatterns = [
    5
    url(r'^#x27;, views.index, name='index'),
    6
    ]
    Copied!
    8.
    Edit the /opt/bitnami/projects/myproject/myproject/urls.py file and modify it to look like this:
    1
    from django.conf.urls import url
    2
    from django.urls import include
    3
    ​
    4
    urlpatterns = [
    5
    url(r'^helloworld/', include('helloworld.urls')),
    6
    ]
    Copied!

Serve the application with Apache

Bitnami Django includes a pre-configured instance of the Apache Web server. Configure Apache to serve the application on the standard Web server port 80:
    1.
    Edit the WSGI application script file at /opt/bitnami/projects/myproject/myproject/wsgi.py and modify it to look like this:
    1
    import os
    2
    import sys
    3
    sys.path.append('/opt/bitnami/projects/myproject')
    4
    os.environ.setdefault("PYTHON_EGG_CACHE", "/opt/bitnami/apps/django/django_projects/myproject/egg_cache")
    5
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
    6
    from django.core.wsgi import get_wsgi_application
    7
    application = get_wsgi_application()
    Copied!
    2.
    Create a conf/ subdirectory in the project directory and create an Apache configuration file:
    1
    mkdir /opt/bitnami/projects/myproject/conf
    2
    touch /opt/bitnami/projects/myproject/conf/httpd-app.conf
    Copied!
    3.
    Add the following Apache directives in the /opt/bitnami/projects/myproject/conf/httpd-app.conf file:
    Define IS_DJANGOSTACK_LOADED WSGIDaemonProcess wsgi-djangostack processes=2 threads=15 display-name=%{GROUP}
    Options +MultiViews AllowOverride All= 2.3> Require all granted WSGIProcessGroup wsgi-djangostack WSGIApplicationGroup %{GLOBAL}
    1
    Alias /myproject/static "/opt/bitnami/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/contrib/admin/static"
    2
    WSGIScriptAlias /myproject '/opt/bitnami/projects/myproject/myproject/wsgi.py'
    Copied!
    4.
    Add the line below to the /opt/bitnami/apache/conf/bitnami/bitnami.conf file:
    1
    Include "/opt/bitnami/projects/myproject/conf/httpd-app.conf"
    Copied!
    5.
    Edit the /opt/bitnami/projects/myproject/myproject/settings.py file and update the ALLOWED_HOSTS variable with the public IP address of your Compute Engine instance, as in the example below:
    1
    ALLOWED_HOSTS = ['XX.XX.XX.XX', 'localhost', '127.0.0.1']
    Copied!
    6.
    Restart the Apache server using the Bitnami control script:
    1
    sudo /opt/bitnami/ctlscript.sh restart apache
    Copied!
Browse to http://XX.XX.XX.XX/myproject/helloworld and confirm that you see the output Hello world!.
If you see this output, your simple Django application is now deployed and operational. You can begin modifying it to suit your requirements.

Configure a database (optional)

To configure a database for your application, modify the /opt/bitnami/projects/myproject/myproject/settings.py file as shown below:

MySQL

1
DATABASES = {
2
'default': {
3
'ENGINE': 'django.db.backends.mysql',
4
'NAME': 'DATABASE-NAME',
5
'HOST': '/opt/bitnami/mysql/tmp/mysql.sock',
6
'PORT': '3306',
7
'USER': 'USERNAME',
8
'PASSWORD': 'PASSWORD'
9
}
10
}
Copied!

PostgreSQL

1
DATABASES = {
2
'default': {
3
'ENGINE': 'django.db.backends.postgresql_psycopg2',
4
'NAME': 'DATABASE-NAME',
5
'HOST': '/opt/bitnami/postgresql',
6
'PORT': '5432',
7
'USER': 'USERNAME',
8
'PASSWORD': 'PASSWORD'
9
}
10
}
Copied!

SQLite

1
DATABASES = {
2
'default': {
3
'ENGINE': 'django.db.backends.sqlite3',
4
'NAME': 'PATH-TO-DATABASE-FILE'
5
}
6
}
Copied!

Cleaning up

After you have finished this tutorial, you can remove the resources you created on Google Cloud so you aren't billed for them any longer. You can delete the resources individually, or delete the entire project.

Deleting the project

Visit the Resource Manager. Select the project you used for this tutorial and click Delete. Once deleted, you cannot reuse the project ID.

Deleting individual resources

Navigate to the Deployment Manager. Find the deployment you used for this tutorial and click Delete.

Next steps

Learn more about the topics discussed in this tutorial:
Last modified 6mo ago