Deploy a Django project
NOTE: If you are using Django 1.x, you need to replace the python3 command with python.
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
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](/google/faq/administration/use-firewall/ 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.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.
Database configuration
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'
}
}