Deploying a django project on nginx with gunicorn

Back when I started  working with django, one of the things that I found was sort of difficult to do was deploying a project, as suggested by the documentation, using apache and mod_wsgi (and previously with mod_python), at least when it came to serve several projects and its media at the same server. Doing all those things with nginx and gunicorn is shockingly easy.

To add a new site on nginx I just touch a new file in the /etc/nginx/sites-enabled, with the contents:

server {
    listen 80;

    location / {

Now, supposing I want to have acces and error logs, and serve my site’s and the admin site’s media, I would add some lines to the server:

access_log /var/www/django/myproject/access.log;
error_log /var/www/django/myproject/error.log;

location /media {
    root /var/www/django/myproject;

location /admin_media {
    alias /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media;

Where /media and /admin_media are the MEDIA_URL and ADMIN_MEDIA_PREFIX pointed by my That is, I have a media folder in my project root directory and the admin media is served directly from the django installation.

To actually run my django site on I create a gunicorn configuration file in my project root (where is located):

bind = ""
workers = 3

Assuming I named the file, gunicorn is run from the same directory doing:

gunicorn_django -c -D

And that’s it.

When the code is updated, there’s no need to restart nginx; instead, gunicorn is restarted (not so prettily) with:

kill -HUP <main gunicorn process id>

To serve multiple sites, just use differents ports (8888 in the example) in each one’s configuration file.


Hello World

Now, this is a proper first post for my blog.

print "Hello blogging world!"