Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Latest commit

 

History

History
220 lines (150 loc) · 8.39 KB

get-started-bitnami-django.md

File metadata and controls

220 lines (150 loc) · 8.39 KB
title description author tags date_published
Get started with Bitnami Django on Google Cloud
Create and deploy a basic Django Web application on Google Cloud with Bitnami Django.
vikram-bitnami
django, python, bitnami
2017-03-15

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:

     sudo su - bitnami
    
  3. Create a folder for your Django application:

     sudo mkdir /opt/bitnami/projects
     sudo chown $USER /opt/bitnami/projects
    
  4. Create a new project:

     cd /opt/bitnami/projects/
     django-admin.py startproject myproject
    
  5. Create a new application skeleton within the project:

     cd /opt/bitnami/projects/myproject
     python3 manage.py startapp helloworld
    
  6. Edit the /opt/bitnami/projects/myproject/helloworld/views.py file and add this content:

     from django.http import HttpResponse
    
     def index(request):
       return HttpResponse("Hello world!")
    
  7. Create the /opt/bitnami/projects/myproject/helloworld/urls.py file and add these lines to it:

     from django.conf.urls import url
     from . import views
    
     urlpatterns = [
         url(r'^$', views.index, name='index'),
     ]
    
  8. Edit the /opt/bitnami/projects/myproject/myproject/urls.py file and modify it to look like this:

     from django.conf.urls import url
     from django.urls import include
     
     urlpatterns = [
         url(r'^helloworld/', include('helloworld.urls')),
     ]
    

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:

    import os
    import sys
    sys.path.append('/opt/bitnami/projects/myproject')
    os.environ.setdefault("PYTHON_EGG_CACHE", "/opt/bitnami/apps/django/django_projects/myproject/egg_cache")
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
  2. Create a conf/ subdirectory in the project directory and create an Apache configuration file:

    mkdir /opt/bitnami/projects/myproject/conf
    touch /opt/bitnami/projects/myproject/conf/httpd-app.conf
  3. Add the following Apache directives in the /opt/bitnami/projects/myproject/conf/httpd-app.conf file:

     <IfDefine !IS_DJANGOSTACK_LOADED>
       Define IS_DJANGOSTACK_LOADED
       WSGIDaemonProcess wsgi-djangostack   processes=2 threads=15    display-name=%{GROUP}
     </IfDefine>
    
     <Directory "/opt/bitnami/projects/myproject/myproject">
         Options +MultiViews
         AllowOverride All
         <IfVersion >= 2.3>
             Require all granted
         </IfVersion>
    
         WSGIProcessGroup wsgi-djangostack
    
         WSGIApplicationGroup %{GLOBAL}
     </Directory>
    
     Alias /myproject/static "/opt/bitnami/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/contrib/admin/static"
     WSGIScriptAlias /myproject '/opt/bitnami/projects/myproject/myproject/wsgi.py'
    
  4. Add the line below to the /opt/bitnami/apache/conf/bitnami/bitnami.conf file:

     Include "/opt/bitnami/projects/myproject/conf/httpd-app.conf"
    
  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:

     ALLOWED_HOSTS = ['XX.XX.XX.XX', 'localhost', '127.0.0.1']
    
  6. Restart the Apache server using the Bitnami control script:

     sudo /opt/bitnami/ctlscript.sh restart apache
    

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

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'DATABASE-NAME',
      'HOST': '/opt/bitnami/mysql/tmp/mysql.sock',
      'PORT': '3306',
      'USER': 'USERNAME',
      'PASSWORD': 'PASSWORD'
  }
}

PostgreSQL

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.postgresql_psycopg2',
      'NAME': 'DATABASE-NAME',
      'HOST': '/opt/bitnami/postgresql',
      'PORT': '5432',
      'USER': 'USERNAME',
      'PASSWORD': 'PASSWORD'
  }
}

SQLite

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.sqlite3',
      'NAME': 'PATH-TO-DATABASE-FILE'
  }
}

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: