Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

194 lines (141 sloc) 7.665 kB
author description keywords license alias modified modified_by published title
name email
Use uWSGI to deploy Python application servers in conjunction with nginx.
Monday, May 9th, 2011
Wednesday, November 10th, 2010
WSGI using uWSGI and nginx on CentOS 5

The uWSGI server provides a non-FastCGI method for deploying Python applications with the nginx web server. In coordination with nginx, uWSGI offers great stability, flexibility, and performance. However, to deploy applications with uWSGI and nginx, you must compile nginx manually with the included uwsgi module.

Set the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you've followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Install uWSGI

Begin by issuing the following commands to update your system and install dependencies for uWSGI:

yum update
yum install python python-devel libxml2 libxml2-devel python-setuptools zlib-devel wget openssl-devel pcre pcre-devel sudo gcc make autoconf automake

Issue the following sequence of commands to download and compile uWSGI:

cd /opt/
tar -zxvf uwsgi-
mv uwsgi- uwsgi/
cd uwsgi/
python2.4 build

Issue the following command to create a dedicated system user to run the uwsgi processes:

useradd -M -r --shell /bin/sh --home-dir /opt/uwsgi uwsgi

Send the following sequence of commands to set the required file permissions:

chown -R uwsgi:uwsgi /opt/uwsgi
touch /var/log/uwsgi.log
chown uwsgi /var/log/uwsgi.log

Configure uWSGI

Issue the following command to download an init script to manage the uWSGI process, located at /etc/init.d/uwsgi:

cd /opt/
wget -O
mv /opt/ /etc/init.d/uwsgi
chmod +x /etc/init.d/uwsgi

Create an /etc/default/uwsgi file to specify specific settings for your Python application. The MODULE specifies the name of the Python module that contains your wsgi specification. Consider the following example:

{: .file-excerpt } /etc/default/uwsgi : ~~~ bash PYTHONPATH=/srv/www/ MODULE=wsgi_configuration_module ~~~

If you want to deploy a "Hello World" application, insert the following code into the /srv/www/ file:

{: .file } /srv/www/ : ~~~ python import os import sys


os.environ['PYTHON_EGG_CACHE'] = '/srv/www/'

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

Issue the following commands to make this init script executable, ensure that uWSGI is restarted following the next reboot sequence, and start the service:

chkconfig --add uwsgi
chkconfig uwsgi on
/etc/init.d/uwsgi start 

Install nginx

Issue the following commands to install and configure the nginx web server from the EPEL Repository:

su -c 'rpm -Uvh'
yum update
yum install nginx 
chkconfig nginx on
/etc/init.d/nginx start 

During this process you will be asked to confirm importing the EPEL GPG key. Confirm this option to successfully complete the installation process.

Configure nginx Server

Create an nginx server configuration that resembles the following for the site where the uWSGI app will be accessible:

{: .file-excerpt } nginx virtual host configuration : ~~~ nginx server { listen 80; server_name; access_log /srv/www/; error_log /srv/www/;

    location / {
        include        uwsgi_params;

    location /static {
        root   /srv/www/;
        index  index.html index.htm;

All requests to URLs ending in /static will be served directly from the /srv/www/ directory. Restart the web server by issuing the following command:

/etc/init.d/nginx restart

Additional Application Servers

If the Python application you've deployed requires more application resources than a single Linode instance can provide, all of the methods for deploying a uWSGI application server are easily scaled to rely on multiple uSWGI instances that run on additional Linodes with the request load balanced using nginx's upstream capability. See our documentation of proxy and software load balancing with nginx for more information. For a basic example configuration, see the following example:

{: .file-excerpt } nginx configuration : ~~~ nginx upstream uwsgicluster { server; server; server; server; server; }

server {
    listen   80;
    access_log /srv/www/;
    error_log /srv/www/;

    location / {
        include        uwsgi_params;
        uwsgi_pass     uwsgicluster;

    location /static {
        root   /srv/www/;
        index  index.html index.htm;

In this example, we create the uwsgicluster upstream, which has five components. One runs on the local interface, and four run on the local network interface of distinct Linodes (the 192.168. addresses or the private "back end" network). The application servers that run on those dedicated application servers are identical to the application servers described above. However, the application server process must be configured to bind to the appropriate network interface to be capable of responding to requests.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Jump to Line
Something went wrong with that request. Please try again.