Nginx – UWSGI and NGINX for Python Apps on Ubuntu 11.10

nginxpythonUbuntuuwsgiwsgi

I am trying to find an optimal way to set up my server to use NGINX and UWSGI to serve python applications. The following has worked so far:

Initial setup:

sudo apt-get install nginx uwsgi uwsgi-plugin-http uwsgi-plugin-python python-setuptools

easy_install pip

pip install web.py

/etc/nginx/sites-available/default:

server {
    listen 80;
    server_name localhost;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:9090;
    }
}

And then I have a basic myapp.py (location doesn't matter for the current setup):

import web

urls = (
    '/', 'index'
)

app = web.application(urls, globals())

class index:
    def GET(self):
        return "Hello from Web.py!"

application = app.wsgifunc()

Then I can issue the following commands and everything works:

sudo service nginx restart

uwsgi --plugins http,python -s 127.0.0.1:9090 myapp

So it works, but it's not very pretty. I noticed that when I installed UWSGI with apt-get that two directories were created: /etc/uwsgi/apps-available and /etc/uqsgi/apps-enabled. This matches the convention for debian servers running NGINX or Apache only with apps instead of sites.

Here is what would be awesome: I would like to be able to drop in application configurations to apps-available (creating symlinks in apps-enabled as needed) and have the UWSGI service pick them up. But I don't quite know where to start. What configuration files do I put in apps-available? And what does the NGINX configuration look like for passing to the uwsgi service rather than passing to the socket created by the command I issued earlier?

Best Answer

I got it working! Here's what I did:

Created /etc/uwsgi/apps-available/myapp.xml:

<uwsgi>
    <socket>/tmp/uwsgi-myapp.sock</socket>
    <plugins>http, python</plugins>
    <chdir>/path/to/directory/containing/python/app</chdir>
    <module>myapp</module><!-- myapp.py from before -->
</uwsgi>

Issued the following commands:

ln -s /etc/uwsgi/apps-available/myapp.xml /etc/uwsgi/apps-enabled/myapp.xml
sudo service uwsgi restart

Updated /etc/nginx/sites-available/default:

server {
    listen 80;
    server_name localhost;
    location / {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi-myapp.sock;
    }
}

Restarted NGINX:

sudo service nginx restart

And everything is golden! Obviously, the above is a very simple configuration and one should review the availalbe UWSGI and NGINX options before going to production.

What also worked is in the UWSGI configuration having <socket>127.0.0.1:9090</socket> and leaving the NGINX configuration as it was.

As a final note: UWSGI supports multiple formats for configuration: (INI, XML, and YAML). I had originally tried YAML but the server would fail to start so I tried XML and everything worked fine.

EDIT:

I just tried INI configuration and it worked as well. The equivilant INI file as the XML file above is as follows:

[uwsgi]
socket = /tmp/uwsgi-myapp.sock
plugins = http, pythong
chdir = /path/to/directory/containing/python/app
module = myapp