Deploy django applications with nginx, uwsgi, virtualenv, south, git and fabric, part 3

This is the third part of the django deploy environment, you may find the second part here.

In this part we'll see how to set up nginx and uwsgi to work with our django project.

Let's install and configure uwsgi

Be sure to have the virtualenv activated (see part 2), and then install uwsgi

$ pip install uwsgi

Ok now we have to configure our project to work with uwsgi, open and edit the myproject/myproject/wsgi.py file, mine looks like this:

"""
WSGI config for sic2012 project.

This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.

Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.

"""
import os
import sys
import site

site.addsitedir('/home/user/virtualenv/myproject/lib/python2.7/site-packages')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)

The bold lines are the ones added to the default wsgi file in order to overlay the virtual environment for the application on top of the baseline environment. Such lines are not required if the virtualenv stays inside the project container folder!

Then we may start uwsgi but may be useful to create an ini file to set some options, I've such ini file named uwsgi.ini in the myproject (container) folder (at the level of manage.py) and looks like this:

[uwsgi]
module=myproject.wsgi:application
socket=/tmp/uwsgi_myproject.sock
#master=False
master=True
pidfile=/tmp/project-master_myproject.pid
vacuum=True
max-requests=5000
daemonize=/tmp/myproject.log

Here we say that the wsgi module is the one edited just above,  notice some things:

  • we have to launch uwsgi always from inside the myproject folder since here is defined the myproject.wsgi module
  • the socket, pidfile and daemonize have the project name inside, the reason is that if we have multiple django projects running we keep different files for them.
  • the line master=False is commented, because the first time we want uwsgi to start the master process, then when restarting the environment we'll kill the process stored in the pidfile and restart it but not the master process so we'll uncomment this line. We'll see this when talking about fabric and automation of deployment in remote (future posts).

It's time to start uwgi, in my environment I have to launch it as root in locale since as normal user it throws some permissions errors due to a call to unlink(), this is not good at all but not so a problem in locale, what is really important is not to start it as root in remote and this is not the case! So...(I'm in the myproject folder)

$ /path/to/virtualenv/bin/uwsgi --ini ./uwsgi.ini

You may check if there were problems by looking at the /tmp/myproject.log file

Set up a nginx virtual host

We've seen in the first part how to install nginx, now it's time to set up a virtual host pointing to our web application in locale.

Generally I create a new host name so edit the /etc/hosts file and add this line:

127.0.0.1 localhost
127.0.0.2 myproject.localhost

Ok, now it's time to add the virtual host configuration, so let's create a nw file myproject in the nginx site-availables folder, in my case /usr/local/nginx/sites-available/myproject . This file looks like this:

# file: /etc/nginx/sites-available/myproject
# nginx configuration for myproject

server {
  server_name myproject.localhost;
  access_log /usr/local/nginx/logs/access.log;
  error_log /usr/local/nginx/logs/error.log;
  location /static {
    root /var/www/myproject; # adjust to fit your path here
  }
  location /media {
    root /var/www/myproject; # adjust to fit your path here
  }
  location / {
    uwsgi_pass unix:/tmp/uwsgi_myproject.sock;
    include /usr/local/nginx/conf/uwsgi_params;
  }
}

Now enable it:

$ ln -s /usr/local/nginx/site-availables/myproject /usr/local/nginx/sites-enabled/myproject

And start or reload the server:

$ sudo /etc/init.d/nginx start

Now visit myproject.localhost in your browser and you should see your application.

Next

Continue reading part 4, how to freeze the project requirements, versioning and prepare all for the use of fabric.

Comments

post a comment

Friday 14 September 2012 23:41:10 - dr. who
gravatar

nice tutorial, thanks!

but unfortunately i only see the nginx start page when i visit: myproject.localhost.

i cant figure out what i have done wrong! do you have any idea?

thanks.

Wednesday 19 September 2012 15:29:54 - abidibo
gravatar

Thanks to you. Mmmmmm difficult to say with such few information, but seems like the virtual host is not properly configured and so the default is charged.
Have you symlinked the myproject virtual host file inside the site-enabled folder?
Check your nginx.conf, heve you got such line inside?
include /usr/local/nginx/sites-enabled/*;
Maybe a typo in this line?
server_name myproject.localhost;
Let me now if you solve the problem, bye!

Thursday 27 September 2012 16:06:26 - Dr. Who
gravatar

Aloha,
I found the error. I configured uwsgi to work with http and nginx to work with uwsgi. So there was no error in your tutorial. I'm new to this so it took a while ...

I will continue with fabric now and let you know if everything worked as intended.
Thank you you helped me to get into nginx & uwsgi!

Best regards from Berlin

Tuesday 15 January 2013 09:50:43 - Aswani kumar
gravatar

can you prepare a screencast for the same, so it will be really helpful. i got confused with sites direcory and virtualenv directory. im still trying to figure what went wrong.