nginx uwsgi flask
Nginx uswgi Python
How to run python web application in web server.
We can run web app under python but, it doesn’t enough for production use.
I tried to prepare python web app env using nginx
Test env is following
- Ubuntu 15.10
- nginx 1.9.3
- python 3.4
Finally, I introduce two ways(reverse proxy, and uWSGI protocol)
Install nginx
sudo apt-get install nginx
To check installation,
nginx -v
Install pip and uwsgi
sudo apt-get install python-pip3 pip3 install uwsgi
Please install python version uwsgi, not distribution version
Install location is /home/kiririn/.local/bin/uswgi
kiririn is username
uWSGI is a kind of proxy server. uWSGI support several programming language,
python, etc…
uWSGI uses uWSGI, http, FastCGI and communicates with webserver
In this time, we tried to communicate with nginx
Check uSWGI
uWSGI works with standalone
Let’s try.
Prepare sample python codes,(this works as web process)
main.py
def application(environ,start_response): status = '200 OK' output = b'Hello World!' response_headers = [('Content-type','text/plain'), ('Content-Length',str(len(output)))] start_response(status,response_header) return [output]
This codes show Hello world in browser.
Use following
/home/kiririn/.local/bin/uwsgi --http :9090 --wsgi-file main.py
uWSGI(http) + nginx reverse proxy
One of ways is to use nginx reverse proxy.
Change nginx setting. /etc/nginx
nginx port is 8888, set reverse proxy setting in
Add following to /etc/nginx/sites-available/default
server { listen 8888; location / { include /etc/nginx/uwsgi_params; proxy_pass http://127.0.0.1:3031; } }
nginx supports uwsgi, it has uwsgi_params file under /etc/nginx
We need to load this file.
Start wsgi. port is 3031
/home/kiririn/.local/bin/uwsgi --http :3031 --wsgi-file main.py
It’s simple setting. just proxy, 3031 is already use by uwsgi
just works as proxy
We can access http://localhost:8888
uWSGI(uWSGI protocol) + nginx
In above case, we use 3031 and 8888 port.
To use uWSGI protocol, we use just only 8888.
uSWGI and nginx communicate by unix socket
/home/kiririn/.local/bin/uwsgi --socket /tmp/uwsgi.sock --wsgi-file main.py --chmod-socket=666
I added chmod-socket. This is for permission problem.
Add following description in /etc/nginx/sites-available/default
server { listen 8888; location / { include /etc/nginx/uwsgi_params; uwsgi_pass unix:///tmp/uwsgi.sock; } }
Access http://localhost:8888, it will work
Nginx + Flask
Let’s create simple flask app.
Install flask
pip3 install flask
Prepare app
I create app under /home/kiririn/www/flask
kiririn is user name.
The file name is main.py
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'Hello Bob!'
Start uWSGI
/home/kiririn/.local/bin/uwsgi --socket /tmp/uswgi.sock --wsgi-file main.py main.py --callable app --chmod-socket=666
Access and test
Access http://localhost:8888
You can see Hello Bob!
Ref
The uWSGI project
Flask + uWSGI + Nginx でハローワールドするまで @ さくらのVPS (CentOS 6.6)
http://ganeza.hatenablog.com/entry/2014/03/04/Python%2BuWSGI%2Bnginx%2Bflask%E3%82%92%E8%A9%A6%E3%81%97%E3%81%A6%E3%81%BF%E3%82%8B