Try running a Django application on an nginx unit

Introduction

This article is JSL (Japan System Giken) Advent Calendar 2020 --Qiita 24th day article.

In the Advent Calendar Article the other day, I wrote about nginx unit, which I was more interested in than before because I had achieved the quota once a year but wanted to challenge the record.

What is nginx unit?

Roughly speaking, it has the following features (Official page).

This goal

Do not write

Various versions

Installation

Proceed while checking the Official Page.

Create /etc/yum.repos.d/unit.repo and add the repository.

[unit]
name=unit repo
baseurl=https://packages.nginx.org/unit/amzn2/$releasever/$basearch/
gpgcheck=0
enabled=1
#installation of nginx unit
$ sudo yum install unit 

#Install the required modules
$ sudo yum install unit-devel unit-python37

#Auto start settings
$ sudo systemctl enable unit

#Start nginx unit
$ sudo systemctl start unit

#You can check the default value of each setting with the help option.
$ unitd --help 

unit options:

  --version            print unit version and configure options

  --no-daemon          run unit in non-daemon mode

  --control ADDRESS    set address of control API socket
                       default: "unix:/var/run/unit/control.sock"

  --pid FILE           set pid filename
                       default: "/var/run/unit/unit.pid"

  --log FILE           set log filename
                       default: "/var/log/unit/unit.log"

  --modules DIRECTORY  set modules directory name
                       default: "/usr/lib64/unit/modules"

  --state DIRECTORY    set state directory name
                       default: "/var/lib/unit"

  --tmp DIRECTORY      set tmp directory name
                       default: "/var/tmp"

  --user USER          set non-privileged processes to run as specified user
                       default: "nobody"

  --group GROUP        set non-privileged processes to run as specified group
                       default: user's primary group

Checking the environment

nginx unit checks and configures the environment via REST API.

$ sudo curl --unix-socket /var/run/unit/control.sock http://localhost/
{
	"certificates": {},
	"config": {
		"listeners": {},
		"applications": {}
	}
}

Currently, nothing is set.

Deploy a Django application

Deploy your Django application as well, following the official page (http://unit.nginx.org/howto/django/). Create a setting file (config.json) assuming that the deployment destination is under / var/www. I was worried about the storage location of config.json, but this time I put it under the Django application.

config.json


{
    "listeners": {
        "*:80": {
            "pass": "routes"
        }
    },
    "routes": [
        {
            "match": {
                "uri": "/static/*"
            },

            "action": {
                "share": "/var/www/my_djangoapp/"
            }
        },
        {
            "action": {
                "pass": "applications/django"
            }
        }
    ],
    "applications": {
        "django": {
            "type": "python 3.7",
            "user": "ec2-user",
            "group": "ec2-user",
            "path": "/var/www/my_djangoapp/",
            "home": "/var/www/my_djangoapp/env/",
            "module": "my_djangoapp.wsgi",
        }
    }
}

Apply config.json as follows.

$ sudo curl -X PUT --data-binary @config.json --unix-socket /var/run/unit/control.sock http://localhost/config/
{
	"success": "Reconfiguration done."
}

This completes the nginx unit settings. It becomes a Django application migrate, and it is necessary to prepare the environment by doing collect static.

Setting points

Impressions

Recommended Posts

Try running a Django application on an nginx unit
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Consider a cloud-native WebSocket application running on AWS
(Python) Try to develop a web application using Django
How to deploy a Django application on Alibaba Cloud
Deploy a Django application on Google App Engine (Python3)
Try running a Schedule to start and stop an instance on AWS Lambda (Python)
Rename an existing Django application
Try running python in a Django environment created with pipenv
Try running tensorflow on Docker + anaconda
Deploy a Django application with Docker
Deploy your Django application on Heroku
Implement a Django app on Hy
Try Ajax on the Django page
Build a web application with Django
Try running Discord Bot on an iPhone you no longer use
Update applications running on Django 1.7 to Django 1.8
Launched a web application on AWS with django and changed jobs
Try running Jupyter Notebook on Mac
Try running Python's web framework Bottle on a lollipop server (lollipo plan)
Migrate Django applications running on Python 2.7 to Python 3.5
Try running PlaidML image judgment on Mac
A note on enabling PostgreSQL with Django
Try running Kobuki's 3D simulator on ROS
I made a WEB application with Django
Run python wsgi server on NGINX Unit
How to run a Django application on a Docker container (development and production environment)
Try running a "newly infected number visualization app" using Streamlit on Amazon EC2
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation
Try Tensorflow with a GPU instance on AWS
Build a Django environment on Raspberry Pi (MySQL)
Try using a QR code on a Raspberry Pi
Deploy Django apps on Ubuntu + Nginx + MySQL (Build)
Run Python web apps on NGINX + NGINX Unit + Flask
Try running Distributed TensorFlow on Google Cloud Platform
A story about running Python on PHP on Heroku
Try to create a new command on linux
[Note] Measure coverage when running Django unit tests
Create a one-file hello world application with django
Beginners try to make an online battle Othello web application with Django + React + Bootstrap (1)
Put an index on a column that causes sushi beer problems with Django1.7 + MySQL