Run python wsgi server on NGINX Unit

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.

Tried environment

NGINX Unit installation

Install ubuntu package version referring to here. https://github.com/nginx/unit#ubuntu-packages

And start

$ sudo service unitd start

Creating a python application

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')]

Configuration file description

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"
		}
	}
}

Reflection of settings

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.

Confirmation of operation

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

Run python wsgi server on NGINX Unit
Run Python web apps on NGINX + NGINX Unit + Flask
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Run Openpose on Python (Windows)
Run Tensorflow 2.x on Python 3.7
Run Python CGI on CORESERVER
Run CGI written in python on Sakura's rental server
Run unix command on python
Until you install Python with pythonbrew and run Flask on a WSGI server
Python server settings (nginx + Gunicorn)
Install Python3 on Sakura server (FreeBSD)
Run Python on Schedule on AWS Lambda
Run TensorFlow Docker Image on Python3
Run TensorFlow2 on a VPS server
Periodically run Python on Heroku Scheduler
Run different versions of Python (wsgi) apps on one development environment
Run servo with Python on ESP32 (Windows)
[Python] Run Flask on Google App Engine
Build CGI Server running on Python 3 on Docker
Run AzureKinect in Python on Christmas Eve.
Run servomotor on Raspberry Pi 3 using python
Python execution server construction (Python + uWSGI + Django + Nginx)
[Python] Run Headless Chrome on AWS Lambda
Run Python code on A2019 Community Edition
Install Python3 on mixhost server [Currently NG]
Run Jupyter notebook on a remote server
Run Python in C ++ on Visual Studio 2017
Rock-paper-scissors with Python Let's run on a Windows local server for beginners
Install Python3 and Django on Amazon Linux (EC2) and run your web server
Periodically run a python program on AWS Lambda
[Note] Python environment construction on rental server "CORESERVER"
Install and run Python3.5 + NumPy + SciPy on Windows 10
Put MicroPython on Windows to run ESP32 on Python
Run the task in the background on the sshed server
twitter on python3
Run Python YOLOv3 in C ++ on Visual Studio 2017
How to run MeCab on Ubuntu 18.04 LTS Python
Hello World with nginx + uwsgi + python on EC2
Run Zookeeper x python (kazoo) on Mac OS X
python on mac
Python on Windbg
Building a Python environment on a Sakura VPS server
Run Laravel-admin on Golang PHP application server RoadRunner
Python unit tests
Run Python on Apache to view InfluxDB data
[CGI] Run the Python program on the server with Vue.js + axios and get the output data
Run Paints Chainer on CPU with official python on win10
Try running a Django application on an nginx unit
Stray build of Python 3 on Sakura's rental server (Note)
Install django on python + anaconda and start the server
Python conda on cygwin
Install python on WSL
python> udp> echo server
Run Python with VBA
PyOpenGL setup on Python 3
Install Python on Pidora.
Run prepDE.py with python3
Install Scrapy on python3
Run Django on PythonAnywhere
Install Python on Mac
Install Python 3 on Mac