We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers. Learn more →

How To Use the Django One-Click Install Image for Ubuntu 14.04

PostedMay 15, 2014 140.8k views Python Django One-Click Install Apps Python Frameworks PostgreSQL DigitalOcean Ubuntu

Status: Deprecated

This article is no longer being maintained. It uses a One-Click app that has been deprecated.

See Instead: An updated version of this article is available here: How To Use the Django One-Click Install Image for Ubuntu 16.04. It uses the Django 1.8.7 on Ubuntu 16.04 One-Click app instead.

Django is a high-level Python framework for developing web applications rapidly. DigitalOcean's Django One-Click app quickly deploys a preconfigured development environment to your VPS employing Django, Nginx, Gunicorn, and Postgres.

Creating the Django Droplet

To use the image, select Django on Ubuntu 14.04 from the Applications menu during droplet creation:

One-Click Apps

Once you create the droplet, navigate to your droplet's IP address (http://your.ip.address) in a browser, and verify that Django is running:

It worked!

You can now login to your droplet as root and read the Message of the Day, which contains important information about your installation:

MOTD

This information includes the username and password for both the Django user and the Postgres database. If you need to refer back to this latter, the information can be found in the file /etc/motd.tail

Configuration Details

The Django project is served by Gunicorn which listens on port 9000 and is proxied by Nginx which listens on port 80.

Nginx

The Nginx configuration is located at /etc/nginx/sites-enabled/django:

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

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/django_project/django_project/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/django_project/django_project/static; 
    }

    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;
    }
}

If you rename the project folder, remember to change the path to your static files.

Gunicorn

Gunicorn is started on boot by an Upstart script located at /etc/init/gunicorn.conf which looks like:

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 gunicorn \
    --name=django_project \
    --pythonpath=django_project \
    --bind=0.0.0.0:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    django_project.wsgi:application

Again, if you rename the project folder, remember to update the name and pythonpath in this file as well.

The Upstart script also sources a configuration file located in /etc/gunicorn.d/gunicorn.py that sets the number of worker processes:

"""gunicorn WSGI server configuration."""
from multiprocessing import cpu_count
from os import environ


def max_workers():
    return cpu_count() * 2 + 1

max_requests = 1000
worker_class = 'gevent'
workers = max_workers()

More information on configuring Gunicorn can be found in the project's documentation.

Django

The Django project itself is located at /home/django/django_project It can be started, restarted, or stopped using the Gunicorn service. For instance, to restart the project after having made changes run:

service gunicorn restart

While developing, it can be annoying to restart the server every time you make a change. So you might want to use Django's built in development server which automatically detects changes:

service gunicorn stop
python manage.py runserver localhost:9000

While convenient, the built in server does not offer the best performance. So use the Gunicorn service for production.

Writing Your First Django App

There are many resources that can provide you with an in-depth introduction to writing Django applications, but for now let's just quickly demonstrate how to get started. Log into your server and switch to the django user. Now let's create a new app in the project:

cd /home/django/django_project
python manage.py startapp hello

Your directory structure should now look like:

.
├── django_project
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── hello
│   ├── admin.py
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── manage.py

Next, we'll create our first view. Edit the file hello/views.py to look like:

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world! This is our first view.")

Then, we can connect that view to a URL by editing django_project/urls.py

from django.conf.urls import patterns, include, url
from hello import views
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^admin/', include(admin.site.urls)),
)

After that, we can restart the project as root: service gunicorn restart

If you reload the page, you'll now see:

Hello, world!

Next Steps

By Andrew Starr-Bochicchio

88 Comments

Creative Commons License