June 6, 2014 · python nginx uwsgi tips tricks code web virtualenv http socket

Setting up uwsgi + nginx. What you need to know!

Well I'm writing this to you after wasting 8 hours of my life debuging a setup of uswgi on ubuntu with upstart and nginx. What a painful, horrible task.

So I thought I'd save you the trouble and give you a almost foolproof set of configuration, tips and explanations so you can tell exactly how to get your stuff sorted.

This my friends is the sum knowledge of my pain and stackoverflow threads I have poured over.

Tip Time

Tip 1 - Use the right version!

I can't tell you the amount of pain I realised I'd gone through purely because I was using uwsgi 1.9.blah.blah rather than uwsgi 2.blah.blah.blah.

MAKE SURE YOU UPGRADE. The latest version allows you to see where its looking for your app and what the issue is. So if you've got an import error: you'll see it. If there's no module: you'll see it.

Tip 2 - http-socket vs socket

Something that really tripped me up was that some tutorials talk about using http-socket and others talk about using socket.

Here's the difference: http-socket means your using the http-protocol and socket means your using tcp. So if you're wanting to test that uwsgi is actually running from the browser, you'll need to use http-socket but if you're trying to hook it up to nginx, nginx sockets use tcp so you'll need just socket.

Look at that, I've just saved you hours of stackflow right there!

Tip 3 - virtualenv and your uwsgi version

If you're using python3.4 in a virtualenv for running your app, but your running uwsgi with the system installed python2.7 for instance: YOUR GONNA HAVE A BAD TIME.

So make sure you source bin/activate and pip install uwsgi so that you have a binary built on the version of python you're using in virtualenv.

PROTIP If you're running uwsgi from upstart, you can just reference the new binary's path: /path/to/your/virtualenv/bin/uswgi yourconfig.ini. No need to muck around with source bin/activate. If you try you will lose HOURS!

Tip 4 - virtualenv and uwsgi - part 2

Uwsgi config is a pain beyond all pains if your using virtual env.

Here's a couple example configs that will work wonders for your soul (don't bother with command line switches). This one is for local dev:

[uwsgi]
http-socket = 127.0.0.1:8081
pythonpath = /path/to/app/
module = mymodule
callable=app
stats = 127.0.0.1:9191
virtualenv = /path/to/your/virtualenv

Use this one for use with nginx (for some multi-process goodness):

[uwsgi]
socket = 127.0.0.1:8081
pythonpath = /path/to/app/
module = mymodule
processes = 4
threads = 2
callable=app
virtualenv = /path/to/your/virtualenv
uid=pythonwebuser

Bonus Tip! Example nginx and upstart config

Upstart /etc/init/uwsgi.conf

#simple uWSGI script

description "Just Tender Instance 2"
start on runlevel [2345]
stop on runlevel [06]

exec /path/to/virtualenv/bin/uwsgi /path/to/config/uwsgi.ini

Start and stop with:
service uwsgi start and service uwsgi stop

Nginx /etc/nginx/sites-enabled/example.conf

upstream uwsgi {
    server 127.0.0.1:8081;
    #server 127.0.0.1:8082;
}

server {
    listen 80;
    listen [::];
    server_name example.com;

    root /path/to/static/files;

    location @uwsgi {
            include uwsgi_params;
            uwsgi_pass uwsgi;
    }

    location / {
            try_files $uri @uwsgi;
    }
}

Summary

Well I hope that saved you some much needed time! Good luck on your pythonista adventures!

Feel free to follow me on twitter @jaitaiwan

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket
Comments powered by Disqus