NGINX Unit
v. 1.25.0

Django§

To run apps based on the Django framework using Unit:

  1. Install Unit with a Python 3 language module.

  2. Install and configure the Django framework. The official docs recommend setting up a virtual environment; if you do, list it as home when configuring Unit later. Here, it’s /path/to/venv/.

  3. Create a Django project. Here, we install it at /path/to/app/; use a real path in your configuration. The following steps assume your project uses basic directory structure:

    /path/to/app/
    |-- manage.py
    |-- django_app1/
    |   |-- ...
    |-- django_app2/
    |   |-- ...
    |-- project/
    |   |-- ...
    |   |-- asgi.py
    |   `-- wsgi.py
    `-- static/
    
  4. Run the following command so Unit can access the application directory:

    # chown -R unit:unit /path/to/app/
    

    Note

    The unit:unit user-group pair is available only with official packages, Docker images, and some third-party repos. Otherwise, account names may differ; run the ps aux | grep unitd command to be sure.

    For further details, including permissions, see the security checklist.

  5. Next, prepare the Django configuration for Unit. Here, the /path/to/app/ directory is stored in the path option; the virtual environment is home; the WSGI or ASGI module in the project/ subdirectory is imported via module. If you reorder your directories, set up path, home, and module accordingly.

    You can also set up some environment variables that your project relies on, using the environment option. Finally, if your project uses Django’s static files, optionally add a route to serve them with Unit.

    Here’s an example (use real values for share, path, environment, module, and home):

    {
        "listeners": {
            "*:80": {
                "pass": "routes"
            }
        },
    
        "routes": [
            {
                "match": {
                    "uri": "/static/*"
                },
    
                "action": {
                    "share": "/path/to/app/"
                }
            },
            {
                "action": {
                    "pass": "applications/django"
                }
            }
        ],
    
        "applications": {
            "django": {
                "type": "python 3.X",
                "path": "/path/to/app/",
                "home": "/path/to/venv/",
                "module": "project.wsgi",
                "environment": {
                    "DJANGO_SETTINGS_MODULE": "project.settings",
                    "DB_ENGINE": "django.db.backends.postgresql",
                    "DB_NAME": "project",
                    "DB_HOST": "127.0.0.1",
                    "DB_PORT": "5432"
                }
            }
        }
    }
    

    Note

    ASGI requires Python 3.5+ and Django 3.0+.

    {
        "listeners": {
            "*:80": {
                "pass": "routes"
            }
        },
    
        "routes": [
            {
                "match": {
                    "uri": "/static/*"
                },
    
                "action": {
                    "share": "/path/to/app/"
                }
            },
            {
                "action": {
                    "pass": "applications/django"
                }
            }
        ],
    
        "applications": {
            "django": {
                "type": "python 3.X",
                "path": "/path/to/app/",
                "home": "/path/to/venv/",
                "module": "project.asgi",
                "environment": {
                    "DJANGO_SETTINGS_MODULE": "project.settings",
                    "DB_ENGINE": "django.db.backends.postgresql",
                    "DB_NAME": "project",
                    "DB_HOST": "127.0.0.1",
                    "DB_PORT": "5432"
                }
            }
        }
    }
    
  6. Upload the updated configuration. Assuming the JSON above was added to config.json:

    # curl -X PUT --data-binary @config.json --unix-socket \
           /path/to/control.unit.sock http://localhost/config/
    

    Note

    The control socket path may vary; run unitd -h or see Startup and Shutdown for details.

    After a successful update, your project and apps should be available on the listener’s IP address and port:

    Django on Unit - Admin Login Screen