Thursday, October 3, 2013

Flask + Gunicorn + Supervisor + Nginx

This morning, I deployed a Flask application in a Ubuntu 13.04 server with Nginx and Gunicorn. Here is the configuration:

0. My app code base (blog):

/projects/blog/

1. I created a shell script to run gunicorn:

/projects/blog/blogguni.sh
========================

#!/bin/bash

NUM_WORKERS=4
# user/group to run as
ADDRESS=127.0.0.1:5000
cd /projects/blog
/projects/.venv/blog/bin/gunicorn blog:app -w $NUM_WORKERS -b $ADDRESS


2. Supervisor configuration:

/etc/supervisor/conf.d/blogguni.conf
==========================

[program:blogguni]
directory = /projects/blog/flaskr/
user = dangtrinhnt
command = /projects/blog/blogguni.sh
stdout_logfile = /projects/logs/supervisor_blog.log
stderr_logfile = /projects/logs/supervisor_blog_error.log


3. Nginx configuration:

/etc/nginx/sites-available/blog
====================

upstream blog {
    server localhost:5000 fail_timeout=0;
}
server {
    listen 80;
    server_name dangtrinhnt.kd.io;
    access_log /projects/logs/access_blog.log;
    error_log /projects/logs/error_blog.log;
    location /static/ {
        alias /projects/blog/static/;
    }
    location / {
        proxy_pass http://blog;
    }
}


I used the Flaskr example from Flask project which you can find at https://github.com/mitsuhiko/flask/tree/master/examples/flaskr/


I deployed the blog in a free VM provided by Koding at https://koding.com/?r=dangtrinhnt
(you can get a free one too).

The blog is now live at http://dangtrinhnt.kd.io/ (only when I'm actively logging in Koding)

It's pretty cool!!!