Django Channels

To run Django apps using the Django Channels framework with Unit:

  1. Install Unit with a Python 3.6+ language module.

  2. Install and configure the Django 3.0+ 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. Install Django Channels in your virtual environment:

    $ cd /path/to/venv/ # Path to the virtual environment; use a real path in your configuration
    
    $ source bin/activate
    
    $ pip install channels
    
    $ deactivate
    
  4. Create a Django project. Here, we’ll use the tutorial chat app, installing 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/  # Project directory
    |-- manage.py
    |-- chat/  # Individual app directory
    |   |-- ...
    |-- mysite/  # Project subdirectory
    |   |-- ...
    |   `-- asgi.py  # ASGI application module
    `-- static/  # Static files subdirectory
    
  5. Change ownership:

    Run the following command (as root) so Unit can access the application directory (If the application uses several directories, run the command for each one):

    # chown -R unit:unit /path/to/app/  # User and group that Unit's router runs as by default
       
    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.

  6. Integrate Django Channels into your project according to the official Channels guide.

  7. Next, create the Django Channels configuration for Unit. Here, the /path/to/app/ directory is stored in the path option; the virtual environment is home; the ASGI module in the mysite/ 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$uri",
             "share_comment": "Serves static files. Thus, URIs starting with /static/ are served from /path/to/app/static/; use a real path in your configuration"
             }
          },
          {
             "action": {
             "pass": "applications/djangochannels"
             }
          }
       ],
       "applications": {
          "djangochannels": {
             "type": "python 3.X",
             "type_comment": "Must match language module version and virtual environment version",
             "path": "/path/to/app/",
             "path_comment": "Project directory; use a real path in your configuration",
             "home": "/path/to/venv/",
             "home_comment": "Virtual environment directory; use a real path in your configuration",
             "module": "mysite.asgi",
             "module_comment": "Note the qualified name of the ASGI module; use a real site directory name in your configuration",
             "environment": {
             "DJANGO_SETTINGS_MODULE": "mysite.settings"
             },
             "environment_comment": "App-specific environment variables"
          }
       }
    }
    
  8. Upload the updated configuration:

Assuming the JSON above was added to config.json. Run the following command as root:

# curl -X PUT --data-binary @config.json --unix-socket \
      /path/to/control.unit.sock \  # Path to Unit's control socket in your installation
      http://localhost/config/      # Path to the config section in Unit's control API
   
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 (here, a chat) run on the listener’s IP address and port:

Django Channels on Unit - Tutorial App Screen


Last modified February 6, 2025