For marketers
who love technology
Home » , , » Which server for deploying a scalable django powered web app? How to install nginx and gunicorn for django.

Which server for deploying a scalable django powered web app? How to install nginx and gunicorn for django.

Debian OpenLogo
Debian OpenLogo (Photo credit: Wikipedia)
English: Nginx Logo Español: Logo de Nginx
English: Nginx Logo Español: Logo de Nginx (Photo credit: Wikipedia)
This post explains how to install nginx and gunicorn for django under linux Debian.

From my readings, nginx should be used to serve static files. Nginx should proxy all other requests to gunicorn.

These two tools are easy to install on my debian squeeze:


sudo apt-get install python-software-properties -y
sudo apt-get install nginx
sudo apt-get remove apache2
sudo apt-get autoremove 
pip install gunicorn


Then we have to configure nginx. For this part, I relied on a the following tutorial.

sudo vim /etc/nginx/nginx.conf

I did not change this file as default configuration options seemed reasonable. At this stage, the 'it works page' should be served on localhost:

firefox http://127.0.0.1/ &

Then we have to tell nginx where the static files are located, and what URL to use to proxy other
requests to guincorn. As I use nginx for a single site, I edit directly the default configuration file:

sudo vim /etc/nginx/sites-enabled/default 

In this file, I remove all the defined "location" directives. And I add the ones related to my django app, to the static files of its admin page, and to the static files (/home/toto/myapp/app/static/*):

    location /static {
        root /home/toto/virtualenv_1/myapp/app/;
    }

    location /static/admin {
        root /home/toto/virtualenv_1/lib/python2.6/site-packages/django/contrib/admin/;
    }    location / {
        proxy_pass http://127.0.0.1:8888;
    }

Then, I restart nginx:

sudo /etc/init.d/nginx restart

If you have an error such as:

Starting nginx: [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
[emerg]: bind() to [::]:80 failed (98: Address already in use)

try using the following command to see which server runs already, and kill it:

sudo lsof -i tcp:80

 Finally, we configure gunicorn. It's incredibly easy.

First, we edit django's settings.py according to the documentation and add "gunicorn" to INSTALLED_APPS. Set the value of the STATIC* variables to '' to the URLs served by Nginx as the static files are all served from nginx. Modify also urls.py to remove the lines related to the static files. Then we simply launch the server:


./manage.py run_gunicorn
 
 
 
 
SHARE

About Gilles

0 comments :

Post a Comment