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.
- Install Bitnami Django on a Compute Engine instance.
- Create a "hello world" Django application.
- Serve the application with Apache.
- Configure a database (optional).
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.
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:
- From the Google Cloud menu, select the Cloud Launcher.
- Search for "django certified by bitnami" and select the resulting
Django Certified by Bitnami
template. - Review the information and cost. Click
Launch on Compute Engine
to proceed. - Review the default zone, machine type, boot disk size and other parameters and modify as needed. Ensure that the
Allow HTTP traffic
andAllow HTTPS traffic
boxes are checked in the firewall configuration. ClickDeploy
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.
Login to the deployed instance and create a simple Django application:
-
From the Deployment Manager, click the
SSH
button to log in to the instance over SSH. -
Switch to the
bitnami
user account:sudo su - bitnami
-
Create a folder for your Django application:
sudo mkdir /opt/bitnami/projects sudo chown $USER /opt/bitnami/projects
-
Create a new project:
cd /opt/bitnami/projects/ django-admin.py startproject myproject
-
Create a new application skeleton within the project:
cd /opt/bitnami/projects/myproject python3 manage.py startapp helloworld
-
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!")
-
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'), ]
-
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')), ]
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:
-
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()
-
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
-
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'
-
Add the line below to the
/opt/bitnami/apache/conf/bitnami/bitnami.conf
file:Include "/opt/bitnami/projects/myproject/conf/httpd-app.conf"
-
Edit the
/opt/bitnami/projects/myproject/myproject/settings.py
file and update theALLOWED_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']
-
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.
To configure a database for your application, modify the /opt/bitnami/projects/myproject/myproject/settings.py
file as shown below:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DATABASE-NAME',
'HOST': '/opt/bitnami/mysql/tmp/mysql.sock',
'PORT': '3306',
'USER': 'USERNAME',
'PASSWORD': 'PASSWORD'
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'DATABASE-NAME',
'HOST': '/opt/bitnami/postgresql',
'PORT': '5432',
'USER': 'USERNAME',
'PASSWORD': 'PASSWORD'
}
}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'PATH-TO-DATABASE-FILE'
}
}
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.
Visit the Resource Manager. Select the project you used for this tutorial and click Delete
. Once
deleted, you cannot reuse the project ID.
Navigate to the Deployment Manager. Find the deployment you used for this tutorial and click Delete
.
Learn more about the topics discussed in this tutorial: