Saturday, June 11, 2016

Nginx Gunicorn Flask stack

I followed this wonderful guide to set up my flask + gunicorn + nginx application jjjdddfff.com on digital ocean. If you want to do the same, you should probably go there. Here are some additional notes that I took for myself.

What is the stack made of?

nginx is your http server. You tell the nginx server what port to listen to and where to pass requests. It can handle some requests on its own, like serving static resources like images, or redirects from one url to another. If it doesn't handle a request on its own, it can pass it to your app server, which in my case is gunicorn.

gunicorn is your python app server. It translates http requests into wsgi (web server gateway interface) compatible requests, which are forwarded to whatever will process the request.

flask is a python web framework. It will contain all the business logic of creating a response for a given request.

An alternative stack might be something like apache/uWsgi/django. Because it's all new to me, so I can't say with any authority which is better. I just needed to start somewhere, and after googling/reading a bit, I settled on this stack. At the worst, I think all of the pieces are plug and play. If I am unhappy with any piece of the puzzle, I don't think it will be too much trouble to switch out pieces of the stack. Right?

What do you need to do

You should look to the guide for the explicit steps, but I think it boils down to the following. First, install the software you need (nginx and virtualenv) and then within a virtual envinronment (gunicorn and flask). Then you need to write a couple of scripts.

Configure a web server (nginx)

In script, /etc/nginx/sites-enabled/homepage, I set up my web server (nginx) to listen to http requests and pass them to our app server (gunicorn) via a sock file.

server {
    listen 80;
    server_name jjjdddfff.com;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/username/homepage/homepage.sock;
    }
}
server {
    server_name www.jjjdddfff.com;
    return 301 $scheme://jjjdddfff.com$request_uri;
}

Set up application server (gunicorn)

In script /etc/init/homepage.conf, I created an upstart file that tells ubuntu to start your web application (homepage) that is bound to that "sock" file used in the web server.

description "Gunicorn application server running homepage"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid username
setgid www-data

script
    cd /home/username/homepage
    . venv/bin/activate
    gunicorn --workers 1 --bind unix:homepage.sock -m 007 homepage
end script

Create a web application (flask)

In script, /home/username/homepage/homepage.py, I set up a flask application which will handle all the business logic of processing requests.

from flask import Flask
application = Flask(__name__)

@application.route("/")
def home():
    return "Hello world"

if __name__ == "__main__":
    application.run(host='0.0.0.0')

Some helpful commands

Test the web server.

sudo nginx -t
sudo service nginx restart

Test the application server.

sudo stop homepage
sudo start homepage

No comments:

Post a Comment