Skip to main content

Nginx uWSGI Flask Python2

Flask + Virtual Env

Python 2.7 Virtual Env

pip2 install virtualenv
cd /home/user/web/domain
virtualenv --python=$(which python2) ./private/
source private/bin/activate
pip install -r ../public_html/requirements.txt

uWSGI Emperor

pip2 install wheel uwsgi

cat /etc/systemd/system/uwsgi.serviceĀ 

[Unit]
Description=uWSGI Emperor service
After=syslog.target

[Service]
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start uwsgi.service
systemctl status uwsgi.service
systemctl enable uwsgi.service

Application configuration file

cat web.domain.site.iniĀ 

[uwsgi]
uid=appuser
gid=www-data
socket=127.0.0.1:13000
procname-prefix-spaced = web.domain.site

# Enable uWSGI stats server
# stats = :13034
# stats-http = true

# the virtualenv (full path)
virtualenv=/home/appuser/web/web.domain.site/private
# Project path
chdir=/home/appuser/web/web.domain.site/public_html
# Django's wsgi file (path starting from chdir/)
wsgi-file=run.wsgi

# Log paths

logto=/var/log/nginx/domains/web.domain.site.uwsgi.log
log-date = [%%Y:%%m:%%d %%H:%%M:%%S]
logformat-strftime
log-format = [%(ftime)]
logfile-chown = www-data:appuser
logfile-chmod = 640

# Do not log some errors caused by client disconnects
ignore-sigpipe = true
ignore-write-errors = true
disable-write-exception = true


master=true
enable-threads=true
processes=2
threads=5
single-interpreter=true
thunder-lock=true
die-on-term=true
max-requests=1000
buffer-size=32768
post-buffering=8192

# clear environment on exit
vacuum=true

Nginx Virtualhost

https://uwsgi-docs.readthedocs.io/en/latest/Nginx.html

location / {
        include uwsgi_params;
        uwsgi_pass uwsgi://localhost:13000;
}

Ref: https://uwsgi-docs.readthedocs.io/en/latest/Systemd.html