HHH
By:
HHH

Projects with different django version - virtualenv

February 4, 2015 1.4k views

So I have two website running on my droplet( Nginx + Gunicorn).
What If I want to install different django version on each project?
I created virtualvenv in each projects directories and installed succesfully different Django versions. But now what?

1 Answer

Using the Django One-Click app as an example of a fairly typical Django setup, Gunicorn is used to run the app while Nginx sits in front of it as a reverse proxy. Nginx can also be used to route requests between different domains by creating separate "server blocks" for each app.

Clipping somethings out for brevity, on the one-click the Nginx configuration looks like:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    server_name: my-domain.com

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

This takes the Django app that is available locally at port 9000 and makes it available publicly at port 80. So to run another app, we'd need to server it on a separate port and allow Nginx to route it accordingly. It might look something like:

upstream app_server_one {
    server 127.0.0.1:9000 fail_timeout=0;
}

upstream app_server_two {
    server 127.0.0.1:7000 fail_timeout=0;
}

server {
    listen 80 default_server;
    server_name: my-domain.com

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_one;
    }
}

server {
    listen 80 default_server;
    server_name: subproject.my-domain.com

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_two;
    }
}

The server_name directive is important here. Any requests made to subproject.my-domain.com will be directed to port 7000 while requests to my-domain.com will go to 9000.

The port that the app is running on local is determined by Gunicorn. On the one-click Guinicon is started and stopped with an Upstart script installed to /etc/init/gunicorn.conf As you are using a virtualenv, you'll need to adapt that script to use the Guinicon from inside the virtualenv. You can make a copy of that script updating the port and file paths for your second app.

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

exec venvs/webapp/bin/gunicorn \
    --name=django_project \
    --pythonpath=django_project \
    --bind=0.0.0.0:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    django_project.wsgi:application
by Justin Ellingwood
Nginx is an incredibly powerful web server and reverse proxy. In this guide, we'll discuss how to set up server blocks on Ubuntu 14.04 in order to server multiple domains from a single server.
  • If I reboot my droplet do I have to manually start gunicorn process?

    I was doing this tutorial - How to Install and configure django with postgres, nginx and gunicorn before you answered my question. And it says that I should use supervisor.

    by Stephen Tippets
    This tutorial runs through both installing and configuring Django with Postgres, Nginx, and Gunicorn on your VPS.
  • @HHH supervisor is one solution. I personally prefer to use the OS's native process management/init system. In the case of Ubuntu 14.04, that's Upstart. This allows you to start and stop processes with the normal service command, like: sudo service gunicorn restart It will also automatically start on boot.

    Though, using whatever you are most comfortable with is best.

  • I'm trying to use Upstart, but it throws this error:

    start: Unknown job: django-project

    and after init-checkconf /etc/init/django-project.conf

    I got:

    ERROR: File /etc/init/progi.conf: syntax invalid:
    init:progi.conf:14: Unknown stanza

    /etc/init/django-project.conf:

    description "Gunicorn daemon for Django project"
    
    start on (local-filesystems and net-device-up IFACE=eth0)
    stop on runlevel [!12345]
    
    # If the process quits unexpectadly trigger a respawn
    respawn
    
    setuid django
    setgid django
    chdir /home/django
    
    exec home/django/django-project/bin/gunicorn \
        --name=django-project \
        --pythonpath=django-project \
        --bind=0.0.0.0:9000 \
        --config /etc/gunicorn.d/gunicorn.py \
        django-project.wsgi:application
    
    
Have another answer? Share your knowledge.