– Kurze Zusammenfassung, ausführlich siehe Quelle am Ende des Artikels –
Python Virtual Environment aufsetzen
pip install virtualenv virtualenv projekt source projekt/bin/activate pip install uwsgi flask
Mini Flask Applikation projekt.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello world!"
WSGI Startfile wsgi.py
from projekt import app if __name__ == "__main__": app.run()
Virtual Environment verlassen
deactivate
uWSGI Configfile erstellen
[uwsgi] module = wsgi:app master = true processes = 5 socket = /opt/projekt/projekt.sock chmod-socket = 660 vacuum = true die-on-term = true
systemd File erstellen /etc/systemd/system/projekt.service
[Unit] Description=uWSGI instance to serve projekt After=network.target [Service] User=projekt Group=www-data WorkingDirectory=/opt/projekt Environment="PATH=/opt/projekt/bin" ExecStart=/opt/projekt/bin/uwsgi --ini projekt.ini [Install] WantedBy=multi-user.target
uWSGI Projekt Service starten
sudo systemctl start projekt sudo systemctl enable projekt
NGINX Config erstellen – /etc/nginx/sites-available/projekt
server { listen 80; server_name 10.10.10.10; location / { include uwsgi_params; uwsgi_pass unix:/opt/projekt/projekt.sock; } }
NGINX Config aktivieren
ln -s /etc/nginx/sites-available/projekt /etc/nginx/sites-enabled systemctl restart nginx
Verifizieren NGINX und uWSGI
dev@dev1:~$ ps aux | grep projekt projekt 17175 0.0 2.1 71216 21812 ? Ss 19:26 0:00 /opt/projekt/bin/uwsgi --ini projekt.ini projekt 17178 0.0 1.7 71216 17316 ? S 19:26 0:00 /opt/projekt/bin/uwsgi --ini projekt.ini projekt 17179 0.0 1.7 71216 17316 ? S 19:26 0:00 /opt/projekt/bin/uwsgi --ini projekt.ini projekt 17180 0.0 1.6 71216 16504 ? S 19:26 0:00 /opt/projekt/bin/uwsgi --ini projekt.ini projekt 17181 0.0 1.7 71472 17848 ? S 19:26 0:00 /opt/projekt/bin/uwsgi --ini projekt.ini projekt 17182 0.0 1.6 71216 16504 ? S 19:26 0:00 /opt/projekt/bin/uwsgi --ini projekt.ini dev@dev1:~$ tail -f /var/log/syslog Jun 19 19:44:42 dev1 uwsgi[17175]: [pid: 17178|app: 0|req: 1/3] 10.10.10.1 () {46 vars in 834 bytes} [Wed Jun 19 19:44:42 2018] GET / => generated 40 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 79 bytes (1 switches on core 0) Jun 19 19:44:43 dev1 uwsgi[17175]: [pid: 17179|app: 0|req: 1/4] 10.10.10.1 () {46 vars in 834 bytes} [Wed Jun 19 19:44:43 2018] GET / => generated 40 bytes in 4 msecs (HTTP/1.1 200) 2 headers in 79 bytes (2 switches on core 0) Jun 19 19:44:44 dev1 uwsgi[17175]: [pid: 17179|app: 0|req: 2/5] 10.10.10.1 () {46 vars in 834 bytes} [Wed Jun 19 19:44:44 2018] GET / => generated 40 bytes in 0 msecs (HTTP/1.1 200) 2 headers in 79 bytes (2 switches on core 0) Jun 19 19:44:45 dev1 uwsgi[17175]: [pid: 17179|app: 0|req: 3/6] 10.10.10.1 () {46 vars in 834 bytes} [Wed Jun 19 19:44:45 2018] GET / => generated 40 bytes in 0 msecs (HTTP/1.1 200) 2 headers in 79 bytes (2 switches on core 0)
Quelle / ausführlicher Artikel: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04