azuredjango

Deploy a Django project

NOTE: We are in the process of modifying the file structure and configuration for many Bitnami stacks. On account of these changes, the file paths stated in this guide may change depending on whether your Bitnami stack uses native Linux system packages (Approach A), or if it is a self-contained installation (Approach B). To identify your Bitnami installation type and what approach to follow, run the command below:

 $ test ! -f "/opt/bitnami/common/bin/openssl" && echo "Approach A: Using system packages." || echo "Approach B: Self-contained installation."

The output of the command indicates which approach (A or B) is used by the installation, and will allow you to identify the paths, configuration and commands to use in this guide. Refer to the FAQ for more information on these changes.

Development

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

$ python3 manage.py runserver SERVER-IP:PORT

You should now be able to browse to your project at http://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

Approach A: Bitnami installations using system packages

To serve your application through the Apache web server with the mod_wsgi module, follow the steps below.

Enable WSGI configuration for the Django application

Ensure the Django project contains a wsgi.py file with contents similar to this:

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PROJECT.settings')
application = get_wsgi_application()
Enable predefined virtual hosts for a Django project

The Bitnami installation comes with predefined HTTP and HTTPS virtual hosts for running Django projects with the mod_wsgi module. To enable them, follow the steps below:

  • Copy the files to remove the .disabled suffix:

    NOTE: These files assume that the Django project is named sample and is located at /opt/bitnami/projects/sample. If your Django project is named or located differently, edit the files below and update the file paths accordingly.

    $ sudo cp /opt/bitnami/apache2/conf/vhosts/sample-vhost.conf.disabled /opt/bitnami/apache2/conf/vhosts/sample-vhost.conf
    $ sudo cp /opt/bitnami/apache2/conf/vhosts/sample-https-vhost.conf.disabled /opt/bitnami/apache2/conf/vhosts/sample-https-vhost.conf
    
  • Restart Apache for the changes to be taken into effect:

    $ sudo /opt/bitnami/ctlscript.sh restart apache
    
Configure Django project for public access

To make the Django project properly work in your web browser, some additional changes may be needed. Open the settings.py file for the Django project and follow these steps:

  • Add the line below to the top of the file:

    import os
    
  • Disable DEBUG mode:

    DEBUG = False
    
  • Set the ALLOWED_HOSTS setting for making the Django project remotely accessible:

    • Public access to the Django project:

      ALLOWED_HOSTS = ['*']
      
    • Restricted access, e.g. only requests from the 11.22.33.44 IP address:

      ALLOWED_HOSTS = ['11.22.33.44']
      
  • Set STATIC_URL and STATIC_ROOT to serve static files:

    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    
  • Build static files. Execute the following command:

    $ python manage.py collectstatic --noinput
    
  • Create a Django superuser if you haven’t already.

Create a custom virtual host

If the predefined virtual hosts are not available to you, or if you prefer to apply a custom configuration, follow the steps below:

NOTE: These steps assume that your Django project will live under the /opt/bitnami/projects directory.

  • Create and edit the /opt/bitnami/apache2/conf/vhosts/myapp-http-vhost.conf file and add the following lines:

    <IfDefine !IS_APPNAME_LOADED>
      Define IS_APPNAME_LOADED
      WSGIDaemonProcess APPNAME python-home=/opt/bitnami/python python-path=/opt/bitnami/projects/APPNAME
    </IfDefine>
    <VirtualHost 127.0.0.1:80 _default_:80>
      ServerAlias *
      WSGIProcessGroup APPNAME
      Alias /robots.txt /opt/bitnami/projects/APPNAME/static/robots.txt
      Alias /favicon.ico /opt/bitnami/projects/APPNAME/static/favicon.ico
      Alias /static/ /opt/bitnami/projects/APPNAME/static/
      <Directory /opt/bitnami/projects/APPNAME/static>
        Require all granted
      </Directory>
      WSGIScriptAlias / /opt/bitnami/projects/APPNAME/APPNAME/wsgi.py
      <Directory /opt/bitnami/projects/APPNAME/APPNAME>
        <Files wsgi.py>
          Require all granted
        </Files>
      </Directory>
    </VirtualHost>
    

    NOTE: Replace the APPNAME placeholder with the actual name of your project.

  • Create and edit the /opt/bitnami/apache2/conf/vhosts/myapp-https-vhost.conf file and add the following lines:

    <IfDefine !IS_APPNAME_LOADED>
      Define IS_APPNAME_LOADED
      WSGIDaemonProcess APPNAME python-home=/opt/bitnami/python python-path=/opt/bitnami/projects/APPNAME
    </IfDefine>
    <VirtualHost 127.0.0.1:443 _default_:443>
      ServerAlias *
      SSLEngine on
      SSLCertificateFile "/opt/bitnami/apache2/conf/bitnami/certs/server.crt"
      SSLCertificateKeyFile "/opt/bitnami/apache2/conf/bitnami/certs/server.key"
      WSGIProcessGroup APPNAME
      Alias /robots.txt /opt/bitnami/projects/APPNAME/static/robots.txt
      Alias /favicon.ico /opt/bitnami/projects/APPNAME/static/favicon.ico
      Alias /static/ /opt/bitnami/projects/APPNAME/static/
      <Directory /opt/bitnami/projects/APPNAME/static>
        Require all granted
      </Directory>
      WSGIScriptAlias / /opt/bitnami/projects/APPNAME/APPNAME/wsgi.py
      <Directory /opt/bitnami/projects/APPNAME/APPNAME>
        <Files wsgi.py>
          Require all granted
        </Files>
      </Directory>
    </VirtualHost>
    

    NOTE: Replace the APPNAME placeholder with the actual name of your project.

  • Restart the Apache server:

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

Approach B: Self-contained Bitnami installations

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 PROJECT/PROJECT/wsgi.py inside the Django project directory. 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/pythonA.B/site-packages/Django-X.Y.Z-pyA.B.egg/django/contrib/admin/static"
    WSGIScriptAlias /PROJECT '/opt/bitnami/apps/django/django_projects/PROJECT/PROJECT/wsgi.py'
    

    NOTE: Where A.B is the Python version (for example 3.6) and X.Y.Z is the Django version (for example 2.0.2).

  • 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/apache22/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/PROJECT/PROJECT/settings.py file 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/APP.

Last modification November 17, 2020