Over a million developers have joined DZone.
Platinum Partner

Nginx & uWSGI Living Together in Your Shed

· DevOps Zone

The DevOps Zone is brought to you in partnership with New Relic. Improving the performance of your app is easy with New Relic's SaaS-based monitoring.

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}
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__)
def hello():
    return "Hello World!"

#!/usr/bin/env bash
${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.

The DevOps Zone is brought to you in partnership with New Relic. Know exactly where and when bottlenecks are occurring within your application frameworks with New Relic APM.


Published at DZone with permission of Col Wilson , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}