4

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?

1 Answer 1

3

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 
3
  • 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? Commented May 15, 2012 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? Commented May 15, 2012 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. Commented May 16, 2012 at 5:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.