Bitnami Django Virtual Machine

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! This guide includes some basic information you will need to get started with your application.

How to import a Bitnami Virtual Machine?

Check the following instructions to import a Bitnami Virtual Machine:

Importing a Bitnami Virtual Machine in VirtualBox
  • Select the "File -> Import Appliance" menu option and select the .ova file downloaded from the Bitnami website. Then click "Continue".
  • Once it is imported, click the "Start" button in the VirtualBox toolbar.

For a detailed walkthrough, check our Virtualbox tutorial.

Importing a Bitnami Virtual Machine in VMware
  • Select the "File -> Import" menu option and select the .ova file downloaded from the Bitnami website. Then click "Continue".
  • Once the import is complete, click "Finish" to start the virtual machine.

For a detailed walkthrough, check our VMware tutorial.

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 password. These credentials allow you to log in to your Virtual Machines server using an SSH client and execute commands on the server using the command line.

What is the administrator password?

Password: The administrator password to log in to your application is randomly generated during the first boot. Check the FAQ to learn how to retrieve it.

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?

You can obtain the SSH password from the virtual machine console when it starts up. Click here for more information.

How to access your application?

Once you have imported your Bitnami Virtual Machine, the IP address for your application is displayed on the virtual machine's login screen. Access the application via your browser by entering this IP address.

Check these instructions about how to remotely access the Bitnami application.

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

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 find the MySQL database credentials?

How to find the PostgreSQL database credentials?

How to connect to the MySQL database?

You can connect to the MySQL database from the same computer where it is installed with the mysql client tool.

$ mysql -u root -p

You will be prompted to enter the root user password. This is the same as the application password.

How to connect to the PostgreSQL database?

You can connect to the PostgreSQL database from the same computer where it is installed with the psql client tool.

$ psql -U postgres

You will be prompted to enter the *postgres* user password. This is the same as the [application password](/virtual-machine/faq#how-to-find-application-credentials).

How to connect to Django from a different machine?

For security reasons, the Django ports in this solution cannot be accessed over a public IP address. To connect to Django from a different machine, you must open port 80, 443 for remote access. Refer to the FAQ for more information on this.

IMPORTANT: Making this application's network ports public is a significant security risk. You are strongly advised to only allow access to those ports from trusted networks. If, for development purposes, you need to access from outside of a trusted network, please do not allow access to those ports via a public IP address. Instead, use a secure channel such as a VPN or an SSH tunnel. Follow these instructions to remotely connect safely and reliably.

How to reset the MariaDB root password?

Please note that depending on the version you have installed, you may find the MariaDB files at /opt/bitnami/mysql

If you don't remember your MariaDB root password, you can follow the steps below to reset it to a new value:

  • Create a file in /home/bitnami/mysql-init with the content shown below (replace NEW_PASSWORD with the password you wish to use):

     UPDATE mysql.user SET Password=PASSWORD('NEW_PASSWORD') WHERE User='root';
     FLUSH PRIVILEGES;
    
  • Stop the MariaDB server:

     $ sudo /opt/bitnami/ctlscript.sh stop mariadb
    
  • Start MariaDB with the following command:

     $ sudo /opt/bitnami/mariadb/bin/mysqld_safe --defaults-file=/opt/bitnami/mariadb/my.cnf --pid-file=/opt/bitnami/mariadb/data/mysqld.pid --init-file=/home/bitnami/mysql-init 2> /dev/null &
    
  • Restart the MariaDB server:

     $ sudo /opt/bitnami/ctlscript.sh restart mariadb
    
  • Remove the init script

     $ rm /home/bitnami/mysql-init
    

How to change the MariaDB root password?

You can modify the MariaDB password using the following command at the shell prompt:

$ /opt/bitnami/mariadb/bin/mysqladmin -p -u root password NEW_PASSWORD

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 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 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.

     $ 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
     $ python3 manage.py startapp APP
    
  • 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 modify it to look like this:

     from django.conf.urls import url
     from django.urls import include
    
     urlpatterns = [
         url(r'^APP/', include('APP.urls')),
     ]
    
  • Follow the steps to deploy your Django project with 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.

    TIP: If you are experiencing problems when editing the above file, check if the permissions in your stack are set properly.

    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/myproject/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.

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

      $ touch /opt/bitnami/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf
      $ 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>
    
    <Directory "/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT">
        Options +MultiViews
        AllowOverride All
        <IfVersion >= 2.3>
            Require all granted
        </IfVersion>
    
        WSGIProcessGroup wsgi-djangostack
    
        WSGIApplicationGroup %{GLOBAL}
    </Directory>
    
    Alias /PROJECT/static "/opt/bitnami/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/contrib/admin/static"
    WSGIScriptAlias /PROJECT '/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
    
  • 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"
    
  • Edit the /opt/bitnami/apps/django/django_projects/myproject/myproject/settings.pyfile and update the ALLOWED_HOSTS variable with the IP address of your server, as in the example below:

    ALLOWED_HOSTS = ['SERVER-IP', 'localhost', '127.0.0.1']
    
  • 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'
  }
}

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.

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.

How to upgrade Django's Python package?

NOTE: Before upgrading Django, make sure that your Django applications are compatible with the new version.

Follow the steps below:

  • Execute this command from the uncompressed directory:

     $ sudo pip install --upgrade Django --install-option="--prefix=/opt/bitnami/apps/django"
    

    If you want a specific version of Django (not the latest one), add the version number as shown below:

     $ sudo pip install --upgrade Django==1.6.5 --install-option="--prefix=/opt/bitnami/apps/django"
    
  • Edit the django-admin.py script (usually in /opt/bitnami/apps/django/bin) and change the first line to use python instead of .python2.7.bin:

     #!/opt/bitnami/python/bin/python
    

How to create a full backup of Django?

Backup

The Bitnami Django Stack is self-contained and the simplest option for performing a backup is to copy or compress the Bitnami stack installation directory. To do so in a safe manner, you will need to stop all servers, so this method may not be appropriate if you have people accessing the application continuously.

Follow these steps:

  • Change to the directory in which you wish to save your backup:

      $ cd /your/directory
    
  • Stop all servers:

      $ sudo /opt/bitnami/ctlscript.sh stop
    
  • Create a compressed file with the stack contents:

      $ sudo tar -pczvf application-backup.tar.gz /opt/bitnami
    
  • Restart all servers:

      $ sudo /opt/bitnami/ctlscript.sh start
    

You should now download or transfer the application-backup.tar.gz file to a safe location.

Restore

Follow these steps:

  • Change to the directory containing your backup:

      $ cd /your/directory
    
  • Stop all servers:

      $ sudo /opt/bitnami/ctlscript.sh stop
    
  • Move the current stack to a different location:

      $ sudo mv /opt/bitnami /tmp/bitnami-backup
    
  • Uncompress the backup file to the original directoryv

      $ sudo tar -pxzvf application-backup.tar.gz -C /
    
  • Start all servers:

      $ sudo /opt/bitnami/ctlscript.sh start
    

If you want to create only a database backup, refer to these instructions for MySQL and PostgreSQL.

How to debug errors in your database?

The main log file is created at /opt/bitnami/mysql/data/mysqld.log on the MySQL database server host.

virtualMachine

Bitnami Documentation