DevOps Zone is brought to you in partnership with:

I'm in my mid forties and living in East Devon, UK. I enjoy explaining technology without all the buzz words and jargon. Col is a DZone MVB and is not an employee of DZone and has posted 13 posts at DZone. You can read more from them at their website. View Full User Profile

Nginx & uWSGI Living Together in Your Shed

01.24.2013
| 2224 views |
  • submit to reddit

Let's assume you have just bought the memorable domain name idrinkink.org and you want to host your lovely web app on that old server in the shed. 

I recently set up both Flask-Ahoy! and Django-Ahoy! on a five year old desktop my daughter was chucking out, so I can guarantee you that this configuration can handle enough traffic to get you going, or to demo products to your customers.

Before we start, I'm assuming you are familiar with python web frameworks (Django, Flask, Bottle etc) and with using virtualenv to segregate your environments. I usually use Flask as my framework of choice, so I'm using their "Hello World" app here.

Let's get started:

export CURRENT=i_drink_ink
mkvirtualenv ${CURRENT}
workon ${CURRENT}
cdvirtualenv
pip install Flask uwsgi
 
 
touch app.py   # the Flask app
touch uwsgi.sh # the uwsgi script that will expose the app
touch idrinkink.org.nginx.conf # the NGinX config to server the app

Now the contents of those files:

from flask import Flask
app = Flask(__name__)
 
@app.route("/")
def hello():
    return "Hello World!"

#!/usr/bin/env bash
 
VIRTUAL_ENV=i_drink_ink
 
${WORKON_HOME}/${VIRTUAL_ENV}/bin/uwsgi \
--socket /tmp/${VIRTUAL_ENV}.sock \
--chmod-socket \
--pythonpath ${WORKON_HOME}/${VIRTUAL_ENV} \
--module app:app \
--logdate \
--daemonize /tmp/uwsgi-${VIRTUAL_ENV}.log

server {
 
    listen 80;
    server_name www.idrinkink.org idrinkink.org;
 
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/i_drink_ink.sock;
    }
}

Finally copy the config into place and start the uwsgi daemon and the server up:

# copy nginx config into available slot
cp idrinkink.org.nginx.conf /etc/nginx/sites-available
 
# link it into the enable slot
ln -s /etc/nginx/sites-available/idrinkink.org.nginx.conf /etc/nginx/sites-enabled/idrinkink.org.nginx.conf
 
# start uwsgi
chmod 755
${WORKON_HOME}/${VIRTUAL_ENV}/bin/python ./uwsgi.sh
 
# restart the nginx service
sudo service nginx restart

This of course is just the basics, but it will hopefully give you a starting point from which you can fiddle to your hearts content.

Good luck fellow traveller.


Published at DZone with permission of Col Wilson, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)