nativeInstallerweblate

Deploy a Django project

Before running the commands shown on this page, you should load the Bitnami stack environment by executing the installdir/use_APPNAME script (Linux and MacOS) or by clicking the shortcut in the Start Menu under “Start -> Bitnami APPNAME Stack -> Application console” (Windows). On OS X VMs, the installation directory is /opt/bitnami and OS X VM users can click the “Open Terminal” button to run commands. Learn more about the Bitnami stack environment and about OS X VMs.

NOTE: The Approach A sections referred to below do not apply to Bitnami native installers. Users of Bitnami native installers should refer only to the Approach B sections.

Development

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

$ python3 manage.py runserver localhost:PORT

You should now be able to browse to your project at http://localhost:PORT.

localhost 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 Windows, Linux and macOS 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 installdir/projects/sample. If your Django project is named or located differently, edit the files below and update the file paths accordingly.

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

      $ sudo installdir/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 installdir/projects directory.

  • Create and edit the installdir/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=installdir/python python-path=/opt/bitnami/projects/APPNAME
      </IfDefine>
      <VirtualHost 127.0.0.1:80 _default_:80>
        ServerAlias *
        WSGIProcessGroup APPNAME
        Alias /robots.txt installdir/projects/APPNAME/static/robots.txt
        Alias /favicon.ico installdir/projects/APPNAME/static/favicon.ico
        Alias /static/ installdir/projects/APPNAME/static/
        <Directory installdir/projects/APPNAME/static>
          Require all granted
        </Directory>
        WSGIScriptAlias / installdir/projects/APPNAME/APPNAME/wsgi.py
        <Directory installdir/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 installdir/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=installdir/python python-path=/opt/bitnami/projects/APPNAME
      </IfDefine>
      <VirtualHost 127.0.0.1:443 _default_:443>
        ServerAlias *
        SSLEngine on
        SSLCertificateFile "installdir/apache2/conf/bitnami/certs/server.crt"
        SSLCertificateKeyFile "installdir/apache2/conf/bitnami/certs/server.key"
        WSGIProcessGroup APPNAME
        Alias /robots.txt installdir/projects/APPNAME/static/robots.txt
        Alias /favicon.ico installdir/projects/APPNAME/static/favicon.ico
        Alias /static/ installdir/projects/APPNAME/static/
        <Directory installdir/projects/APPNAME/static>
          Require all granted
        </Directory>
        WSGIScriptAlias / installdir/projects/APPNAME/APPNAME/wsgi.py
        <Directory installdir/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 installdir/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('installdir/apps/django/django_projects/PROJECT')
      os.environ.setdefault("PYTHON_EGG_CACHE", "installdir/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 installdir/apps/django/django_projects/PROJECT/ directory.

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

      $ touch installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf
      $ touch installdir/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 installdir/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 "installdir/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 "installdir/apps/django/lib/pythonA.B/site-packages/Django-X.Y.Z-pyA.B.egg/django/contrib/admin/static"
      WSGIScriptAlias /PROJECT 'installdir/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 installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf file, add this code:

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

      Include "installdir/apps/django/django_projects/PROJECT/conf/httpd-prefix.conf"
    
  • Edit the installdir/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 = ['localhost', 'localhost', '127.0.0.1']
    
  • Restart the Apache server:

      $ sudo installdir/ctlscript.sh restart apache
    

You should now be able to browse to your project at http://localhost/PROJECT/APP.

Last modification June 16, 2021