Deployment of Applications Using uWSGI and Nginx

This post is outdated. See this post for more up-to-date directions.

It should be safe to assume that anyone remotely interested in such a topic must already have a fair understanding of what this post is about, so let’s not waste any time on introductions.

The environment is as usual a low-end VPS with Ubuntu 10.04 Lucid LTS (minimal version). First things first, if you have not already done this:

apt-get update
apt-get upgrade -y

for i in busybox-static \
         syslog-ng \
         python-setuptools \
         python-pip \
         python-software-properties \
         ; do
  apt-get install $i -yf

Then we install (0.35 at the time of this writing) itself using pip:

pip install

Next comes the servers:

add-apt-repository ppa:stevecrozz/ppa
apt-get update
apt-get install uwsgi nginx -yf

For this demonstration, we use the minimalist Hello World example from tutorial as our web application. Save the following as ‘/var/www/apps/’:

import web

urls = (
  '/', 'index'

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

class index:
        def GET(self):
                return "Hello, world!"

if __name__ == "__main__":

We use the built-in web server to test whether the above code is OK. In ‘/var/www/apps’ folder, type this:


Point your browser to IP:8080 and you should be greeted with ‘Hello, world!’. Press Ctrl-C to stop the built-in server.

Working upwards, next we set up uWSGI. We will be using uWSGI in VirtualHosting mode, so that more applications can be added later dynamically without having to change uWSGI configuration. Create ‘/etc/uwsgi.xml’ with the following contents


We then set up Nginx by creating a site file in /etc/nginx/sites-enabled with the following contents:

server {
  # Change this if you want to serve your application on another port
  listen 80;

  # Replace this with your domain name

  # You can use virtual directory like '/apps/' here, but remember that
  # you should matching 'urls' defined in your application file
  location / {
    include uwsgi_params;

    # This should match the 'socket' entry of your uwsgi.xml

    # This is the absolute path to the folder containing your application
    uwsgi_param UWSGI_CHDIR /var/www/apps;

    # This is actually not necessary for our simple application,
    # but you may need this in future
    uwsgi_param UWSGI_PYHOME /var/www/apps;

    # This is the name of your application file, minus the '.py' extension
    uwsgi_param UWSGI_SCRIPT index;

We are almost done, but there is one last and very important step: uWSGI requires a wsgi application to be defined in the application file ( in our case). Just add this line to the end of

application = app.wsgifunc()

Now that everything is in place, we can (re)start Nginx and uWSGI:

service nginx restart
service uwsgi restart

Point your browser to your domain and you will now be greeted by ‘Hello, world!’ served by Nginx through uWSGI. Job done.

This entry was posted in Linux, Web and tagged , , . Bookmark the permalink.

2 Responses to Deployment of Applications Using uWSGI and Nginx

  1. jyr says:

    I have a with

    import web
    urls = ("/", "index")
    app = web.application(urls, globals())
    class index:
        def GET(self):
            return 'Hello, world!'
    if __name__ == "__main__":
        application = app.wsgifunc()

    and return

    uWSGI Error
    wsgi application not found

    Any suggestions?

    • farter says:


      You are appending the ‘application = …’ line to the ‘if’ conditional statement, so it will only get executed when you run ‘python’ in console, but not when uWSGI is executing, hence ‘wsgi application not found’.