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:
Issued the following commands:
Updated /etc/nginx/sites-available/default:
Restarted NGINX:
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: