Over a million developers have joined DZone.

Nginx & uWSGI Living Together in Your Shed

· DevOps Zone

The DevOps zone is brought to you in partnership with Sonatype Nexus. The Nexus suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

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 Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today


Published at DZone with permission of Col Wilson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}