Flask mit nginx und uWSGI bereitstellen

Installieren:
nginx
python-virtualenv
build-essential
python-dev
python

sudo apt-get install python-virtualenv build-essential python python-dev
sudo mkdir /var/www/demoapp

cd /var/www/demoapp
virtualenv venv
. venv/bin/activate

flask installieren:

pip install flask

uwsgi installieren:

pip install uwsgi

nginx config:

server {
    listen      80; #443
    server_name myapp.com;
    charset     utf-8;
    client_max_body_size 75M;

    location / { try_files $uri @myapp; }
    location @myapp {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/demoapp/demoapp_uwsgi.sock;
    }
   #alternnative locatio
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/demoapp/demoapp_uwsgi.sock;
    }
}
nginx -t
service nginx reload

uWSGI config:

/var/www/demoapp/demoapp_uwsgi.ini:

[uwsgi]
#application's base folder
base = /var/www/demoapp

#python module to import, start/setup file
app = myapp
module = %(app)

home = %(base)/venv
pythonpath = %(base)

#socket file's location
socket = /var/www/demoapp/%n.sock

#permissions for the socket file
chmod-socket    = 666
# security
#chmod-socket    = 644

#the variable that holds a flask application inside the module
callable = app

#location of log files
logto = /var/log/uwsgi/%n.log

uWSGI Emperor

/etc/init/uwsgi.conf

description "uWSGI"
start on runlevel [2345]
stop on runlevel [06]
respawn

env UWSGI=/var/www/demoapp/venv/bin/uwsgi
env LOGTO=/var/log/uwsgi/emperor.log

exec $UWSGI --master --emperor /etc/uwsgi/vassals --die-on-term --uid www-data --gid www-data --logto $LOGTO

config dateien nach vassals verlinken –> /etc/uwsgi/vassals

sudo mkdir /etc/uwsgi && sudo mkdir /etc/uwsgi/vassals
sudo ln -s /var/www/demoapp/demoapp_uwsgi.ini /etc/uwsgi/vassals

uWSGI starten:

sudo start uwsgi

Statische Dateien:

location /static {
    root /var/www/demoapp/;
}

https://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04