Deploy your Bitnami Django Stack on Google Cloud Platform now! Launch Now

Bitnami Django for Google Cloud Platform

Description

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Python is a dynamic, object-oriented programming language used for many kinds of software.

First steps with the Bitnami Django Stack

Welcome to your new Bitnami application running on Google Cloud Platform! Here are a few questions (and answers!) you might need when first starting with your application.

What credentials do I need?

You need two sets of credentials:

  • The application password. This will allow you to log in to the services of the Bitnami stack.

  • The server credentials, consisting of an SSH username and key. These credentials allow you to log in to your Google Cloud Platform server using an SSH client and execute commands on the server using the command line.

What is the administrator password?

What SSH username should I use for secure shell access to my application?

SSH username: bitnami

How do I get my SSH key or password?

What are the default ports?

A port is an endpoint of communication in an operating system that identifies a specific process or a type of service. Bitnami stacks include several services or servers that require a port.

Remember that if you need to open some ports you can follow the instructions given in the FAQ to learn how to open the server ports for remote access.

Port 22 is the default port for SSH connections.

Bitnami opens some ports for the main servers. These are the ports opened by default: 80, 443.

Which components are installed with the Bitnami Django Stack?

The Bitnami Django Stack ships the components listed below. If you want to know which specific version of each component is bundled in the stack you are downloading, check the README.txt file on the download page or in the stack installation directory. You can also find more information about each component using the links below.

Main components

Dependencies and additional components

  • SQLite database
  • Imaging
  • easy_install
  • pip
  • OpenSSL library
  • CURL library

How can I get started with Django?

To get started, we suggest the following steps:

Step 1. Try the sample Django project.

The Bitnami Django Stack comes with a sample project, which you can access via your browser at http://SERVER-IP/Project. You can modify the sample project using the files at /opt/bitnami/apps/django/django_projects.

For more information, refer to these instructions.

Step 2. Create a custom Django project.

You can create a new project for your application at /opt/bitnami/apps/django/django_projects. Simply change to the above directory and run the following command to initialize a new Django project:

$ sudo django-admin.py startproject PROJECT

For more information, refer to these instructions.

Step 3. Deploy your Django application.

To deploy your application, use the standalone server in Django by executing the following command inside your project folder:

$ python manage.py runserver

This will start the Django server for your application on port 8000. Find out how to access the application using your Web browser.

For a production environment, we recommend configuring Apache with the mod_wsgi module before starting to serve your application. For more information, refer to these instructions.

How to start or stop the services?

Each Bitnami stack includes a control script that lets you easily stop, start and restart services. The script is located at /opt/bitnami/ctlscript.sh. Call it without any service name arguments to start all services:

$ sudo /opt/bitnami/ctlscript.sh start

Or use it to restart a single service, such as Apache only, by passing the service name as argument:

$ sudo /opt/bitnami/ctlscript.sh restart apache

Use this script to stop all services:

$ sudo /opt/bitnami/ctlscript.sh stop

Restart the services by running the script without any arguments:

$ sudo /opt/bitnami/ctlscript.sh restart

Obtain a list of available services and operations by running the script without any arguments:

$ sudo /opt/bitnami/ctlscript.sh

How can I run a command in the Bitnami Django Stack?

Log in to the server console as the bitnami user and run the command as usual. The required environment is automatically loaded for the bitnami user.

How to upload files to the server with SFTP?

NOTE: Bitnami applications can be found in /opt/bitnami/apps.
  • If you are using the Bitnami Launchpad for Google Cloud Platform, obtain your server SSH key by following these steps:

    • Browse to the Bitnami Launchpad for Google Cloud Platform dashboard and sign in if required using your Bitnami account.
    • Select the "Virtual Machines" menu item.
    • Select your cloud server from the resulting list.
    • Download the SSH key for your server in PPK or PEM format. Note the server IP address on the same page.

Server information

NOTE: Replace USERNAME in the commands below with your Google Cloud platform username.
	$ sudo su USERNAME
	$ ssh-keygen -t rsa -f ~/.ssh/my-ssh-key -C USERNAME
  • Enter the passphrase twice. The SSH key pair will be generated and saved in /home/USERNAME/.ssh/my-ssh-key and /home/USERNAME/.ssh/my-ssh-key.pub.

Although you can use any SFTP/SCP client to transfer files to your server, this guide documents FileZilla (Windows, Linux and Mac OS X), WinSCP (Windows) and Cyberduck (Mac OS X).

Using an SSH Key

Once you have your server's SSH key, choose your preferred application and follow the steps below to connect to the server using SFTP.

FileZilla
IMPORTANT: To use FileZilla, your server private key should be in PPK format.

Follow these steps:

  • Download and install FileZilla.
  • Launch FileZilla and use the "Edit -> Settings" command to bring up FileZilla's configuration settings.
  • Within the "Connection -> SFTP" section, use the "Add keyfile" command to select the private key file for the server. FileZilla will use this private key to log in to the server.

    FileZilla configuration

  • Use the "File -> Site Manager -> New Site" command to bring up the FileZilla Site Manager, where you can set up a connection to your server.
  • Enter your server host name and specify bitnami as the user name.
  • Select "SFTP" as the protocol and "Ask for password" as the logon type.

    FileZilla configuration

  • Use the "Connect" button to connect to the server and begin an SFTP session. You might need to accept the server key, by clicking "Yes" or "OK" to proceed.

You should now be logged into the /home/bitnami directory on the server. You can now transfer files by dragging and dropping them from the local server window to the remote server window.

If you have problems accessing your server, get extra information by use the "Edit -> Settings -> Debug" menu to activate FileZilla's debug log.

FileZilla debug log

WinSCP
IMPORTANT: To use WinSCP, your server private key should be in PPK format.

Follow these steps:

  • Download and install WinSCP.
  • Launch WinSCP and in the "Session" panel, select "SFTP" as the file protocol.
  • Enter your server host name and specify bitnami as the user name.

    WinSCP configuration

  • Click the "Advanced…" button and within the "SSH -> Authentication -> Authentication parameters" section, select the private key file for the server. WinSCP will use this private key to log in to the server.

    WinSCP configuration

  • From the "Session" panel, use the "Login" button to connect to the server and begin an SCP session.

You should now be logged into the /home/bitnami directory on the server. You can now transfer files by dragging and dropping them from the local server window to the remote server window.

If you need to upload files to a location where the bitnami user doesn't have write permissions, you have two options:

  • Once you have configured WinSCP as described above, click the "Advanced…" button and within the "Environment -> Shell" panel, select sudo su - as your shell. This will allow you to upload files using the administrator account.

    WinSCP configuration

  • Upload the files to the /home/bitnami directory as usual. Then, connect via SSH and move the files to the desired location with the sudo command, as shown below:

     $ sudo mv /home/bitnami/uploaded-file /path/to/desired/location/
    
Cyberduck
IMPORTANT: To use Cyberduck, your server private key should be in PEM format.

Follow these steps:

  • Select the "Open Connection" command and specify "SFTP" as the connection protocol.

    Cyberduck configuration

  • In the connection details panel, under the "More Options" section, enable the "Use Public Key Authentication" option and specify the path to the private key file for the server.

    Cyberduck configuration

  • Use the "Connect" button to connect to the server and begin an SFTP session.

You should now be logged into the /home/bitnami directory on the server. You can now transfer files by dragging and dropping them from the local server window to the remote server window.

How to connect instances hosted in separate virtual networks or VPCs?

The Google Cloud Platform makes it possible to connect instances hosted in separate Virtual Private Clouds (VPCs), even if those instances belong to different projects or are hosted in different regions. This feature, known as VPC Network Peering, can result in better security (as services do not need to be exposed on public IP addresses) and performance (due to use of private, rather than public, networks and IP addresses).

Learn more about VPC Network Peering.

How to start with the sample Django project?

Access the sample project via your browser at http://SERVER-IP/Project.

The Django project files are located at /opt/bitnami/apps/django/django_projects.

Here is an example of what you will see:

Django demo

How to create a new superuser?

You can easily create a superuser account to access the Django admin site by following these steps, assuming that the projects folder is located at /opt/bitnami/apps/django/django_projects.

  • Change to your project folder:

     $ cd /opt/bitnami/apps/django/django_projects/PROJECT
    
  • Create a new superuser:

     $ python manage.py createsuperuser
    
  • Wait for the prompt and fill the required information to set up a new superuser.

How to create a new Django project?

The projects folder is located at /opt/bitnami/apps/django/django_projects.

Follow the steps below:

  • Change to your projects folder and run the following command to create a new project. Replace the PROJECT placeholder with the name of your project in this and all subsequent commands.

     $ sudo django-admin.py startproject PROJECT
    
  • If you wish to create a new application inside the project, execute the following commands as well. Replace the APP placeholder with the name of your application in this and all subsequent commands.

     $ cd PROJECT
     $ sudo python manage.py startapp APP
    

    You can check your Django version at any time by executing the following command:

     python -c "import django; print(django.get_version())"
    
  • Edit the /opt/bitnami/apps/django/django_projects/PROJECT/APP/views.py file and add this content:

     from django.http import HttpResponse
    
     def index(request):
       return HttpResponse("Hello world!")
    
  • Create the /opt/bitnami/apps/django/django_projects/PROJECT/APP/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/apps/django/django_projects/PROJECT/PROJECT/urls.py file and add this line to the list of URL patterns.

     url(r'^APP/', include('APP.urls')),
    

    For example, your file should look like this:

     url(r'^admin/', include(admin.site.urls)),
     url(r'^APP/', include('APP.urls')),
     url(r'^$', default_urlconf),
    
  • Restart Apache.

     $ sudo /opt/bitnami/ctlscript.sh restart apache
    

You should now be able to see your new application at http://SERVER-IP/PROJECT/APP.

If you prefer to use the standalone server in Django, refer to this section. Note that in this case, you will need to create an SSH tunnel to port 8000 (or the port configured for the standalone server) following these instructions. In this case, you should be able to see your new application at http://localhost:8000/APP.

To get started with Django, check the official Django documentation for the version that you are using.

How to deploy a Django project?

Development

For development, use the standalone server in Django by executing the following command inside your project folder:

$ python manage.py runserver SERVER-IP:PORT

SERVER-IP and PORT are optional parameters. 127.0.0.1:8000 will be used by default if you don't specify any of them. If you plan to remotely access the server, you should use the IP address 0.0.0.0, or the actual server IP address.

IMPORTANT: Remember to open the port used by the Django server in the server firewall. Refer to the FAQ for more information.

Production

For a production environment, we recommend configuring Apache with the mod_wsgi module before starting to serve your application (already installed and activated by default).

  • First, modify the WSGI application script file at /opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/wsgi.py. Remember to replace PROJECT with the actual path and name to your Django project.

    Although the exact content may be different depending on your Django version, ensure that the content is similar to the code below and that you add your project to the path with sys.path.append.

     import os
     import sys
     sys.path.append('/opt/bitnami/apps/django/django_projects/PROJECT')
     os.environ.setdefault("PYTHON_EGG_CACHE", "/opt/bitnami/apps/django/django_projects/PROJECT/egg_cache")
    
     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT.settings")
    
     from django.core.wsgi import get_wsgi_application
     application = get_wsgi_application()
    
  • Create a conf/ folder in the /opt/bitnami/apps/django/django_projects/PROJECT/ directory.

     $ sudo mkdir /opt/bitnami/apps/django/django_projects/PROJECT/conf
    
  • Create the following files:

     $ sudo touch /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf
     $ sudo touch /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-app.conf
    
  • On Linux, you can run the application with mod_wsgi in daemon mode. Add the following code in /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-app.conf:

     <IfDefine !IS_DJANGOSTACK_LOADED>
       Define IS_DJANGOSTACK_LOADED
       WSGIDaemonProcess wsgi-djangostack processes=2 threads=15 display-name=%{GROUP}
     </IfDefine>
    
     WSGIScriptAlias /PROJECT '/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
     <Directory "/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/">
         WSGIProcessGroup wsgi-djangostack
         WSGIApplicationGroup %{GLOBAL}
         <IfVersion < 2.3 >
             Order allow,deny
             Allow from all
         </IfVersion>
         <IfVersion >= 2.3>
             Require all granted
         </IfVersion>
     </Directory>
    
  • In the /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf file, add this code:

     Include "/opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-app.conf"
    
  • Add the line below to the /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf file:

     Include "/opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf"
    
  • Restart the Apache server.

     $ sudo /opt/bitnami/ctlscript.sh restart apache
    

You should now be able to browse to your project at http://SERVER-IP/PROJECT.

How to configure a database for your project?

If you wish to configure a database for your project, configure the settings.py file with the following settings. The examples below show how to configure the database connection with a database called "djangostack".

MySQL

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

PostgreSQL

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

SQLite

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.sqlite3',
      'NAME': '/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/PROJECT.db'
  }
}

What is GeoDjango?

GeoDjango intends to be a world-class geographic Web framework. Its goal is to make it as easy as possible to build GIS (Geographic information system) Web applications. GeoDjango is an included contrib module for Django.

How can I get GeoDjango with PostGIS support?

PostGIS is included in all stacks that ship PostgreSQL.

How to create a database template with PostGIS support?

NOTE: The following section assumes that you are working with Django 1.5+ which already supports PostGIS 2.0+. It is based on the official GeoDjango documentation.

Create a database template by following the steps below. In this example, the template will be named template_postgis.

  • Create a template:

     $ createdb template_postgis
    
     # Allows non-superusers the ability to create from this template
     $ psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';"
    
     $ psql template_postgis -c "create extension postgis"
     $ psql template_postgis -c "create extension postgis_topology"
     $ psql template_postgis -f /opt/bitnami/postgresql/share/contrib/postgis-2.0/legacy.sql
    
     # Enabling users to alter spatial tables.
     $ psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;"
     $ psql -d template_postgis -c "GRANT ALL ON geography_columns TO PUBLIC;"
     $ psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"
    

    To check the version of PostGIS and its dependencies, execute the following command:

     $ psql template_postgis -c "SELECT PostGIS_Full_Version()"
    
    NOTE: Although the official documentation suggests loading legacy.sql only "if really necessary", we have found that it is required for installing Tiger Geocoder and running the OGC tests suite. Also, it is necessary when restoring data from an old database. Once these tasks are done, you can always uninstall it using uninstall_legacy.sql.
  • Create a new database from the template with PostGIS support using this command:

     $ createdb -T template_postgis geocoder
    

    Or, from the database console:

     CREATE DATABASE geocoder TEMPLATE template_postgis;
    

How can I create a geographic web application using GeoDjango?

NOTE: The following section assumes that you are working with Django 1.5+ which already supports PostGIS 2.0+. It is based on the official GeoDjango documentation.

Follow these steps:

  • Create a spatial database:

     $ createdb -T template_postgis geodjango
    
  • Create a new project:

     $ django-admin.py startproject geodjango
     $ cd geodjango
     $ python manage.py startapp world
    
  • Configure database settings and add a few variables so GEOS and GDAL components are found. Edit the project settings file geodjango/settings.py and update it to look like this:

     DATABASES = {
       'default': {
           'ENGINE': 'django.contrib.gis.db.backends.postgis', 
           'NAME': 'geodjango',  # Name of your spatial database
           'USER': 'postgres',   # Database user 
           'PASSWORD': 'bitnami',# Database password 
           'HOST': 'SERVER-IP',
           'PORT': '5432',                      
       }
     }
    
     GEOS_LIBRARY_PATH = '/opt/bitnami/postgresql/lib/libgeos_c.so'
     GDAL_LIBRARY_PATH = '/opt/bitnami/postgresql/lib/libgdal.so'
     GDAL_DATA = '/opt/bitnami/postgresql/share/gdal' 
    
  • In the same file, include the django.contrib.admin.gis and world applications in the INSTALLED_APPS list:

     INSTALLED_APPS = (
         'django.contrib.auth',
         'django.contrib.contenttypes',
         'django.contrib.sessions',
         'django.contrib.sites',
         'django.contrib.messages',
         'django.contrib.staticfiles',
         'django.contrib.admin',
         'django.contrib.gis',
         'world'
     )
    

Now that the environment is set, follow the official GeoDjango tutorial, starting from the "Geographic Data" section.

google