googledjango

Deploy a Django project

NOTE: Bitnami documentation usually assumes that server console commands are executed under the bitnami user account. However, when connecting through a browser SSH console, you may be logged in under a different user account. To switch to the bitnami user account, use the command sudo su - bitnami.

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

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/apache/conf/vhosts/sample-vhost.conf.disabled /opt/bitnami/apache/conf/vhosts/sample-vhost.conf
      $ sudo cp /opt/bitnami/apache/conf/vhosts/sample-https-vhost.conf.disabled /opt/bitnami/apache/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/apache/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/apache/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/apache/conf/bitnami/certs/server.crt"
        SSLCertificateKeyFile "/opt/bitnami/apache/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
    
Last modification February 9, 2023