Question

Projects with different django version - virtualenv

  • Posted on February 4, 2015
  • HHHAsked by HHH

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?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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