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 16.04

PostedApril 27, 2017 2.3k views One-Click Install Apps Django Ubuntu Ubuntu 16.04

Introduction

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

Creating a Django Droplet

To create a Django Droplet, start on the Droplet creation page. In the Choose an image section, click the One-click apps tab and select the Django 1.8.7 on 16.04 image.

Next, select a size for your Droplet, your desired region, and any additional settings (like private networking IPv6 support, or backups). Add any SSH keys and fill in a hostname for your Droplet. When you're ready, click Create Droplet to spin up the server.

Once it's created, navigate to http://your_server_ip in your favorite browser to verify that Django is running. You'll see a page with the header It worked! Congratulations on your first Django-powered page.

You can now log into to your Droplet as root.

  • ssh root@your_server_ip

Make sure to read the Message of the Day, which contains important information about your installation, like the username and password for both the Django user and the Postgres database.

Login output
------------------------------------------------------------------------------- Thanks for using the DigitalOcean Django One-Click Application Image The "ufw" firewall is enabled. All ports except for 22, 80, and 443 are BLOCKED. Let's Encrypt has been pre-installed for you. If you have a domain name, and you will be using it with this 1-Click app, please see: http://do.co/le-lemp Django is configured to use Postgres as the database back-end. You can use the following SFTP credentials to upload your files (using FileZilla/WinSCP/Rsync): Host: your_server_ip User: django Pass: 2fd21d69bb13890c960b965c8c88afb1 You can use the following Postgres database credentials: DB: django User: django Pass: 9853a37c3bc81bfc15f264de0faa9da5 Passwords have been saved to /root/.digitalocean_passwords

If you need to refer back to this later, you can find the information in the file /etc/update-motd.d/99-one-click.

Configuration Details

The Django project is served by Gunicorn, which listens on /home/django/gunicorn.socket. Gunicorn is proxied by Nginx, which listens on port 80.

The Nginx configuration file is located at /etc/nginx/sites-enabled/django. If you rename the project folder, remember to change the path to your static files.

Gunicorn is started on boot by a Systemd file at /etc/systemd/system/gunicorn.service. This Systemd script also sources a configuration file located at /etc/gunicorn.d/gunicorn.py that sets the number of worker processes. You can find more information on configuring Gunicorn in the Gunicorn project's documentation.

The Django project itself is located at /home/django/django_project.

Note: If you rename the project folder, you need to make a few configuration file updates. Specifically, you need to change the path to your static files in the Nginx configuration. You also need to update the WorkingDirectory, name, and pythonpath in the Gunicorn Systemd file.

The project can be started, restarted, or stopped using the Gunicorn service. For instance, to restart the project after having made changes, run:

  • systemctl restart gunicorn.service

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

  • systemctl stop gunicorn.service
  • python manage.py runserver 0.0.0.0:8000

You can then access the application through http://your_server_ip:8000 in a browser. This built-in server does not offer the best performance, so it's best practice to use the Gunicorn service for production.

Writing Your First Django App

There are many in-depth guides on writing Django applications, but this step will just get you up and running with a very basic Django app.

If you haven't already, log into your server as root.

ssh root@your_server_ip

Next, switch to the django user.

  • su django

Move into the project directory.

  • cd /home/django/django_project

Now create a new app called hello.

python manage.py startapp hello

This will create a new directory named hello in the folder django_project. The whole directory tree will be structured like this:

Directory structure
.
├── django_project
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── settings.py.orig
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── hello
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── manage.py

It's not necessary, but you can generate this output yourself with the tree utility. Install it with sudo apt-get install tree and then use tree /home/django/django_project.

Next, create your first view. Open the file hello/views.py for editing using nano or your favorite text editor.

  • nano hello/views.py

It'll look like this originally:

Original hello/views.py
from django.shortcuts import render

# Create your views here.

Modify it to match the following. This tells Django to return Hello, world! This is our first view. as an HTTP response.

Modified hello/views.py
from django.shortcuts import render
from django.http import HttpResponse

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

Save and close the flie. Next, we need to connect the view we just created to a URL. To do this, open django_project/urls.py for editing.

  • nano django_project/urls.py

Add the following two lines to the file, which imports the view you just created and sets it to the default URL:

django_project/urls.py
. . .
from django.conf.urls import include, url
from django.contrib import admin
from hello import views

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

Save and close the file, then log out of the django user and return to the root shell.

  • exit

As root, restart the project.

  • systemctl restart gunicorn.service

Now, if you reload your Droplet's IP address, http://your_server_ip, you'll see a page with Hello, world! This is our first view.

Next Steps

You're ready to start working with Django. From here, you can:

0 Comments

Creative Commons License