Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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
share|improve this answer
    
Thanks for this! This is just what I've been looking for. However, I've followed these instructions from a fresh install and now when I visit the root of my site with a browser I get a '502: Bad Gateway' nginx error. I'm using Amazon EC2 with Ubuntu 11.10: would this require further configuration? –  benwad May 15 '12 at 13:33
    
Update: nginx's error.log said connect() to unix:///tmp/uwsgi-myapp.sock failed: No such file or directory. Does this mean that the socket isn't being created? If so, why not? –  benwad May 15 '12 at 14:06
    
Try listing the contents of /tmp (ls /tmp) after you restart uwsgi. If it's not there then check the uswsgi logs to see what's up. –  KPthunder May 16 '12 at 5:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.