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.
Roughly speaking, it has the following features (Official page).
nginx unit
on EC2 (Amazon Linux2) and deploying the Django application.uWSGI
and Gunicorn
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
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 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
.
The name specified by type
in config.json specifies the module name of the directory specified by --modules
, but it seems that * blank * must be inserted between the versions.
The Django application directory must be properly owned with the values specified in user
and group
in config.json.
When operating a Web application that handles multiple languages and versions with microservices, I thought that it might be considered as one of the options because it seems that maintenance and operation can be performed only with nginx unit
.
The setting via REST API was fresh, but I couldn't find the syntax check function in the scope of investigation, and it was a little strict to check the error while checking the log file.
If you are assuming a Python-only web application, I thought that uWSGI
or Gunicorn
would be sufficient.
Recommended Posts