The NGINX Unit has been released, and I feel that it can be used as an application server that runs WSGI for Python. For the time being, I tried to move Hello World.
Install ubuntu package version referring to here. https://github.com/nginx/unit#ubuntu-packages
And start
$ sudo service unitd start
Create a python application with wsgi. Just place a file called wsgi.py in the specified path and follow the wsgi style. You can make it with wsgi, so you can use any framework.
wsgi.py
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello World\n'.encode('utf-8')]
Instead of changing the placed file, it is used like notifying the setting with API, so describe the setting file and upload it to reflect the setting.
start.json
{
"applications": {
"hello": {
"type": "python",
"workers": 2,
"path": "/home/ubuntu",
"module": "wsgi"
}
},
"listeners": {
"*:8400": {
"application": "hello"
}
}
}
The control API hits the API on a unix socket. See also: https://github.com/nginx/unit#configuration
Upload the described configuration file with curl and reflect it.
$ sudo curl -X PUT -d @start.json --unix-socket /var/run/control.unit.sock http://localhost/
Or, ubuntu package installation has a script that does that, so it's easy to use.
$ sudo service unitd restoreconfig start.json
You can get the settings by requesting with GET.
$ sudo curl --unix-socket /var/run/control.unit.sock http://localhost/
{
"applications": {
"hello": {
"type": "python",
"workers": 2,
"path": "/home/ubuntu",
"module": "wsgi"
}
},
"listeners": {
"*:8400": {
"application": "hello"
}
}
}
Since unitd doesn't have a configuration file, it needs to be reflected every time it is restarted.
I set the application to work on port 8400, so I made an HTTP request with curl there and got the result.
$ curl http://localhost:8400/
Hello World
Compared to uWSGI etc., the functions are overwhelmingly small, but it may become multifunctional in the future. Will it be used in virtualenv / venv in the future?
Recommended Posts