abidibo.net

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

deploy django nginx programming wsgi

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.

Subscribe to abidibo.net!

If you want to stay up to date with new contents published on this blog, then just enter your email address, and you will receive blog updates! You can set you preferences and decide to receive emails only when articles are posted regarding a precise topic.

I promise, you'll never receive spam or advertising of any kind from this subscription, just content updates.

Subscribe to this blog

Comments are welcome!

blog comments powered by Disqus

Your Smartwatch Loves Tasker!

Your Smartwatch Loves Tasker!

Now available for purchase!

Featured