Skip to content

Instantly share code, notes, and snippets.

@KavenTheriault
Last active November 21, 2017 23:47
Show Gist options
  • Save KavenTheriault/7cc10dd519c61cea00acf33d26d34f17 to your computer and use it in GitHub Desktop.
Save KavenTheriault/7cc10dd519c61cea00acf33d26d34f17 to your computer and use it in GitHub Desktop.
uwsgi+nginx+flask on Debian 8

Host flask api in Nginx using UWSGI

In this exemple of configuration. All communications sent to localhost/api will be sent to the uwsgi socket.

Dependencies

$ apt-get install python python-pip build-essential python-dev uwsgi-plugin-python
$ pip install uwsgi

Directories

/etc/nginx/ (nginx)
/var/www/ (app)
/etc/uwsgi/apps-available/ (uwsgi)
/lib/systemd/system/ (service files location)

Steps

#create life_html in /etc/nginx/sites-available/

$ sudo ln -s /etc/nginx/sites-available/life_api /etc/nginx/sites-enabled/life_api
$ sudo ln -s /etc/nginx/sites-available/life_html /etc/nginx/sites-enabled/life_html

Start service command

sudo systemctl start uwsgi-app@life_api.service
[uwsgi]
master=true
module = api
callable = app
chdir = /var/www/life_api/
socket = /tmp/life_api.uwsgi.sock
virtualenv = /var/www/life_api/venv
logto = /var/www/life_api/log/%n.log
chmod-socket = 666
uid = www-data
gid = www-data
server {
listen 80;
server_name localhost;
root /var/www/life_html/;
error_log /var/www/life_html/log/error.log;
access_log /var/www/life_html/log/access.log;
location /api {
try_files $uri @context;
}
location @context {
include uwsgi_params;
uwsgi_pass unix:///tmp/life_api.uwsgi.sock;
}
}
[Unit]
Description=%i uWSGI app
After=network.target
[Service]
ExecStart=/usr/local/bin/uwsgi \
--ini /etc/uwsgi/apps-available/%i.ini
Restart=on-failure
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment