// Tutorial //

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

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

This tutorial is out of date and no longer maintained.

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

<div class=“author”>By Andrew Starr-Bochicchio</div>


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 our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up

Tutorial Series: DigitalOcean 1-Click Application Images

This series links all of the DigitalOcean 1-Click Application images into a cohesive list. Articles are listed in the order that the images are shown on the create droplet screen. New articles are added as more application images are published.

About the authors
Default avatar
Andrew SB

author

Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
10 Comments

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!

I hope Digitalocean publish an updated droplet for Django 1.9.x (latest stable), Python 3.x (latest stable), Nginx (latest stable) , MariaDB (latest stable) configured for multisite !! :)

Someone agree ?

for anyone else having issues with static files, try this: sudo service nginx restart

Hi … I can’t locate the file /etc/init/gunicorn.conf

I was very disappointed to spend a couple hours on moving my app to a Django Droplet to find that this one-click is using Python 2. Please update the documentation so it explicitly states Python 2 only.

Also, I’d love to see a new Python3 based one-click droplet. Going to delete my droplet now. :-(

How can I make Django works with python3 by default.

This comment has been deleted

How to I set the gunicon start script to “python3 manage.py runserver localhost:9000”?

@asb Hi Andrew, is there a tutorial for 16.04 1-click install. I do not have this file: /etc/init/gunicorn.conf So hard to know how to proceed. A minor thing, but maybe highlighting any changes we need to make to config files like some of the other tutorials do would be a great addition :D

Looking forward to starting with this 1-click, so any help you can give regarding the 16.04 image would be great. I know there are a couple of other comments around this too :)

Hey guys, I just want to tell you that if you upgrade Django you should change your

alias /home/django/django_project/django_project/static; 

to

alias /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;

In this way you will have the latest admin with the latest static files

Another vote for this tutorial updated to work with Python 3.