Deploying Django

Extra group and user

Create a new group and user who will run the services:

sudo groupadd --system djangows
sudo useradd --system --gid djangows --shell /bin/bash --home /var/www/djangows djwsuser

Create the home directory stated in the above command and have it owned by the newly created user:

sudo mkdir -p /var/www/djangows
sudo chown djwsuser /var/www/djangows/

As this user install the Django application in a virtual environment in her home directory:

sudo su - djwsuser
virtualenv -p python3 <django_application_name>

In order to install the EOA Publication Platform there, consult Installation with Virtual Environment.

Nginx and Gunicorn

This is based on the tutorial from Digitalocean and uses Gunicorn and Nginx as in the example, but SQLite as the database.

Install nginx

Simply install nginx with a package manager. On Debian Linux, for example:

sudo apt-get install nginx

Install gunicorn in the virtual environment

Gunicorn is a Python WSGI HTTP Server and natively supports Django applications. Activate the virtual environment and install it with:

pip install gunicorn

You can test the functionality by starting gunicorn with:

gunicorn eoapp.wsgi:application --bind 0.0.0.0:8000

Startup scripts

Create a service file for the Django application called gunicorn.service and copy it to the directory /etc/systemd/system/. An example file is at https://github.molgen.mpg.de/EditionOpenAccess/eoa-utilities/blob/master/gunicorn.service. Register the service with:

systemctl daemon-reload

Start the service with:

sudo systemctl start gunicorn.service

To enable at startup:

systemctl enable gunicorn.service

Create virtual host in nginx

Create a new site called eoapp in /etc/nginx/sites-available:

server {
   listen 80;
      server_name localhost;

   location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                root /var/www/djangows/eoapp_ve/eoapp;
                        }

   location / {
          include proxy_params;
                  proxy_pass http://unix:/var/www/djangows/eoapp_ve/run/eoapp.sock;
                      }
   }

Disable the default site, enable the site and restart nginx:

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/eoapp /etc/nginx/sites-enabled/
sudo service nginx restart

Ready for production

As final steps, to prepare the system for production, open the settings.py file and set:

Debug=False

and check if the ALLOWED_HOSTS setting contains the addresses under which the application can be reached.

Finally, activate the virtual environment and collect the static files in the place where nginx will find them:

python manage.py collectstatic

Check the page https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ for further measures to secure the web service.

Administrating Django

In order to perform administrative tasks, change to the directory of the Django application, activate the virtual environment and go ahead.