NGINX Unit

Django Apps§

To run your Django projects and apps in Unit:

  1. Install Unit with the appropriate Python language module version.

  2. If you haven’t already done so, create your Django project and apps where you usually store them.

  3. Prepare Unit configuration for your update. To obtain an initial template, query the control API:

    # curl --unix-socket /path/to/control.unit.sock \
           http://localhost/config/ > config.json
    

    Note

    Control socket path may vary; run unitd --help or see Startup for details.

    This creates a JSON file with Unit’s current settings; update it with your project settings as follows.

    Suppose you use a basic directory structure for your Django project:

    /home/django/project/
    |-- manage.py
    |-- app1/
    |   |-- ...
    |-- app2/
    |   |-- ...
    `-- project/
        |-- ...
        `-- wsgi.py
    

    Edit the JSON, adding a listener entry to point to a Unit app with your project’s WSGI module; the project and its apps will run on the listener’s IP and port. If you use a virtual environment, reference it as home:

    {
        "listeners": {
            "127.0.0.1:8080": {
                "pass": "applications/django_project"
            }
        },
    
        "applications": {
            "django_project": {
                "type": "python 3",
                "path": "/home/django/project/",
                "home": "/home/django/venv/",
                "module": "project.wsgi"
            }
        }
    }
    

    Note

    Mind that Unit will look for an application callable in the WSGI module to run the entire project.

    Here, the top-level project directory becomes path; its child project and the wsgi.py in it are imported via module. If you reorder your directories, set up path and module accordingly.

  4. Upload the updated configuration:

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

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

    $ curl 127.0.0.1:8080/admin/
    $ curl 127.0.0.1:8080/app1/