Over a million developers have joined DZone.

Nginx & uWSGI Living Together in Your Shed

· DevOps Zone

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

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.


Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

Topics:

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.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}